トップ ライブラリ/Framework/CMS Struts Struts1

Struts1とは―どのようなものか?

Struts1とはどのようなものか見ていきます。

▲記事トップへ

目次

この記事の目次です。

1. Struts1とは
2. 簡単なWebアプリケーションのサンプル
3. struts-config.xmlのmessage-resourcesタグ(プロパティファイルで設定した内容を表示)
4. Action.exequte()メソッド(アクションクラスで設定した内容を表示)
5. html:textタグのサンプル(テキストボックスで入力した内容を表示)

もっと知識を広げるための参考
更新履歴

1. Struts1とは

Struts1とは、JavaコードはJSPファイルと分離できる整備されたJSPカスタムタグ、画面の遷移を設定ファイル(struts-config.xml)で簡単に変更できる機能が特徴のフレームワークです。 最終リリースは2008年10月4日の1.3.10で、2013年4月5日にサポート終了しました。 2014年には ClassLoader が操作可能な脆弱性など深刻な脆弱性も見つかっています。

2. 簡単なWebアプリケーションのサンプル

Struts1を使用した簡単なWebアプリケーションのサンプルです。

詳細

3. struts-config.xmlのmessage-resourcesタグ(プロパティファイルで設定した内容を表示)

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

関連ライブラリ(lib下に置くjarファイル)

Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。

タグライブラリ(tld下に置くtldファイル)

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\.

ソースコード

ページ(index.jsp)

<%@ 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>

ページ(hello.jsp)

<%@ 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>

アクションクラス(IndexAction.java)

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");
  }
}

ビーンクラス(SampleBean.java)

package struts;
import org.apache.struts.action.ActionForm;

public final class SampleBean extends ActionForm {
  private static final long serialVersionUID = 1L;
}

Strutsの設定ファイル(struts-config.xml)

<?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>

アプリケーションの配備記述子(web.xml)

<?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>

Tomcatの設定ファイル(server.xml)

<?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!!」と表示されます。

4. Action.exequte()メソッド(アクションクラスで設定した内容を表示)

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

関連ライブラリ(lib下に置くjarファイル)

Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。

タグライブラリ(tld下に置くtldファイル)

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\.

ソースコード

ページ(index.jsp)

<%@ 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>

ページ(hello.jsp)

<%@ 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>

アクションクラス(IndexAction.java)

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");
  }
}

ビーンクラス(SampleBean.java)

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;
  }
}

Strutsの設定ファイル(struts-config.xml)

<?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>

アプリケーションの配備記述子(web.xml)

<?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>

Tomcatの設定ファイル(server.xml)

<?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!!」と表示されます。

5. html:textタグのサンプル(テキストボックスで入力した内容を表示)

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

関連ライブラリ(lib下に置くjarファイル)

Apache Strutsより、struts-1.3.10-all.zipをダウンロード・解凍し、 libフォルダにある以下のjarファイルを<Tomcatホーム>\webapps\SampleStruts\WEB-INF\libに配置します。

タグライブラリ(tld下に置くtldファイル)

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\.

ソースコード

ページ(index.jsp)

<%@ 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>

ページ(hello.jsp)

<%@ 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>

アクションクラス(IndexAction.java)

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");
  }
}

ビーンクラス(SampleBean.java)

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;
  }
}

Strutsの設定ファイル(struts-config.xml)

<?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>

アプリケーションの配備記述子(web.xml)

<?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>

Tomcatの設定ファイル(server.xml)

<?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」へアクセスすると「テキストボックス」と「ボタン」が表示されます。

「テキストボックス」に文字を入力して、「ボタン」を押すと画面遷移し、「<入力した文字>!!」と表示されます。

※<入力した文字>は、「テキストボックス」に入力した文字を表します。

もっと知識を広げるための参考

もっと知識を広げるための参考です。

Struts

StrutsをテーマにStrutsとはの概要、Struts1とは何か、Struts2とは何かをまとめています。

詳細

更新履歴

この記事の更新履歴です。

戻る

スポンサーリンク

サイト内のページ

言語
C・C++ /HTML /Java /JavaScript /PHP /シェルスクリプト

開発環境
Ant /Burp /Eclipse /Fiddler /gcc /gdb /Git /g++ /JDK /JMeter /JUnit /Teraterm /ZAP

技術・仕様
Ajax /CORBA /Jakarta EE(旧称J2EE、Java EE) /JNI

ライブラリ/Framework/CMS
bootstrap /jQuery /FuelPHP /Lucene /MyBatis /Seasar2 /Spring /Struts /WordPress

Web API
Google Maps

ITインフラOSとミドルウェア
Linux /Windows /シェル
ActiveMQ /Tomcat /MariaDB /MySQL /Nagios /Redis /Solr

ITインフラサーバー
公開Webサーバー

ITインフラネットワーク
プログラミング /構築

ITインフラセキュリティ
公開サーバーのセキュリティ

PC製品
ZOTAC

SI
ホームページの作り方

その他
IT用語 /ITスキル体系

スポンサーリンク

関連サイト内検索ツール

zealseedsおよび関連サイト内のページが検索できます。

IPアドレス確認ツール

あなたのグローバルIPアドレスは以下です。

3.143.168.172

HTMLの表示色確認ツール

パスワード生成ツール

文字数のプルダウンを選択して、取得ボタンを押すと「a~z、A~Z、0~9」の文字を ランダムに組み合わせた文字列が表示されます。

ここに生成されます。

スポンサーリンク