struts2でアプリ作成(準備編1)
準備編としているが、Eclipseの準備はできている前提。
m2e(Maven管理用プラグインくらい。。。)
ここを参考にしました。
http://d.hatena.ne.jp/uriyuri/20080430/1209540139
プロジェクト作成
File->New->ProjectからWeb->Dynamic Web Projectを選択
プロジェクト名を入力。今回は「myapp」。
他はデフォルトのままで「Next」
Sourse Folders on build path:の設定画面が表示される。
src->削除
src/main/java 追加
src/main/resources 追加
src/test/java 追加
src/test/resources 追加
src/main/webapp 追加
「Next」
Web Module設定画面が表示される。
Content directoryを「src/main/webapp」に。
「Finish」
Maven有効化
プロジェクト右クリック->Configure->Convert to Maven Project
設定画面が表示されます。
とりあえず
version=1.0
Packaging=warに変更
プロジェクト直下にpom.xmlが作成されます。
pom.xml設定
今回はstruts2とconvention-pluginを使用する。
http://struts.apache.org/release/2.1.x/docs/convention-plugin.html
convention pluginは「設定より規約」の考えの下、設定ファイルの記載をできるだけ行わずに開発をするためのプラグインです。
pom.xmlをMaven pom Editorで開く。
Dependenciesタブを開き、「Dependencies」一覧のaddボタンをクリック。
「struts2-core」「struts2-convention-plugin」を検索して追加する。本日時点での最新が2.3.12なので、両方それを指定。
設定ファイル
web.xml(変更)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>myapp</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> </web-app>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> </struts>
テスト用画面の作成
src/main/webapp/index.jsp(web.xmlのwelcome-file)
<%@page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> </head> <body> トップページ </body> </html>
以下のTopActionとtop.jspはConvention Pluginによって自動的に日も付けられます。
src/main/java/kamegu/action/TopAction
ここではトップパッケージをkameguにしてます。
package kamegu.action; import com.opensymphony.xwork2.ActionSupport; public class TopAction extends ActionSupport { public String message1 = "テストです。その1"; public String execute() { return SUCCESS; } public String getMessage2() { return "テストです。その2"; } }
serialVersionUID に関するwarningが出る場合は、
プロジェクト右クリック->properties->Java compiler->Errors/Warning
でPotential programming probrems->Serializable class without serialVersionUIDをIgnoreにすると、出なくなります
src/main/webapp/content/top.jsp
<%@page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> </head> <body> トップ画面です <br> <s:property value="message1"/><br> <s:property value="message2"/><br> </body> </html>
サーバ作成、起動
Server Viewからサーバを新規追加し、プロジェクトを追加する。
サーバを起動し、
http://localhost:8080/myapp/
にアクセス。(ポート番号、コンテキスト名は正しいものに変える)
「トップページ」と表示されればOK
http://localhost:8080/myapp/top
「トップ画面です
テストです。その1
テストです。その2」
と表示されればOK。