StrutsをテーマにStrutsとはの概要、Struts1とは何か、Struts2とは何かをまとめています。
この記事の目次です。
Strutsとは、Apacheで開発が行われているJavaのWebアプリケーションのフレームワークです。 読み方は「ストラッツ」で正式名は「Apache Struts」です。
Strutsはバージョン1と2で大きく異なります。 以下ではStruts1とはどのようなものか、Struts2とはどのようなものか、サンプルコードと一緒に見ていきます。
Struts1とはどのようなものか見ていきます。
Struts1で簡単なHello Worldアプリケーションのサンプルを見ていきます。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ hello.jsp
│ index.jsp
└─WEB-INF
│ struts-config.xml
│ web.xml
├─classes
│ └─struts
│ IndexAction.class
│ SampleBean.class
├─lib
│ commons-beanutils-1.8.0.jar
│ commons-chain-1.2.jar
│ commons-digester-1.8.jar
│ commons-logging-1.0.4.jar
│ struts-core-1.3.10.jar
│ struts-taglib-1.3.10.jar
└─tld
struts-bean.tld
struts-html.tld
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにあるstruts-taglib-1.3.10.jarを解答し、META-INF\tldにあるstruts-bean.tldとstruts-html.tldを <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tldに配置します。
cd struts-1.3.10-all\lib
jar -xvf struts-taglib-1.3.10.jar
copy struts-bean.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
copy struts-html.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html>
<head>
<title>Welcomeページ</title>
</head>
<body>
<html:form action="/IndexAction">
<html:submit property="submit" value="表示" />
</html:form>
</body>
</html:html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<html:html>
<head>
<title>Helloページ</title>
</head>
<body>
<bean:write name="sampleBean" property="name" />!
</body>
</html:html>
package struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public final class IndexAction extends Action {
public ActionForward execute (
ActionMapping map, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
SampleBean sampleBean=(SampleBean)form;
sampleBean.setName("Hello World!!");
request.setAttribute("sampleBean",sampleBean);
return map.findForward("success");
}
}
package struts;
import org.apache.struts.action.ActionForm;
public final class SampleBean extends ActionForm {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="SampleBean" type="struts.SampleBean" />
</form-beans>
<action-mappings>
<action path="/IndexAction" type="struts.IndexAction"
name="SampleBean" scope="request">
<forward name="success" path="/hello.jsp" />
</action>
</action-mappings>
</struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
</web-app>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" reloadable="true" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/index.jsp」へアクセスすると 「ボタン」が表示されます。そして、「ボタン」を押すと画面遷移し「Hello World!!」と表示されます。
Struts1のstruts-config.xmlのmessage-resourcesタグ(プロパティファイルで設定した内容を表示)を見ていきます。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ hello.jsp
│ index.jsp
└─WEB-INF
│ struts-config.xml
│ web.xml
├─classes
│ ├─resources
│ │ message_sample.properties
│ └─struts
│ IndexAction.class
│ SampleBean.class
├─lib
│ commons-beanutils-1.8.0.jar
│ commons-chain-1.2.jar
│ commons-digester-1.8.jar
│ commons-logging-1.0.4.jar
│ struts-core-1.3.10.jar
│ struts-taglib-1.3.10.jar
└─tld
struts-bean.tld
struts-html.tld
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにあるstruts-taglib-1.3.10.jarを解答し、META-INF\tldにあるstruts-bean.tldとstruts-html.tldを <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tldに配置します。
cd struts-1.3.10-all\lib
jar -xvf struts-taglib-1.3.10.jar
copy struts-bean.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
copy struts-html.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html>
<head>
<title>Welcomeページ</title>
</head>
<body>
<html:form action="/IndexAction">
<html:submit property="submit" value="表示" />
</html:form>
</body>
</html:html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<html:html>
<head>
<title>Helloページ</title>
</head>
<body>
<bean:message key="message001"/>
</body>
</html:html>
package struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public final class IndexAction extends Action {
public ActionForward execute (
ActionMapping map, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("sampleBean",form);
return map.findForward("success");
}
}
package struts;
import org.apache.struts.action.ActionForm;
public final class SampleBean extends ActionForm {
private static final long serialVersionUID = 1L;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<message-resources parameter="resources/message_sample"/>
<form-beans>
<form-bean name="SampleBean" type="struts.SampleBean" />
</form-beans>
<action-mappings>
<action path="/IndexAction" type="struts.IndexAction"
name="SampleBean" scope="request">
<forward name="success" path="/hello.jsp" />
</action>
</action-mappings>
</struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
</web-app>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" reloadable="true" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/index.jsp」へアクセスすると 「ボタン」が表示されます。そして、「ボタン」を押すと画面遷移し「Hello World!!」と表示されます。
Action.exequte()メソッド(アクションクラスで設定した内容を表示)を見ていきます。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ hello.jsp
│ index.jsp
└─WEB-INF
│ struts-config.xml
│ web.xml
├─classes
│ └─struts
│ IndexAction.class
│ SampleBean.class
├─lib
│ commons-beanutils-1.8.0.jar
│ commons-chain-1.2.jar
│ commons-digester-1.8.jar
│ commons-logging-1.0.4.jar
│ struts-core-1.3.10.jar
│ struts-taglib-1.3.10.jar
└─tld
struts-bean.tld
struts-html.tld
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにあるstruts-taglib-1.3.10.jarを解答し、META-INF\tldにあるstruts-bean.tldとstruts-html.tldを <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tldに配置します。
cd struts-1.3.10-all\lib
jar -xvf struts-taglib-1.3.10.jar
copy struts-bean.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
copy struts-html.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html>
<head>
<title>Welcomeページ</title>
</head>
<body>
<html:form action="/IndexAction">
<html:submit property="submit" value="表示" />
</html:form>
</body>
</html:html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<html:html>
<head>
<title>Helloページ</title>
</head>
<body>
<bean:write name="sampleBean" property="name" />!
</body>
</html:html>
package struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public final class IndexAction extends Action {
public ActionForward execute (
ActionMapping map, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
SampleBean sampleBean=(SampleBean)form;
sampleBean.setName("Hello World!!");
request.setAttribute("sampleBean",sampleBean);
return map.findForward("success");
}
}
package struts;
import org.apache.struts.action.ActionForm;
public final class SampleBean extends ActionForm {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="SampleBean" type="struts.SampleBean" />
</form-beans>
<action-mappings>
<action path="/IndexAction" type="struts.IndexAction"
name="SampleBean" scope="request">
<forward name="success" path="/hello.jsp" />
</action>
</action-mappings>
</struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
</web-app>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" reloadable="true" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/index.jsp」へアクセスすると 「ボタン」が表示されます。そして、「ボタン」を押すと画面遷移し「Hello World!!」と表示されます。
Struts1のhtml:textタグのサンプル(テキストボックスで入力した内容を表示)を見ていきます。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ index.jsp
└─WEB-INF
│ struts-config.xml
│ web.xml
├─classes
│ └─struts
│ IndexAction.class
│ SampleBean.class
├─jsp
│ hello.jsp
├─lib
│ commons-beanutils-1.8.0.jar
│ commons-chain-1.2.jar
│ commons-digester-1.8.jar
│ commons-logging-1.0.4.jar
│ struts-core-1.3.10.jar
│ struts-taglib-1.3.10.jar
└─tld
struts-bean.tld
struts-html.tld
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。
Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにあるstruts-taglib-1.3.10.jarを解答し、META-INF\tldにあるstruts-bean.tldとstruts-html.tldを <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tldに配置します。
cd struts-1.3.10-all\lib
jar -xvf struts-taglib-1.3.10.jar
copy struts-bean.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
copy struts-html.tld <Tomcatホーム>\webapps\SampleStruts\WEB-INF\tld\.
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html>
<head>
<title>Welcomeページ</title>
</head>
<body>
<html:form action="/IndexAction">
<html:text property="name"/>
<html:submit property="submit" value="表示" />
</html:form>
</body>
</html:html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<html:html>
<head>
<title>Helloページ</title>
</head>
<body>
<bean:write name="sampleBean" property="name" />!
</body>
</html:html>
package struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public final class IndexAction extends Action {
public ActionForward execute (
ActionMapping map, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("sampleBean",form);
return map.findForward("success");
}
}
package struts;
import org.apache.struts.action.ActionForm;
public final class SampleBean extends ActionForm {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="SampleBean" type="struts.SampleBean" />
</form-beans>
<action-mappings>
<action path="/IndexAction" type="struts.IndexAction"
name="SampleBean" scope="request">
<forward name="success" path="/WEB-INF/jsp/hello.jsp" />
</action>
</action-mappings>
</struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
</web-app>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" reloadable="true" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/index.jsp」へアクセスすると「テキストボックス」と「ボタン」が表示されます。
「テキストボックス」に文字を入力して、「ボタン」を押すと画面遷移し、「<入力した文字>!!」と表示されます。
※<入力した文字>は、「テキストボックス」に入力した文字を表します。
Apache Struts2は、Strutsの次世代バージョンで、 Javaプラットフォームを対象としてオープンソースのWebアプリケーション・フレームワークです。
Struts2を使用してみて、たとえば、以下のような利点が感じられます。
まずは簡単なHello Worldアプリケーションのサンプルコードを見ていきます。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ index.jsp
└─WEB-INF
│ web.xml
├─classes
│ │ struts.xml
│ └─sample
│ └─struts
│ IndexAction.class
└─lib
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-codebehind-plugin-2.3.14.jar
struts2-core-2.3.14.jar
xwork-core-2.3.14.jar
Apache Strutsより、struts-2.3.14-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>サンプル</title></head>
<body>
ボタンを押すとメッセージが下に表示されます。
<s:form action="index.action"><s:submit value="ボタン"/></s:form>
<s:property value="message"/>
</body>
</html>
package sample.struts;
public class IndexAction {
public String message;
public String execute() {
message = "Hello World!!";
return "success";
}
}
<?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.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>sample.struts</param-value>
</init-param>
</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>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" docBase="<Webアプリケーションの置き場所>" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/」へアクセスすると 「ボタン」が表示されます。そして、「ボタン」を押すと「ボタン」の下に「Hello World!!」と表示されます。
Struts2のアノテーションについて見ていきます。
Struts2のResultアノテーション(ページ遷移先を指定するサンプル)を見ていきます。
Welcomeページ(index.jsp)へ遷移し、ボタンを押すとHelloページ(hello.jsp)へ遷移するサンプルを紹介しています。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ index.jsp
└─WEB-INF
│ web.xml
├─classes
│ │ struts.xml
│ └─sample
│ └─struts
│ IndexAction.class
├─jsp
│ hello.jsp
└─lib
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-codebehind-plugin-2.3.14.jar
struts2-core-2.3.14.jar
xwork-core-2.3.14.jar
Apache Strutsより、struts-2.3.14-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム/webapps>/SampleStruts/webapps/SampleStruts/WEB-INF/libに配置します。
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Welcomeページ</title></head>
<body>
ボタンを押すとhelloページに遷移します。
<s:form action="index.action">
<s:submit method="hello" value="ボタン"/>
</s:form>
</body>
</html>
package sample.struts;
import org.apache.struts2.config.Result;
@Result(name = "hello", value = "/WEB-INF/jsp/hello.jsp")
public class IndexAction {
public String message;
public String execute() {
return "success";
}
public String hello() {
message = "Hello World!!";
return "hello";
}
}
※本ソースのコンパイルには、以下のjarファイルが必要です。
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Helloページ</title></head>
<body>
<s:property value="message"/>
</body>
</html>
<?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.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>sample.struts</param-value>
</init-param>
</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>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" reloadable="true" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/」へアクセスすると Welcomeページが表示されます。
そして、「ボタン」を押すとHelloページに遷移し、「Hello World!!」と表示されます。
Struts2のプロパティファイルについて見ていきます。
Struts2のプロパティファイルの設定と取得(作成したmessage_ja.propertiesからメッセージを取得するサンプル)を見ていきます。
Welcomeページ(index.jsp)へ遷移し、ボタンを押すと作成したmessage_ja.propertiesから取得したメッセージが画面に表示されるサンプルを掲載しています。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ index.jsp
└─WEB-INF
│ web.xml
├─classes
│ │ message_ja.properties
│ │ struts.xml
│ └─sample
│ └─struts
│ IndexAction.class
└─lib
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-codebehind-plugin-2.3.14.jar
struts2-core-2.3.14.jar
xwork-core-2.3.14.jar
Apache Strutsより、struts-2.3.14-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム/webapps>/SampleStruts/webapps/SampleStruts/WEB-INF/libに配置します。
<%@ page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>サンプル</title>
</head>
<body>
<p>
<s:form action="index.action" theme="simple">
<s:submit value="データ取得"/>
</s:form>
</p>
<p>
<s:property value="label"/>
</p>
</body>
</html>
package sample.struts;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String label;
public String execute() {
String embeddings[] = {"こんにちは", "太郎"};
label = getText("MSG01",embeddings);
return "success";
}
}
※本ソースのコンパイルには、以下のjarファイルが必要です。
MSG01={0}!{1}さん!
<?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.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="message" />
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>sample.struts</param-value>
</init-param>
</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>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" reloadable="true" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/」へアクセスするとWelcomeページが表示されます。
そして、「ボタン」を「こんにちは!太郎さん! 」と表示されます。
Struts2のStrutsタグについて見ていきます。
Struts2のactionerrorタグ(ログインフォームサンプル)について見ていきます。
ログインフォームにメールアドレスとパスワードを入力し、不正入力の場合にログインフォーム上部にエラーメッセージが表示されるサンプルを掲載しています。
以下の環境で動作確認をおこないました。
<tomcatホームディレクトリ>/webappsの下に以下の構成を作成します。
□Tomcatホーム/webapps/
├─conf
│ server.xml
└─webapps
└─SampleStruts
│ index.jsp
└─WEB-INF
│ web.xml
├─classes
│ │ struts.xml
│ └─sample
│ └─struts
│ LoginAction.class
├─jsp
│ hello.jsp
└─lib
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-codebehind-plugin-2.3.14.jar
struts2-core-2.3.14.jar
xwork-core-2.3.14.jar
Apache Strutsより、struts-2.3.14-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム/webapps>/SampleStruts/webapps/SampleStruts/WEB-INF/libに配置します。
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>ログインページ</title>
</head>
<body>
<s:actionerror />
<s:form action="login.action">
<s:textfield label="メールアドレス" name="email"/>
<s:password label="パスワード" name="pass" />
<s:submit/>
</s:form>
</body>
</html>
package sample.struts;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import com.opensymphony.xwork2.ActionSupport;
@Results({
@Result(name="input",value="login.jsp"),
@Result(name="success",value="/WEB-INF/jsp/hello.jsp")
})
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String email;
public String pass;
public String execute() {
// 未入力チェック
if (email == null || email.length() == 0) {
addActionError("メールアドレスを入力してください。");
}
if (pass == null || pass.length() == 0) {
addActionError("パスワードを入力してください。");
}
if (hasActionErrors()) {
return "input";
}
// 入力内容チェック
if (!(email.equals("e@ma.il") && pass.equals("pass"))) {
addActionError("入力内容が不正です。");
}
if (hasActionErrors()) {
return "input";
}
// エラーが無ければ成功
return "success";
}
}
※本ソースのコンパイルには、以下のjarファイルが必要です。
<%@ page language="java"
contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Helloページ</title></head>
<body>
ようこそ!<s:property value="email"/>さん!
</body>
</html>
<?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.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>sample.struts</param-value>
</init-param>
</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>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
:
<省略>
:
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
:
<省略>
:
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
:
<省略>
:
<Context path="/SampleStruts" reloadable="true" />
:
<省略>
:
</Host>
</Engine>
</Service>
</Server>
Tomcatを起動し、ブラウザより「http://localhost:8080/SampleStruts/」へアクセスすると ログインページが表示されます。
そして、フォームにメールアドレスとパスワードを入力し、「ボタン」を押すとHelloページに遷移し、「ようこそ!<メールアドレス>さん!」と表示されます。
ただし、メールアドレスまたはパスワードが不正の場合、ログインページのフォーム上部にエラーメッセージが表示されます。
この記事の更新履歴です。
サイト内のページ
言語
C・C++
/HTML
/Java
/JavaScript
/PHP
/シェルスクリプト
開発環境
Ant
/Eclipse
/gcc
/gdb
/g++
/JDK
/JUnit
/ZAP
技術・仕様
Ajax
/CORBA
/Java EE(旧称J2EE)
/JNI
ライブラリ/Framework/CMS
jQuery
/Lucene
/MyBatis
/Spring
/Struts
/Seasar2
/WordPress
ITインフラ
OSとミドルウェア
Linux
/Windows
/シェル
Apache/Tomcat
/MySQL
/Redis
/Solr
/vsftpd
ITインフラ
サーバー
Web公開サーバー構築
ITインフラ
セキュリティ
公開サーバーのセキュリティ
SI
ホームページの作り方
/小さな会社のISMS
スポンサーリンク
関連サイト内検索ツール
zealseedsおよび関連サイト内のページが検索できます。
IPアドレス確認ツール
あなたのグローバルIPアドレスは以下です。
54.173.237.152
HTMLの表示色確認ツール
パスワード生成ツール
文字数のプルダウンを選択して、取得ボタンを押すと「a~z、A~Z、0~9」の文字を ランダムに組み合わせた文字列が表示されます。
ここに生成されます。
スポンサーリンク