Raspberry Pi 4 で Struts1.x を動かす!
はじめに
レガシーシステムの保守案件のために、Struts1.xを覚える必要がありました。Struts1.xは、最後のバージョンが1.3.10ですでにサポートが終了されてます。情報も少なく、ChatGPTが頼りになります。 今回は Raspberry Pi 4 で Struts1.3.10 を動かすことに成功しましたので、その手順を記します。なお、 Struts1.3.10 を動かすために Tomcat9 が必要になります。Tomcat9 は設定済みのものとして解説していきます。
Struts 1とは
Struts 1は、JavaによるWebアプリケーションのフレームワークであり、Apache Software Foundationが開発したものです。2000年代初頭に非常に人気がありましたが、現在ではStruts 2に移行され、サポートも終了しています。それでも、歴史的な観点や既存のプロジェクトでStruts 1に触れることはあるかもしれません。
以下に、Struts 1の基本的な概念とアーキテクチャについて説明します。
Struts 1の基本概念
Model-View-Controller (MVC) アーキテクチャ
- Model: ビジネスロジックやデータアクセスの層。通常、JavaBeansやEJBが使われます。
- View: プレゼンテーション層。JSPが一般的に使用されます。
- Controller:
リクエストを処理し、適切なビューに転送する層。Struts
1では、
ActionServlet
がこれを担当します。
ActionServlet
リクエストを受け取り、対応するAction
クラスに処理を委譲します。
struts-config.xml
という設定ファイルで、リクエストパスとAction
クラスの対応を定義します。
Actionクラス
- 各リクエストに対するビジネスロジックを実装するクラス。
Action
クラスはexecute
メソッドを持ち、このメソッドがリクエスト処理のエントリーポイントとなります。
Form Beans
- ユーザ入力データをカプセル化するJavaBeanクラス。
- HTMLフォームのデータをJavaオブジェクトとして扱うために使用されます。
ActionForward
- リクエスト処理後の遷移先を定義します。
Action
クラスのexecute
メソッドの戻り値として使用され、次のビュー(JSPなど)にリクエストを転送します。
必要なディレクトリの作成
以下のコマンドを実行して、必要なディレクトリを作成します。
sudo mkdir -p /opt/tomcat9/webapps/strutsapp/WEB-INF/lib
JARファイルのダウンロード
作成したディレクトリへ移動して、MavenリポジトリからStruts 1.3.10のコアライブラリをダウンロードします。
cd /opt/tomcat9/webapps/strutsapp/WEB-INF/lib
sudo wget https://repo1.maven.org/maven2/org/apache/struts/struts-core/1.3.10/struts-core-1.3.10.jar
sudo wget https://repo1.maven.org/maven2/org/apache/struts/struts-taglib/1.3.10/struts-taglib-1.3.10.jar
sudo wget https://repo1.maven.org/maven2/org/apache/struts/struts-tiles/1.3.10/struts-tiles-1.3.10.jar
sudo wget https://repo1.maven.org/maven2/org/apache/struts/struts-extras/1.3.10/struts-extras-1.3.10.jar
さらに以下のコマンドを実行して、これらの依存ライブラリもダウンロードします。
sudo wget https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.9.4/commons-beanutils-1.9.4.jar
sudo wget https://repo1.maven.org/maven2/commons-logging/commons-logging/1.2/commons-logging-1.2.jar
sudo wget https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar
sudo wget https://repo1.maven.org/maven2/commons-digester/commons-digester/2.1/commons-digester-2.1.jar
sudo wget https://repo1.maven.org/maven2/commons-chain/commons-chain/1.2/commons-chain-1.2.jar
sudo wget https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar
Strutsアプリケーションの設定
Webアプリケーションのデプロイメント記述子
web.xml
を作成します。
sudo vi /opt/tomcat9/webapps/strutsapp/WEB-INF/web.xml
以下の内容を追加します。
<!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>
<display-name>Struts Application</display-name>
<
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>1</load-on-startup>
<servlet>
</
servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
<servlet-mapping>
</
welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file-list>
</web-app> </
Strust設定ファイルの作成
次に struts-config.xml
を作成します。
sudo vi /opt/tomcat9/webapps/strutsapp/WEB-INF/struts-config.xml
以下の内容を追加します。
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
>
"http://struts.apache.org/dtds/struts-config_1_3.dtd"struts-config>
<form-beans>
<form-bean name="helloForm" type="org.apache.struts.action.ActionForm"/>
<form-beans>
</
action-mappings>
<<!-- ForwardActionのparameter属性を追加 -->
action path="/hello" type="org.apache.struts.actions.ForwardAction" parameter="/hello.jsp"/>
<action-mappings>
</struts-config> </
JSPファイルの作成
JSPファイルを作成します。
sudo vi /opt/tomcat9/webapps/strutsapp/hello.jsp
以下の内容を追加します。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
!DOCTYPE html>
<
<html>
<head>
<title>Hello Struts</title>
</head>
<body>
<h1>Hello, Struts!</h1>
</body> </html>
ここまでで、 /opt/tomcat9/webapps/strutsapp
以下の構造は次の通りです。
/opt/tomcat9/webapps/strutsapp/
|-- hello.jsp
`-- WEB-INF
|-- lib
| |-- commons-beanutils-1.9.4.jar
| |-- commons-chain-1.2.jar
| |-- commons-collections-3.2.2.jar
| |-- commons-digester-2.1.jar
| |-- commons-logging-1.2.jar
| |-- struts-core-1.3.10.jar
| |-- struts-extras-1.3.10.jar
| |-- struts-taglib-1.3.10.jar
| `-- struts-tiles-1.3.10.jar
|-- struts-config.xml
`-- web.xml
これらのファイル群は、tomcat9が実行されるユーザー権限に変えておきます。
sudo chown -R pi:pi /opt/tomcat9/webapps/strutsapp
sudo chmod -R 755 /opt/tomcat9/webapps/strutsapp
ファイル/ディレクトリ | 説明 |
---|---|
/opt/tomcat9/webapps/strutsapp/ | アプリケーションのルートディレクトリ。Tomcatのwebappsディレクトリにデプロイされています。 |
hello.jsp | クライアントに表示されるJSPページ。このページは/hello リクエストに対するレスポンスとして表示されます。 |
WEB-INF/ | Webアプリケーションの設定ファイルやライブラリを格納するディレクトリ。直接ブラウザからアクセスできません。 |
lib/ | アプリケーションで使用するライブラリ(JARファイル)を格納するディレクトリ。 |
commons-beanutils-1.9.4.jar | Apache Commons BeanUtilsライブラリ。JavaBeansの操作を簡素化するためのツールを提供します。 |
commons-chain-1.2.jar | Apache Commons Chainライブラリ。チェインパターンを実装するためのツールを提供します。 |
commons-collections-3.2.2.jar | Apache Commons Collectionsライブラリ。コレクションフレームワークの拡張機能を提供します。 |
commons-digester-2.1.jar | Apache Commons Digesterライブラリ。XMLの解析とマッピングをサポートします。 |
commons-logging-1.2.jar | Apache Commons Loggingライブラリ。ログ記録のための統一インターフェースを提供します。 |
struts-core-1.3.10.jar | Struts 1フレームワークのコアライブラリ。MVCアーキテクチャをサポートします。 |
struts-extras-1.3.10.jar | Struts 1の追加機能ライブラリ。拡張機能や追加のアクションを提供します。 |
struts-taglib-1.3.10.jar | Struts 1のタグライブラリ。JSPで使用するカスタムタグを提供します。 |
struts-tiles-1.3.10.jar | Struts 1のTilesフレームワーク。テンプレートベースのページレイアウトをサポートします。 |
struts-config.xml | Struts 1の設定ファイル。アクションマッピング、フォームBean、その他の設定を定義します。 |
web.xml | Webアプリケーションのデプロイメント記述子。サーブレット、フィルタ、リスナーの設定を定義します。 |
Tomcatの再起動
設定を反映させるためにTomcatを再起動します。
sudo systemctl restart tomcat9
Strutsアプリケーションの確認
ブラウザを開き、
http://raspberry.local:8080/strutsapp/hello.do
URLにアクセスしてStrutsアプリケーションが正しく動作するか確認します。

3日かけて、ようやく Struts 1.3.10 を動作させることができました!なかなか苦労しました(笑)
関連記事
- MavenでJavaプロジェクトをビルドするまで【macOS】
- 【XYペンプロッター制作⑦】サーボモータでペンを上下させる(仮完成)
- 【XYペンプロッター制作④】Grbl v0.9とCNCjsのインストール
- ArduinoでC++で作った自作ライブラリを使う方法
- FlaskアプリケーションをGunicornのWSGIサーバーで運用する方法|macOS