LoginSignup
3
3

More than 5 years have passed since last update.

Struts2の独自intercepterにてエラーメッセージを格納する。

Last updated at Posted at 2014-09-04

Struts2にてエラーメッセージを使用するときはActionSupportのaddActionErrorを使用することが多いと思いますが、
独自に定義するintercepter内ではActionSupportを継承していないので、そのまま使えません。

なので、下記のようにActionInvocationからActionSupportを呼び出して、利用しましょう。

独自のintercepter

public String intercept(ActionInvocation invocation) throws Exception {

    try {

        // ここに処理を書きます。 


    } catch (Exception e) {

        ActionSupport action = (ActionSupport) invocation.getAction();
        action.addActionError(action.getText("ここにエラーメッセージを入れます。"));

        // エラー画面に遷移する。
        // struts.xmlにエラー先を定義されているものとする。
        return "error"; 
    }

    return invocation.invoke();
}

以上です。

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