Struts2.5の環境構築を行ってみた。
Struts2ではいろいろな設計モデルが可能みたいで、
今回は1画面1アクションのパターンで構築してみた。
以下のアクション遷移のパターンで構築してみた。
以下URLからダウンロードします。
https://struts.apache.org/download.cgi
使用するjarファイルは以下となります。
commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang3-3.6.jar
commons-logging-1.1.3.jar
freemarker-2.3.23.jar
javassist-3.20.0-GA.jar
log4j-api-2.8.2.jar
ognl-3.1.15.jar
struts2-core-2.5.13.jar
Webアプリケーションの構築を行います。
Webアプリケーションの構成は以下です。
lib配下にダウンロードしたJarファイルを配置します。(対象Jarファイルは上記に記載)
ビルドパスの構成の「出力フォルダー」が「xxx/WEB-INF/classes」になっていること。
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?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>SampleWebStruts2</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
struts.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- chainを使用するために必要な設定 --> <constant name="struts.mapper.action.prefix.enabled" value="true" /> <package name="default" extends="struts-default"> <action name="Sample1" class="sample.struts2.action.Sample1Action"> <result name="success">/WEB-INF/jsp/Sample1.jsp</result> </action> <action name="Sample1.doAdd" method="doAdd" class="sample.struts2.action.Sample1Action"> <result type="chain" name="success">Result</result> </action> <action name="Sample1.doUpdate" method="doUpdate" class="sample.struts2.action.Sample1Action"> <result type="chain" name="success">Result</result> </action> <action name="Sample1.doDelete" method="doDelete" class="sample.struts2.action.Sample1Action"> <result type="chain" name="success">Result</result> </action> <action name="Result" class="sample.struts2.action.ResultAction"> <result name="success">/WEB-INF/jsp/Result.jsp</result> </action> </package> </struts> |
Sample1Action.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package sample.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class Sample1Action extends ActionSupport { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } /** シリアルバージョン. */ private static final long serialVersionUID = 1L; public String execute() { System.out.println("Sample1Action execute >>>>>"); return SUCCESS; } public String doAdd() { System.out.println("Sample1Action doAdd >>>>>"); message = "追加しました。"; return SUCCESS; } public String doUpdate() { System.out.println("Sample1Action doUpdate >>>>>"); message = "更新しました。"; return SUCCESS; } public String doDelete() { System.out.println("Sample1Action doDelete >>>>>"); message = "削除しました。"; return SUCCESS; } } |
ResultAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package sample.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class ResultAction extends ActionSupport { /** シリアルバージョン. */ private static final long serialVersionUID = 1L; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute() { System.out.println("ResultAction execute >>>>>"); this.message = this.message + "★処理結果★"; return SUCCESS; } } |
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<%@ page contentType="text/html; charset=UTF-8" %> <%@taglib uri="/struts-tags" prefix="s" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Index</title> </head> <body> <h1>Index</h1> <s:form action="Sample1"> <s:submit value="Sample1" /> </s:form> </body> </html> |
Sample1.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<%@ page contentType="text/html; charset=UTF-8" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head><title>Sample1</title></head> <body> <h1>Sample1</h1> <s:form action="Sample1" > <s:submit value="再表示" /> <s:submit action="Sample1.doAdd" value="追加"/><br> <s:submit action="Sample1.doUpdate" value="更新"/><br> <s:submit action="Sample1.doDelete" value="削除"/><br> </s:form> </body> </html> |
Result.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@ page contentType="text/html; charset=UTF-8" %> <%@taglib uri="/struts-tags" prefix="s" %> <html> <head><title>結果</title></head> <body> <h1>結果</h1> ${message} <s:form action="Sample1"> <s:submit value="Sample1" /> </s:form> </body> </html> |
Tomcatを起動し実行の確認を行います。
以下URLにアクセス
http://localhost:8080/SampleWebStruts2/
Index画面が表示されます。「Sample1」ボタンを押します。
Sample1画面が表示されました。
「追加」ボタンを押します。
結果画面が表示されした。
メッセージが追加処理時のメッセージになっていることを確認します。
無事構築できました☆
参考にしたURL)
https://ameblo.jp/tyoku123/entry-10431520052.html
https://www.zanmai.net/blog/data/104.html
https://dzone.com/tutorials/java/struts-2/struts-2-example/dispatchAction-in-struts-2-example-1.html
以上です。