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ファイルを配置する
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