タイトル: ActionMapping
SEOタイトル: Struts1 の ActionMapping とは? <action> 定義・ActionForward・findForward の使い方
| この記事の要点 |
|
ActionMapping とは
org.apache.struts.action.ActionMapping は Struts1 のコアクラスで、struts-config.xml に書かれた 要素 1 件分の情報を保持します。フレームワークはリクエスト URL に対応する ActionMapping を見つけ、それを Action#execute() の第 1 引数として渡します。
struts-config.xml の典型例
属性の意味
| 属性 | 意味 | 備考 |
|---|---|---|
path | リクエスト URL の論理名(拡張子前) | /login なら /login.do が来る |
type | 実行する Action クラス FQDN | 必須 |
name | 使用する ActionForm の論理名 | form-beans 内の name と対応 |
scope | フォームのスコープ | request / session |
input | 検証失敗時の戻り先 JSP | validate="true" 時に必須 |
validate | フォーム検証を行うか | true で validate() が呼ばれる |
parameter | サブメソッド指定(DispatchAction 用) | method 等 |
Action#execute() での使い方
package com.example.action;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
LoginForm lf = (LoginForm) form;
// ビジネスロジック
boolean ok = AuthService.login(lf.getUserId(), lf.getPassword());
if (ok) {
// へ
return mapping.findForward("success");
} else {
// ActionErrors を積んで戻る
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.login.failed"));
saveErrors(request, errors);
// へ
return mapping.findForward("failure");
}
}
}
findForward() の動作順
mapping.findForward("success") は次の順で ActionForward を探します:
- その
配下の - 見つからなければ
配下の - それも無ければ
nullを返す(白画面 / NPE の元)
入力検証失敗時のフロー(input 属性)
validate="true" の場合、ActionForm#validate() が execute() より先に呼ばれます。ActionErrors が空でなければ execute() はスキップされ、input 属性の JSP に戻ります:
public class LoginForm extends ActionForm {
private String userId;
private String password;
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (userId == null || userId.isEmpty()) {
errors.add("userId", new ActionMessage("error.userId.required"));
}
if (password == null || password.length() < 8) {
errors.add("password", new ActionMessage("error.password.length"));
}
return errors; // 空でなければ input 属性の JSP へ
}
// getter/setter ...
}
modules による分割
大規模アプリでは機能単位に config を分けられます:
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
config/order
/WEB-INF/struts-config-order.xml
config/admin
/WEB-INF/struts-config-admin.xml
URL は /order/list.do, /admin/users.do のように module 名が prefix になります。
Spring MVC への移行
Struts1 は Apache 公式 2013 年 12 月 EOL。新規開発はもちろん、既存も計画的に Spring MVC / Spring Boot に移行すべきです。対応関係:
| Struts1 | Spring MVC |
|---|---|
| @RequestMapping("/login") |
ActionForm | @ModelAttribute の DTO |
ActionForward | return "home" (View 名) |
findForward("success") | 同上 / ModelAndView |
validate() | @Valid + Bean Validation |
struts-config.xml | Java Config / @Configuration |
FAQ
Q: findForward() が null を返す
A: のスペルミスが多い。 にも無いか確認。
Q: input 属性が無いと?
A: validate="true" で検証失敗時に java.lang.IllegalStateException: No input attribute。input は必須。
Q: redirect="true" の意味
A: RequestDispatcher.forward() ではなく sendRedirect() を使う。request 属性は失われるので注意。