Struts 2 對於 Interceptor 攔截器的基本概念與實作 的瞭解後,我們現在來作一下 Interceptor 在 Struts 2 上的基本運用。

首先建立一個 class : com.test.interceptor.MyInterceptor 並且 implements Interceptor,這個 Interceptor 是個 Interface,所以需要實作三個 method:init()、destroy()、intercept(ActionInvocation invocation),其中只有 intercept(ActionInvocation invocation) 需要回傳一個 String 的值,可以利用  invocation.invoke(); 來讓系統自已跳到預設的下一個 Action method。

 

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

public class MyInterceptor implements Interceptor {

    @Override
    public void destroy() {
        System.out.println("MyInterceptor destroy()");

    }

    @Override
    public void init() {
        System.out.println("MyInterceptor Init()");
        
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("MyInterceptor:"+invocation.invoke());
        
        return invocation.invoke();
    }

}

 

再來對 struts.xml 加入自建的 interceptor,在 <interceptors>...</interceptors> 中是定義我們自建的 Interceptor ,讓 Struts 知道有這一個 Interceptor (攔截器),然後再對要攔截的Action name 作設定。

要注意的是,因為我們的 <package name="test" 是 extends="struts-default" ,而 struts-default.xml (在struts2-core-2.x.x.jar 裡預設的) 的預設 Interceptor (攔截器) 是defaultStack,若沒加入它,則系統會停在我們自建的 Interceptor (攔截器),而不會繼續往下執行 validate() -> execute()...等相關的 method()。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="test" namespace="/test" extends="struts-default">

        <interceptors>
            <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor>       

        </interceptors>

        
        <!-- Spring Testing -->
        <action name="userLogin_*" method="{1}" class="testUserLogin">
            <result name="input">userLogin.jsp</result>
            <result name="success">eip.jsp</result>
       
            <interceptor-ref name="myInterceptor"/>
            <interceptor-ref name="defaultStack"/>
       
        </action>  

        <!-- Add additional "example" package actions here. -->

    </package>
</struts>

 

上面的設定,也可以用 Interceptor Stock 來達成,如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="test" namespace="/test" extends="struts-default">

        <interceptors>
            <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor>
        
            <interceptor-stack name="testStock">
                <interceptor-ref name="myInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>      
        
        <!-- Spring Testing -->
        <action name="userLogin_*" method="{1}" class="testUserLogin">
            <result name="input">userLogin.jsp</result>
            <result name="success">eip.jsp</result>  
            
            <interceptor-ref name=name="testStock">
        </action>  

        <!-- Add additional "example" package actions here. -->

    </package>
</struts>

 

或者是利用 default-interceptor-ref 來對全部的 Action Name 作 Interceptor (攔截器),如下:

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="test" namespace="/test" extends="struts-default">

        <interceptors>
            <interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor"></interceptor>
        
            <interceptor-stack name="testStock">
                <interceptor-ref name="myInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="testStock"></default-interceptor-ref>
        
        <!-- Spring Testing -->
        <action name="userLogin_*" method="{1}" class="testUserLogin">
            <result name="input">userLogin.jsp</result>
            <result name="success">eip.jsp</result>              
            
        </action>  

        <!-- Add additional "example" package actions here. -->

    </package>
</struts>

 

參考:

http://struts.apache.org/maven/index.html

http://struts.apache.org/maven/xwork-core/apidocs/index.html

arrow
arrow
    文章標籤
    struts2 interceptor
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()