LoginSignup
4
4

More than 5 years have passed since last update.

Struts2で独自インターセプター内でリダイレクト先を動的に変えたい

Last updated at Posted at 2014-08-25

Struts2で独自に定義したインターセプター内で動的にリダイレクト先を変える処理を書きたかったのだが、色々ハマったのでメモ。

struts.xmlにリダイレクト先を追加。

リダイレクト先はOGNL式で動的に処理できるように定義しておく。

struts.xml

<result name="redirect" type="redirect">
    <param name="location">${redirectUrl}</param>
</result>

インターセプター内でリダイレクト先を格納。


/** リダイレクト先URL */
private String redirectUrl;

public String intercept(ActionInvocation invocation) throws Exception {


    // 適当な処理


    // リダイレクト先のURLをvalueStackに格納
    ValueStack valueStack = invocation.getStack();
    valueStack.set("redirectUrl",redirectUrl);

    return "redirect";
}

参考URL
http://yuroyoro.hatenablog.com/entry/20080414/1208140350
http://stackoverflow.com/questions/173846/struts2-how-to-do-dynamic-url-redirects
http://struts.apache.org/development/2.x/docs/parameters-in-configuration-results.html

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