文章出處

1、struts2攔截器interceptor的三種配置方法


方法1. 普通配置法

<struts> 
    <package name="struts2" extends="struts-default"> 
        <interceptors> 
            <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor> 
        </interceptors>

        <action name="register" class="edu.hust.action.RegisterAction"> 
            <result name="input">/register.jsp</result> 
            <result>/result.jsp</result> 
             
            <!-- 在自定義interceptor并將其ref時, 系統會覆蓋掉默認的interceptor-stack(defaultStack), 為了保證系統默認的defaultStack不受印象,
我們需要顯式的將其引入 --> <!-- 注意兩個interceptor-ref的順序, 順序不同, 執行效果也不同: 先配置的先執行/后配置的先退出(先進后出) --> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInterceptor"></interceptor-ref> </action> </package> </struts>

 

方法2. 配置攔截器棧(即將多個interceptor串聯的一種元素)。然后在<action>中引入該攔截器棧就可以了。

<struts> 
    <package name="struts2" extends="struts-default"> 
         
        <interceptors> 
            <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor> 
         
            <interceptor-stack name="myInterceptorStack"> 
                <interceptor-ref name="myInterceptor"></interceptor-ref> 
                <interceptor-ref name="defaultStack"></interceptor-ref> 
            </interceptor-stack> 
        </interceptors> 
         
        <action name="register" class="edu.hust.action.RegisterAction"> 
            <result name="input">/register.jsp</result> 
            <result>/result.jsp</result> 
             
            <interceptor-ref name="myInterceptorStack"></interceptor-ref> 
        </action> 
    </package> 
</struts>

 


方法3. 修改默認攔截器,將自定義的攔截器棧定義為struts2的默認攔截器。

<struts> 
    <package name="struts2" extends="struts-default"> 
         
        <interceptors> 
            <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor> 
            <interceptor-stack name="myInterceptorStack"> 
                <interceptor-ref name="myInterceptor"></interceptor-ref> 
                <interceptor-ref name="defaultStack"></interceptor-ref> 
            </interceptor-stack> 
        </interceptors>

        <!-- 此默認interceptor是針對所有action的 --> 
        <!-- 如果某個action中引入了interceptor, 則在這個action中此默認interceptor就會失效 --> 
        <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref> 
         
        <action name="register" class="edu.hust.action.RegisterAction"> 
            <result name="input">/register.jsp</result> 
            <result>/result.jsp</result> 
        </action> 
         
    </package> 
</struts>

 

2. Interceptor的角色對象

(1)攔截目標對象(被代理對象),這里目標對象就是action;

(2)攔截器(一個類,動態的將某些方法插入到目標對象的某方法的before、after);

(3)對目標對象生成的(動態)代理對象(代理對象內部方法綜合了目標對象方法+攔截器方法)。程序最終執行的是目標對象的代理,而這個代理已經插入了interceptor。

 

攔截器作用:攔截action。interceptor相當于一個入口和出口,通過interceptor進入action,執行完action的代碼再通過interceptor出去。

 

下面針對struts2 -- interceptor(Interceptor怎么寫) 這篇文章的前兩個例子進行配置,如下

<struts> 
    <package name="struts2" extends="struts-default"> 
         
        <interceptors> 
            <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor> 
            <interceptor name="myInterceptor2" class="edu.hust.interceptor.MyInterceptor2"></interceptor> 
            <interceptor-stack name="myInterceptorStack"> 
                <interceptor-ref name="myInterceptor"></interceptor-ref> 
                <interceptor-ref name="myInterceptor2"></interceptor-ref> 
                <interceptor-ref name="defaultStack"></interceptor-ref> 
            </interceptor-stack> 
        </interceptors> 
         
        <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref> 
         
        <action name="register" class="edu.hust.action.RegisterAction"> 
            <result name="input">/register.jsp</result> 
            <result>/result.jsp</result> 
        </action> 
         
    </package> 
</struts>

 
Console會得到以下打印輸出

intercept start
intercept2 start
2008-9-19 19:42:06 com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>
信息: Detected AnnotationActionValidatorManager, initializing it...
intercept2 finish
intercept finish

 

相關:Struts2的工作原理(圖解)詳解

攔截器

文章列表


不含病毒。www.avast.com
全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()