1.

getInputForward

編集
この記事の要点
  • Struts 1 の ActionMapping#getInputForward() メソッド
  • input 属性に指定した遷移先ActionForward として取得
  • 主用途: フォームのバリデーション失敗時に入力画面へ戻す
  • return mapping.getInputForward();execute() 内で呼ぶ

 

Struts 1 の ActionMapping#getInputForward() についての記事です。struts-config.xml<action> タグで指定した input 属性の遷移先を、ActionForward として取得します。入力エラー時に元のフォーム画面へ戻すときに使う代表的なメソッドです。

定義

修飾子と型 メソッドと説明
public ActionForward getInputForward()
struts-config.xml」で設定した<action>タグ内のinputに指定した画面に遷移します。

typical な使い方

フォームのバリデーションが失敗したとき、入力画面に戻すために execute() 内から呼びます。

public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest req,
        HttpServletResponse res) throws Exception {

    ActionErrors errors = validateInput(form);
    if (!errors.isEmpty()) {
        saveErrors(req, errors);
        return mapping.getInputForward(); // 入力画面に戻る
    }

    // 正常処理
    return mapping.findForward("success");
}

struts-config.xml 側の設定

input 属性が指している遷移先がそのまま getInputForward() の戻り値になります。

<action path="/userRegister"
        type="com.example.UserRegisterAction"
        name="userForm"
        scope="request"
        input="/user/registerForm.jsp">
    <forward name="success" path="/user/registerSuccess.jsp"/>
</action>

この場合、getInputForward()/user/registerForm.jsp を指す ActionForward を返します。

findForward との違い

メソッド遷移先の決め方主な用途
getInputForward()<action>input 属性入力エラー時に元の入力画面に戻る
findForward("name")<forward name="..."/>path正常処理後の遷移、複数パターンの分岐

注意点

  • input 属性が <action> に書かれていないと null や設定不備で例外になる場合がある
  • 自動入力検証 (validate="true") を使う場合、エラー時には Struts が自動で input 先へ戻すため、getInputForward() を明示的に呼ぶ必要はない
  • Struts 1 系は 2013 年に EOL。新規開発では Spring MVC 等が選択肢
編集
Post Share
子ページ

子ページはありません

同階層のページ

同階層のページはありません