0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

📘 Vol.7.3:Struts2 における struts.xml の書き方と役割

Last updated at Posted at 2025-05-20

本記事では、Struts2 フレームワークにおいて中核を成す設定ファイル struts.xml の役割と構成、そして処理の流れについて詳しく解説します。


🧭 全体の処理の流れ(login.jsp を例に)

Struts2 アプリケーションでは、画面(JSP)から送信されたリクエストが struts.xml の設定に従って Action クラスにマッピングされ、そこでロジックが処理されます。

ここではログイン処理を例に、処理の始まりから終わりまでを順を追って見ていきます。


1️⃣ アプリケーション起動直後に最初に読み込まれるページ(web.xml)

<welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
</welcome-file-list>
  • アプリ起動時、まずこの設定により login.jsp が呼び出されます。

  • web.xml はアプリケーションの起点を定義する デプロイメントディスクリプタ です。


2️⃣ ユーザーがログインフォームから情報を入力し、送信

<s:form action="login" method="post">
  <label>ユーザー名:</label>
  <s:textfield name="user.user_name" label="ログインID" />
  <br><br>
  <label>パスワード:</label>
  <s:password name="user.password" label="パスワード" />
  <br><br>
  <s:submit value="ログイン" />
</s:form>
  • action="login" により、login.action という URL に POST リクエストが送られます。

  • この時点ではまだ JSP 側しか見えていませんが、この action 名は Struts2 の struts.xml によって解釈されます。


3️⃣ Struts2 のエントリポイント:struts.xml

<action name="login"
  class="com.company.bulletinboard.action.login.LoginAction">
  <result name="login">/login.jsp</result>
  <result name="admin">/view/ManagementMenu.jsp</result>
  <result name="user">/view/userPortal.jsp</result>
  <result name="input">/login.jsp</result>
</action>
  • name="login" により、login.action のリクエストがこの にマッチします。

  • class="LoginAction" が処理を担当します(Controller の役割)。

  • result name="○○" により、LoginAction の戻り値に応じて遷移先の JSP を切り替えます。

🔁 つまり、何がトリガーなのか?

Struts2 におけるトリガー(起点)は、JSP などで指定される の action 名です。
これが内部的には xxx.action という URL にマッピングされ、struts.xml の によって該当の Action クラスが呼ばれます。


📌 Struts2 の処理フロー図(簡略)

Webブラウザ
    ↓ ①アクセス
web.xml
    ↓ ②login.jsp 表示
login.jsp の <s:form action="login">
    ↓ ③POST: login.action
StrutsPrepareAndExecuteFilter(内部)
    ↓
struts.xml の <action name="login">
    ↓
LoginAction(BaseAction 継承)
    ↓ ④mainProc() 実行 → 戻り値で遷移
    ↓
結果に応じて JSP 表示

🛠 struts.xml の役割まとめ

項目 説明
<action name="~"> JSP の action 属性と対応するリクエスト名
class="~" 対応する Action クラス(Controller)
<result name="~"> Action の戻り値に応じた遷移先の JSP
input バリデーションエラー時の戻り先(自動で処理される)

📎 関連リンク
🔄 Vol.7.2:Struts2 における Controller(コントローラ)の責務
https://qiita.com/juehara-crypto/items/06b4f9296a296db8f4ba

🧱 Vol.5:共通処理クラス(BaseAction)の設計と活用
https://qiita.com/juehara-crypto/items/a5f244a5f58597f33d06

💻 GitHub ソースコードはこちら
https://github.com/juehara-crypto/Portfolio_Bulletinboard/tree/master/03.%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%A0/Bulletinboard/src/main/java/com/company/bulletinboard/action


🧩 次回予告(Vol.7.4)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?