Struts 2が独自のインターセプターを作成
このチュートリアルでは、Struts 2で独自のインターセプターを作成する方法を示します。
要約手順:
-
com.opensymphony.xwork2.interceptor.Interceptorを実装するクラスを作成します。
-
intercept(ActionInvocation invocation)メソッドを実装します。
-
struts.xmlでインターセプターを構成します。
-
アクションにリンクします。
Struts 2 interceptors
Struts 2には多くの準備ができたインターセプターが付属しています。独自のインターセプターを作成する前に、available Struts 2 interceptorsのリストを確認してください。
独自のインターセプターを作成する完全な例:
1. アクション
ユーザー要求を転送してメッセージを印刷する単純なアクション。
HelloAction.java
package com.example.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.example.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; } }
Explanation
インターセプタークラスはcom.opensymphony.xwork2.interceptor.Interceptor interfaceを実装する必要があります。 インターセプターの初期化中に、init()が呼び出されます。インターセプターの破壊、destroy()が呼び出されます。 最後に、作業を行うすべてのインターセプターコードをintercept(ActionInvocation invocation)メソッド内に配置します。
invocation.invoke()
インターセプターintercept()メソッドで、must called the invocation.invoke()を実行し、その結果を返します。 これは、次のインターセプターまたはアクションを呼び出すためのメソッドです。 アクションは、invocation.invoke()メソッドを呼び出さずに続行できませんでした。
destroy() is not reliable
この方法は信頼できないため、destroy()内にコードを配置することはお勧めしません。 アプリケーションサーバーが強制的にシャットダウンされるか、コマンドによって強制終了されると、destroy()は呼び出されません。
3. struts.xml
struts.xmlファイルでインターセプターを構成します。
pages/hello.jsp
4. Demo
サーバーの初期化中に、インターセプター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経由でアクションにアクセスしている間:http://localhost:8080/Struts2Example/helloAction.action
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()...