技術メモのかけら

内容はもとより調べたことすら忘れてしまうので個人的なメモです。とにかく短く、結論だけ書いていきます。

Struts2最小構成サンプル

Struts2はバージョンによる違いが大きくネット上の情報はあてにならないので、公式サイトを中心に見ていくことにする。
手始めにチュートリアルを参考に単純なサンプルを動かしてみる。

プロジェクトを作成

Eclipseから適当な名前で動的Webプロジェクトを作成する。

index.jspを作成

WebContentフォルダ直下に作成する。

<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
</head>
<body>
<h1>HelloWorld</h1>
</body>
</html>
jarファイルの設定

WebContent/WEB-INF/libに必須jarファイルを配置する

  1. commons-fileupload-X.X.X.jar
  2. commons-io-X.X.X.jar
  3. commons-lang-X.X.jar
  4. commons-logging-X.X.X.jar
  5. commons-logging-api.X.X.jar
  6. freemarker-X.X.X.jar
  7. ognl-X.X.X.jar
  8. struts2-core-X.X.X.X.jar
  9. xwork-core-X.X.X.jar
  10. javassist-X.X.X.jar
Web.xmlの設定
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml

Actionには何も指定しない。index.actionが呼ばれたら、index.jspを表示するだけ設定。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<constant name="struts.devMode" value="true" />

	<package name="basicstruts2" extends="struts-default">

		<action name="index">
			<result>/index.jsp</result>
		</action>

	</package>

</struts>
動作確認

Tomcatに配置して以下のURLで動作を確認する。
http://localhost:8080/StrutsWork/index.action