什麼是攔截器html
攔截器(Interceptor)是Struts 2的一個強有力的工具,有許多功能都是構建於它之上,如國際化(前兩篇博客介紹過)、轉換器,校驗等。java
攔截器是動態攔截Action調用的對象。它提供了一種機制可使開發者能夠定義在一個action執行的先後執行的代碼,也能夠在一個action執行前阻止其執行。同時也是提供了一種能夠提取action中可重用的部分的方式。apache
說到攔截器有一個東西不 能落下——攔截器鏈(Interceptor Chain,在Struts 2中稱爲攔截器棧Interceptor Stack)。攔截器鏈就是將攔截器按必定的順序聯結成一條鏈。在訪問被攔截的方法或字段時,攔截器鏈中的攔截器就會按其以前定義的順序被調用。cookie
實現原理session
Struts 2的攔截器實現相對簡單。當請求到達Struts 2的ServletDispatcher時,Struts 2會查找配置文件,並根據其配置實例化相對的攔截器對象,而後串成一個列表(list),最後一個一個地調用列表中的攔截器。
app
Struts 2提供了豐富多樣的,功能齊全的攔截器實現。你們能夠到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看關於默認的攔截器與攔截器鏈的配置。
jsp
相關說明以下:ide
攔截器工具 |
名字字體 |
說明 |
Alias Interceptor |
alias |
在不一樣請求之間將請求參數在不一樣名字件轉換,請求內容不變 |
Chaining Interceptor |
chain |
讓前一個Action的屬性能夠被後一個Action訪問,如今和chain類型的result()結合使用。 |
Checkbox Interceptor |
checkbox |
添加了checkbox自動處理代碼,將沒有選中的checkbox的內容設定爲false,而html默認狀況下不提交沒有選中的checkbox。 |
Cookies Interceptor |
cookies |
使用配置的name,value來是指cookies |
Conversion Error Interceptor |
conversionError |
將錯誤從ActionContext中添加到Action的屬性字段中。 |
Create Session Interceptor |
createSession |
自動的建立HttpSession,用來爲須要使用到HttpSession的攔截器服務。 |
Debugging Interceptor |
debugging |
提供不一樣的調試用的頁面來展示內部的數據情況。 |
Execute and Wait Interceptor |
execAndWait |
在後臺執行Action,同時將用戶帶到一箇中間的等待頁面。 |
Exception Interceptor |
exception |
將異常定位到一個畫面 |
File Upload Interceptor |
fileUpload |
提供文件上傳功能 |
I18n Interceptor |
i18n |
記錄用戶選擇的locale |
Logger Interceptor |
logger |
輸出Action的名字 |
Message Store Interceptor |
store |
存儲或者訪問實現ValidationAware接口的Action類出現的消息,錯誤,字段錯誤等。 |
Model Driven Interceptor |
model-driven |
若是一個類實現了ModelDriven,將getModel獲得的結果放在Value Stack中。 |
Scoped Model Driven |
scoped-model-driven |
若是一個Action實現了ScopedModelDriven,則這個攔截器會從相應的Scope中取出model調用Action的setModel方法將其放入Action內部。 |
Parameters Interceptor |
params |
將請求中的參數設置到Action中去。 |
Prepare Interceptor |
prepare |
若是Acton實現了Preparable,則該攔截器調用Action類的prepare方法。 |
Scope Interceptor |
scope |
將Action狀態存入session和application的簡單方法。 |
Servlet Config Interceptor |
servletConfig |
提供訪問HttpServletRequest和HttpServletResponse的方法,以Map的方式訪問。 |
Static Parameters Interceptor |
staticParams |
從struts.xml文件中將中的中的內容設置到對應的Action中。 |
Roles Interceptor |
roles |
肯定用戶是否具備JAAS指定的Role,不然不予執行。 |
Timer Interceptor |
timer |
輸出Action執行的時間 |
Token Interceptor |
token |
經過Token來避免雙擊 |
Token Session Interceptor |
tokenSession |
和Token Interceptor同樣,不過雙擊的時候把請求的數據存儲在Session中 |
Validation Interceptor |
validation |
使用action-validation.xml文件中定義的內容校驗提交的數據。 |
Workflow Interceptor |
workflow |
調用Action的validate方法,一旦有錯誤返回,從新定位到INPUT畫面 |
Parameter Filter Interceptor |
N/A |
從參數列表中刪除沒必要要的參數 |
Profiling Interceptor |
profiling |
經過參數激活profile |
在struts.xml中添加以下配置:
<!-- 配置全局攔截器 -->
<package name="all" extends="struts-default">
<interceptors>
<!-- 定義權限控制攔截器 -->
<interceptor name="authority"
class="akai.cost.ms.base.AuthInterceptor" />
<!-- 定義一個包含權限控制的攔截器棧 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<!-- 定義默認攔截器 -->
<default-interceptor-ref name="mydefault" />
<!-- 定義全局處理結果 -->
<global-results>
<!-- 邏輯名爲login的結果,映射到/login.jsp頁面 -->
<result name="login">/login.jsp</result>
</global-results>
</package>
使用方法:其餘包繼承這個包名就能夠了
<package name="abc" extends="all" namespace="/">
附:攔截器類
annoction註解中使用攔截器和攔截棧
1 //直接在類名稱的上端寫入便可,value中指定要引入的攔截器的名稱便可 2 @InterceptorRef(value="token") 3 //攔截棧的引用,藍色字體即攔截棧的引用名稱 4 @InterceptorRefs(@InterceptorRef("sessionCheckStack"))