1.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怎么寫)"這篇文章的MyInterceptor.class和MyInterceptor2.class。根據下面的配置文件執行程序
<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
這個結果解釋了"interceptor相當于一個入口和出口,通過interceptor進入action,執行完action的代碼再通過interceptor出去"這句話。
3. extends MethodFilterInterceptor的攔截器如何配置哪些方法該攔截、哪些方法不該攔截(針對方法攔截的配置)
<struts>
<package name="struts2" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor3" class="edu.hust.interceptor.MyInterceptor3"></interceptor>
</interceptors>
<action name="register" class="edu.hust.action.RegisterAction" method="queryAll">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
<!-- myInterceptor3攔截器只對RegisterAction中的queryAll()方法和insert()方法進行了攔截, 其他方法未進行攔截 -->
<interceptor-ref name="myInterceptor3">
<param name="includeMethods">queryAll, execute</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="register" class="edu.hust.action.RegisterAction" method="insert">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
<interceptor-ref name="myInterceptor3">
<param name="includeMethods">queryAll, insert</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="register" class="edu.hust.action.RegisterAction" method="update">
<result name="input">/register.jsp</result>
<result>/result.jsp</result>
<interceptor-ref name="myInterceptor3">
<param name="includeMethods">queryAll, insert</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/ForWayfarer/archive/2008/09/20/2955586.aspx