それをダウンロードする –

Struts2-Create-Own-Interceptor-Example.zip

このチュートリアルでは、Struts 2で独自のインターセプタを作成する方法を示します。

要約手順:

  1. クラスの実装を作成する


  2. intercept(ActionInvocation呼び出し)

    メソッドを実装します.


  3. struts.xml

    にインターセプタを設定します.

  4. アクションにリンクしてください.

    • Struts 2インターセプタ** Struts 2には多くのレディセプタが用意されています。作成する前にhttp://struts.apache.org/2.0.14/docs/interceptors.html[available Struts 2インターセプタ]のリストを確認してください独自のインターセプタ。

独自のインターセプタを作成するための完全な例:

1.アクション

ユーザーの要求を転送してメッセージを印刷する単純なアクション。

  • HelloAction.java **

package com.mkyong.common.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

    public String execute() throws Exception {

        System.out.println("HelloAction execute() is called");
        return SUCCESS;

    }
}

2.インターセプタ

完全なインターセプタの例。

  • PrintMsgInterceptor.java **

package com.mkyong.common.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PrintMsgInterceptor implements Interceptor{

       //called during interceptor destruction
    public void destroy() {
        System.out.println("CustomInterceptor destroy() is called...");
    }

   //called during interceptor initialization
    public void init() {
        System.out.println("CustomInterceptor init() is called...");
    }

   //put interceptor code here
    public String intercept(ActionInvocation invocation) throws Exception {

        System.out.println("CustomInterceptor, before invocation.invoke()...");

        String result = invocation.invoke();

        System.out.println("CustomInterceptor, after invocation.invoke()...");

        return result;
    }

}

  • 説明

    インターセプタクラスは、

    com.opensymphony.xwork2.interceptor.Interceptorインタフェース

    を実装する必要があります。インターセプタの初期化中に、

    init()

    が呼び出されます。インターセプタ破壊、

    destroy()

    が呼び出されます。最後に、

    intercept(ActionInvocation呼び出し)** メソッドの内部で作業を行うすべてのインターセプタコードを配置します。

  • invocation.invoke()

    インターセプタのintercept()メソッドでは、invocation.invoke()

    を呼び出してその結果を返す必要があります。これは、次のインターセプタまたはアクションを呼び出すためのメソッドです。

    invocation.invoke()

    メソッドを呼び出さなければ、アクションは続行されません。

  • destroy()は信頼できない

    このメソッドは信頼できないため、

    destroy()

    の内部にコードを置くことはお勧めしません。アプリケーションサーバーが強制的にシャットダウンされたり、コマンドでkillされたりすると、

    destroy()** は呼び出されません。

3. struts.xml

  • struts.xml ** ファイルでインターセプタを設定します。

<?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="default" namespace="/" extends="struts-default">

    <interceptors>
    <interceptor name="printMsgInterceptor"
    class="com.mkyong.common.interceptor.PrintMsgInterceptor"></interceptor>

         <interceptor-stack name="newStack">
            <interceptor-ref name="printMsgInterceptor"/>
        <interceptor-ref name="defaultStack"/>
          </interceptor-stack>

    </interceptors>

    <action name="helloAction"
    class="com.mkyong.common.action.HelloAction" >
    <interceptor-ref name="newStack"/>
    <result name="success">pages/hello.jsp</result>
     </action>

   </package>
</struts>

4.デモ

サーバーの初期化中に、インターセプター

init()

メソッドが呼び出されます。

INFO: Overriding property struts.i18n.reload - old value: false new value: true
15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Overriding property struts.configuration.xml.reload - old value: false new value: true

CustomInterceptor init() is called...

15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
15 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on/0.0.0.0:8009
15 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/20  config=null
15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 994 ms

URLを介してアクションにアクセスしている間:

INFO: Overriding property struts.i18n.reload - old value: false new value: true
15 Julai 2010 11:37:42 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Overriding property struts.configuration.xml.reload - old value: false new value: true

CustomInterceptor init() is called...

15 Julai 2010 11:37:42 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
15 Julai 2010 11:37:42 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on/0.0.0.0:8009
15 Julai 2010 11:37:42 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/20  config=null
15 Julai 2010 11:37:42 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 994 ms

CustomInterceptor, before invocation.invoke()...
HelloAction execute() is called
CustomInterceptor, after invocation.invoke()...

リファレンス

インターセプタのドキュメント]。

http://struts.apache.org/2.0.14/docs/building-your-own-interceptor.html

[Struts

2独自のインターセプターを構築する]