?????Struts2的核心在于它復雜的攔截器,幾乎70%的工作都是由攔截器完成的。比如我們之前用于將上傳的文件對應于action實例中的三個屬性的fileUpload攔截器,還有用于將表單頁面的http請求參數設置成action中對應的屬性的param攔截器等。總之,在整個Struts框架中攔截器的作用是相當大的,本篇將從以下幾點詳細介紹下有關Struts攔截器的內容:
- 攔截器在Struts中的作用
- 自定義攔截器實現類
- 配置攔截器(包含配置默認攔截器)
- 引用攔截器
- 配置攔截指定方法的攔截器
- 攔截器的攔截順序
一、攔截器在Struts中的作用
?????在我們的web.xml中,我們配置了一個過濾器,實現將所有請求交付StrutsPrepareAndExecuteFilter類。一旦接受到任意action的請求,該類會創建和初始化一個ActionProxy實例,它代理了具體的action,在其中我們可以添加任意攔截器在execute方法執行之前和之后做一些額外的操作,最終會調用該action實例的execute方法,為用戶返回視圖結果字符串,然后系統會根據該視圖結果字符串調取相應的視圖頁面。下圖是攔截器和action之間的關系:
這是一種典型的AOP思想,當我們在Struts.xml中定義一個包的時候,大部分情況下我們會繼承struts-default文件,所以雖然我們在自己的配置文件中并沒有手動配置任何的攔截器,但是我們創建的action卻被很多攔截器攔截處理,就是因為struts-default中配置的攔截器生效了。Struts中內建了很多的攔截器,他們大多被配置在struts-default文件中,詳細的內建攔截器的介紹可以參考官方API,接下來我們看如何自定義一個攔截器。
二、自定義攔截器實現類
?????想要實現自己的攔截器類只需要實現 com.opensymphony.xwork2.interceptor.Interceptor.Interceptor 接口即可,該接口中有如下幾個方法:
public abstract void destroy();
public abstract void init();
public abstract String intercept(ActionInvocation paramActionInvocation)
throws Exception;
init 方法在執行攔截方法之前回調,主要用于初始化一些資源,destroy 與init 方法對應,在攔截器實例被銷毀之前回調,主要用于釋放在init 方法中打開的資源。intercept 方法是我們的攔截方法,我們可以重寫該方法來完成對action實例的攔截,該方法具有一個ActionInvocation 類型的參數,該參數內部引用了具體的action實例對象(如果該action還有其他攔截器的話),我們可以調用該參數的invoke方法調用具體action實例的execute方法或者調用下一個攔截器,intercept方法返回一個String 類型的字符串代表了具體視圖頁面。下面看個具體的例子:
public class TestAction extends ActionSupport {
public String execute(){
System.out.println("執行execute方法......");
return SUCCESS;
}
}
public class MyIntercept implements Interceptor {
public void init() {}
public void destroy() {}
public String intercept(ActionInvocation action) throws Exception{
System.out.println("攔截action開始.......");
String result = action.invoke();
System.out.println("攔截action結束.......");
return result;
}
}
省略了配置攔截器和TestAction 的代碼,下圖是上述程序運行的結果截圖:
三、配置和引用攔截器
?????上述的示例定義了一個簡單的攔截器實現類,我們省略了在struts.xml中配置和引用該攔截器的代碼,本小節將詳細的介紹如何在struts.xml中定義和引用我們自定義實現的攔截器類。
從struts-default.xml中我們可以看出來,我們使用<interceptors>元素定義攔截器name和物理位置的配對,例如:
<interceptors>
<interceptor name="test" class="MyPackage.TestAction"/>
......
......
</interceptors>
上述代碼定義了一個攔截器test,它對應于具體的一個class。需要注意的是,定義攔截器的元素 interceptors 及其子元素必須被配置在某個package包下。
以上只是定義了一個攔截器和具體攔截器實現類之間的映射關系,但是想要實現對某個具體的action的攔截需要使用元素<interceptor-ref>根據name屬性值引用一個上述已經定義了的攔截器。例如:
<action name="test" class="MyPackage.TestAction">
<interceptor-ref name="test"/>
<result name="success">/index.jsp</result>
......
......
</action>
正如上述代碼展示的一樣,該元素用于引用一個已經定義好了的攔截器,并且該元素出現在具體的action內部,表明了該action具有一個test攔截器。以上代碼實現了對單個攔截器的定義和引用,其實對于攔截器棧(一堆攔截器的組合)來說配置也是類似的。定義一個攔截器棧的代碼是如下的:
<interceptor-stack name="攔截器棧名">
interceptor-ref name="攔截器一"/>
interceptor-ref name="攔截器二"/>
interceptor-ref name="攔截器三"/>
.....
</interceptor-stack>
引用一個攔截器棧就沒什么區別了:
interceptor-ref name="攔截器棧名"/>
當然我們也可以通過
<default-interceptor-ref name="攔截器名"/>
配置默認攔截器或者攔截器棧,如果該包下某個action沒有顯式指定攔截器,那么就會調用該默認攔截器,否則如果顯式配置了攔截器,那么默認攔截器將會失效。
四、為Action中指定方法配置攔截器
?????在默認情況下,我們為action配置了攔截器之后,該攔截器將會攔截該action中所有的方法,這有時候會給我們帶來麻煩,當然struts為我們提供API用來針對具體的某個方法配置攔截器。這里涉及到一個抽象類:MethodFilterInterceptor。該類實際上實現了Interceptor并完成了一些默認實現,我們簡單看看其中的代碼:
public abstract class MethodFilterInterceptor
extends AbstractInterceptor
{
//該set集合保存了該攔截器不需要攔截的所有方法
protected Set<String> excludeMethods = Collections.emptySet();
//該set集合保存了所有該攔截器需要攔截的方法
protected Set<String> includeMethods = Collections.emptySet();
//省略getter,setter方法
//用于攔截action的入口
public String intercept(ActionInvocation invocation)
throws Exception
{
if (applyInterceptor(invocation)) {
return doIntercept(invocation);
}
return invocation.invoke();
}
//判斷當前需要調用的action處理邏輯方法是否需要被此攔截器攔截
protected boolean applyInterceptor(ActionInvocation invocation)
{
String method = invocation.getProxy().getMethod();
boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(this.excludeMethods, this.includeMethods, method);
if ((this.log.isDebugEnabled()) &&
(!applyMethod)) {
this.log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.", new String[0]);
}
return applyMethod;
}
//這是需要我們重寫的方法,具體作用下文介紹
protected abstract String doIntercept(ActionInvocation paramActionInvocation)
throws Exception;
}
從上述代碼中可以看出,該抽象類實現了Interceptor接口并完成了基本的實現。除此之外,該類提供了兩個集合用于保存該攔截器需要攔截的所有方法和不需要攔截的所有方法,攔截器入口intercept中會首先判斷此次請求action實例中的邏輯處理方法是否需要被該攔截器攔截,如果需要被攔截,那么將會調用doIntercept我們自己實現的攔截器邏輯。否則直接調用invoke方法執行處理邏輯。所以一般來說,我們只需要重寫doIntercept方法完成攔截器的核心處理即可。
當然此處需要注意一點的是,用于判斷當前請求的處理邏輯方法是否需要被該攔截器攔截的方法applyInterceptor是在intercept中進行校驗的,也就是說在執行doIntercept方法之前excludeMethods和includeMethods的值應當是已經初始化完畢了的。所以我們在doIntercept中再次為這兩個屬性賦值是沒用的,因為已經完成了校驗。一般我們在struts.xml中為這兩個屬性賦值,因為該配置文件是先被加載的。下面我們看個實例:
//自定義一個攔截器
public class MyIntercept extends MethodFilterInterceptor {
protected String doIntercept(ActionInvocation action)
throws Exception{
System.out.println("攔截開始......");
String result = action.invoke();
System.out.println("攔截結束......");
return result;
}
}
//引用該攔截器并指定不需要攔截的方法
<action name="test" class="MyPackage.TestAction">
<interceptor-ref name="test">
<param name="excludeMethods">execute</param>
</interceptor-ref>
<result name="success">/index.jsp</result>
</action>
下面我們看運行的結果截圖:
顯然我們指明了該攔截器不用攔截方法execute,當然結果顯示的也是如我們所愿。如果我們修改上述struts.xml中內容:
<action name="test" class="MyPackage.TestAction">
<interceptor-ref name="test">
<param name="includeMethods">execute</param>
</interceptor-ref>
<result name="success">/index.jsp</result>
</action>
我們指定該execute方法是需要被攔截器攔截的,下面運行的結果截圖:
當然如果需要指定多個方法需要被攔截或者不用被攔截,可以使用英文逗號隔開這些方法,例如:
<param name="includeMethods">方法一,方法二,方法三</param>
最后還有一點是:如果一個方法既被放在了includeMethods中也被放在了excludeMethods中,那么框架將會選擇攔截該方法。
五、有關攔截器機制的其他一些細節
?????攔截器的執行順序是按照引用攔截器的順序決定的,例如我們定義兩個攔截器:
<action name="test" class="MyPackage.TestAction">
<interceptor-ref name="test"/>
<interceptor-ref name="test2"/>
<result name="success">/index.jsp</result>
</action>
也就是說第一個攔截器攔截action之后,會調用invoke方法,如果還有其他攔截器則會調用下一個攔截器,一層層嵌套,最后結束最外層的攔截器。
上述實例中我們使用param參數為攔截器類中的includeMethods屬性賦值,但是如果是一個攔截器棧中我們有該如何為其中某個具體的攔截器屬性賦值呢?
<interceptor-ref name="攔截器棧">
<param name="攔截器一.屬性名">屬性值</param>
</interceptor-ref>
至此,我們簡單了解了有關struts2中攔截器器的相關知識,如需深刻理解還要在具體項目中體會,總結不到之處,望海涵!