Interceptor(インタセプタ)

Struts2 で action の処理の前後に別の処理を割り込ませることのできる仕組み。

struts.xml 設定

基本的なインタセプタを定義。

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 
   <package name="app-default" extends="struts-default">
 
   <!-- インタセプタ以外の設定いろいろ ここから -->
   <!-- インタセプタ以外の設定いろいろ ここまで -->
 
   <!-- デフォルトインタセプタの設定 ここから -->
   <interceptors>
      <interceptor-stack name="defaultInterceptorsStack">
         <!-- 不要なインタセプタは消しておくこと -->
         <interceptor-ref name="exception" />
         <interceptor-ref name="systemExceptionHandleInterceptor" />
         <interceptor-ref name="logicalExceptionHandlingInterceptor" />
         <interceptor-ref name="alias" />
         <interceptor-ref name="servletConfig" />
         <interceptor-ref name="i18n" />
         <interceptor-ref name="chain" />
         <interceptor-ref name="debugging" />
         <interceptor-ref name="profiling" />
         <interceptor-ref name="modelDriven" />
         <interceptor-ref name="fileUpload" />
         <interceptor-ref name="checkbox" />
         <interceptor-ref name="staticParams" />
         <interceptor-ref name="defaultParametersInterceptor">
            <param name="excludeParams">dojo\..*</param>
         </interceptor-ref>
         <interceptor-ref name="validation">
            <param name="excludeMethods">
               input,back,cancel,browse
            </param>
            <param name="validateAnnotatedMethodOnly">
               false
            </param>
         </interceptor-ref>
         <interceptor-ref name="validatorFailureFollowInterceptor" />
         <interceptor-ref name="workflow">
            <param name="excludeMethods">
               input,back,cancel,browse
            </param>
         </interceptor-ref>
      </interceptor-stack>
   </interceptors>
   <!-- デフォルトインタセプタの設定 ここまで -->
 
   <!-- デフォルトのインタセプタスタックを定義 -->
   <default-interceptor-ref name="defaultInterceptorsStack" />
 
   </package>
 
</struts>

インタセプタのクラスを作成

com.opensymphony.xwork2.interceptor.Interceptor を implements したクラスをつくる。

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 
 
 
 
 
 
 
-
|
-
|
!
-
-
!
 
-
|
!
-
-
!
|
-
 
!
-
 
-
!
 
!
|
!
package foo;
 
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
 
@SuppressWarnings("serial")
public class BeforeInterceptor implements Interceptor {
 
    /**
	 *	初期化。
	 */
    public void init() {
        // 特に処理は不要。
    }
 
    /**
	 *	終了。
	 */
    public void destroy() {
        // 特に処理は不要。
    }
 
    /**
	 *	割り込み処理を実装。
	 */
    public String intercept(ActionInvocation invocation) throws Exception {
        
        // ここにアクション前後に割り込ませたい処理を書く。
        
        return invocation.invoke();
    }
 
}

インタセプタのアクションを定義

アクションの前後に割り込ませたいインタセプタを定義。 インタセプタというのは、要は、Webで呼び出されるアクションの前後に必ず追従して呼び出される別のアクションと考えれば良い。ので、定義の仕方もアクションと同じ。

この定義は struts.xml に書いても良いけど、ここは別ファイルに定義して include することにする。

foo.xml

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 
   <package name="foo" namespace="/foo" extends="app-default">
 
   <interceptors>
      <!-- カスタムインタセプタの定義 -->
      <interceptor name="beforeInterceptor"
         class="BeforeInterceptor" />
 
      <!-- 呼び出しスタックの定義 -->
      <interceptor-stack name="fooStack">
         <!-- 以下に書いた順番で呼び出されていく -->
         <interceptor-ref name="beforeInterceptor"/>
         <interceptor-ref name="defaultInterceptorsStack" />
      </interceptor-stack>
 
   </interceptors>
 
   <!-- アクションの定義 -->
   <action name="loginHogeAction" method="login" class="hogeAction">
      <!-- ここにアクションの処理前に割り込ませたいスタックを指定 -->
      <interceptor-ref name="fooStack"/>
      <!-- アクションの設定 -->
      <result name="input" >/WEB-INF/foo/login.vm</result>
      <result name="success" >/WEB-INF/foo/loginOk.vm</result>
      <!-- ここにアクションの処理後に割り込ませたいスタックを指定 -->
   </action>
 
   </package>
 
</struts>

このファイルを struts.xml に include しておく。

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 
 
-
!
 
 
 
 
 
 
 
 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
   ...
 
   <include file="/foo.xml" />
 
   ...
</struts>

これでインタセプタの処理がアクションの前(後)に必ず実行されるようになる。

参考


MLEXP. Wiki


トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-05-15 (土) 12:07:34 (5088d)