本文轉載自:http://www.blogjava.net/zzzlyr/archive/2009/10/12/297998.html
1: 所有攔截器的超級接口 Interceptor ,Action 去實現這個接口 ;
Interceptor 它其中有三個方法 (init(),destroy() ,interceptor()):
Init() 方法 : 在服務器起動的時候加載一次 , 並且只加載一次 ;
Destroy() 方法 : 當攔截器銷燬時執行的方法 ;
Interceptor() 方法 : 其中裏邊有一個參數 invocation
public String intercept(ActionInvocation invocation) throws xception {
System. out .println( "interceptor!!" );
String result=invocation.invoke();
return result;
}
Invocation.invoke() 是如果只有一個攔截器執行完這個方法後 , 會返回給視圖 , 如果有多個攔截器 , 它順序的執行完所有的攔截器 , 才返回給視圖 .
2: 可以在系統初始化中給攔截器指定默認的參數 ( 也包括了定義攔截器方式 ) 如下 :
在攔截器類中把 hello 當做屬性 set/get 方式注入到攔截器類中 ;
< interceptors >
<!-- 先定義攔截器 -->
< interceptor name = "myInterceptor" class = "com.zzz.struts2.interceptor.MyInterceptor" >
<!-- 指定系統初始化給攔截器的參數 -->
< param name = "hello" > 張釗釗 </ param >
</ interceptor >
<!-- 加到自己設置的攔截器棧裏邊去 -->
< interceptor-stack name = "myStack" >
< interceptor-ref name = "myInterceptor" >
</ interceptor-ref >
< interceptor-ref name = "defaultStack" ></ interceptor-ref >
</ interceptor-stack >
</ interceptors >
<!-- 改變系統默認的攔截器 , 改成自己的默認攔截器 , 並且一個系統只能有一個默認的攔截器 , 這樣這個攔截器棧會默認應用到所有的 Action 上去 -->
< default-interceptor-ref name = "myStack" >
</ default-interceptor-ref >
也可以在使用攔截器的時候給它設置參數 :
就是在一個 action 的 reslut 下面配置上如下 :
< action name = "register"
class = "com.zzz.struts2.action.RegisterAction" >
< result name = "success" > /success.jsp </ result >
<!-- result 它其中還有一個信息轉發類型 type="" 記住,如果不轉向 JSP ,轉向圖表,可以改變 type="" 值 -->
< result name = "input" > /register.jsp </ result >
< interceptor-ref name = "myInterceptor" >
< param name = "hello" > welcome </ param >
</ interceptor-ref >
< interceptor-ref name = "myStack" ></ interceptor-ref >
</ action >
2. 攔截器 , 攔截器棧和默認的攔截器之間的關係
1: 攔截器和攔截器棧是一個級別的 , 也就是說一個攔截器棧中包括許多攔截器 , 一個攔截器棧中還可以包括許多攔截器棧 , 配置如下方式 :
< interceptors >
<!-- 先定義攔截器 -->
< interceptor name = "myInterceptor" class = "com.zzz.struts2.interceptor.MyInterceptor" >
<!-- 指定系統初始化給攔截器的參數 -->
< param name = "hello" > 張釗釗 </ param >
</ interceptor >
<!-- 加到自己設置的攔截器棧裏邊去 -->
< interceptor-stack name = "myStack" >
< interceptor-ref name = "myInterceptor" >
</ interceptor-ref >
< interceptor-ref name = "defaultStack" ></ interceptor-ref >
</ interceptor-stack >
</ interceptors >
攔截器的使用 :1. 先定義 ;2. 在引用使用 ;
< interceptor name = "myInterceptor" class = "com.zzz.struts2.interceptor.MyInterceptor" >
< interceptor-ref name = "myInterceptor" >
</ interceptor-ref >
2:struts2 中有一個系統默認的攔截器棧是 defaultStack, 如果你手動引用自己的攔截器 , 系統默認的攔截器棧將不起作用 ; 這樣必需手動引入系統的攔截器棧 < interceptor-ref name = "defaultStack" >
</ interceptor-ref >
如果想改變系統默認的攔截器棧 , 可以這樣配置 :
< default-interceptor-ref name = "myStack" >
</ default-interceptor-ref > 其中 myStack 是自己定義的攔截器棧名字 ;
如果攔截器棧中有多個攔截器 , 在執行 action 之前的順序跟配置攔截器的順序一致 , 而在 action 之後執行的順序是相反的 ;
3: 抽象的攔截器類 AbstractInterceptor
1:Interceptor 這個超級攔截器接口 , 有三方法需要實現 , 但是如果不想使用 init();
和 destroy() 方法 , 可以去繼承這個抽象攔截器類 ;
它的使用跟上邊的沒有什麼區別 ;
4: 方法過濾攔截器 MethodFilterInterceptor
1: 上邊的攔截器都要是針對整個 action 的 , 如果針對某個方法進行攔截可以去繼承這個類 ;
它的使用跟上邊的使用方法差不多 , 只是需要要配置它對那個方法進行攔截 , 方法過濾攔截器最好不要配置到自己設置默認的攔截器棧裏邊 , 自己手動配置 .
interceptor-ref name = "myInterceptor3" >
< param name = "includeMethods" > execute </ param >
< param name = "excludeMethods" > execute </ param >
</ interceptor-ref >
< interceptor-ref name = "defaultStack" ></ interceptor-ref >
其中 includeMethods ,excludeMethods 是固定寫法 : includeMethods 包含攔截那些方法 , 多個方法需要用 ”,” 隔開 ; excludeMehtods 是排除攔截的那些方法 ;
5: 鑑聽器 PreResultListener 接口
1: 它的鑑聽點在攔截器執行完某個 action 方法後 , 在渲染視圖之前做一些事情;讓某個類去實現這個接口;
然後向需要它的攔截器中註冊進去如下代碼:
public class MyInterceptor3 extends MethodFilterInterceptor {
private static final long serialVersionUID = 3756655410194005443L;
@Override
protected String doIntercept(ActionInvocation invocation ) throws Exception {
// 把鑑聽器註冊到攔截中去 ;
invocation .addPreResultListener( new MyListener());
System. out .println( "my Interceptor3" );
String result=arg0.invoke();
System. out .println( "my interceptor3 finshed!" );
return result;
}
}