淺談Struts2攔截器的原理與實(shí)現(xiàn)
一、理解Struts2攔截器
1. Struts2攔截器是在訪問(wèn)某個(gè)Action或Action的某個(gè)方法,字段之前或之后實(shí)施攔截,并且Struts2攔截器是可插拔的,攔截器是AOP的一種實(shí)現(xiàn).
2. 攔截器棧(Interceptor Stack)。Struts2攔截器棧就是將攔截器按一定的順序聯(lián)結(jié)成一條鏈。在訪問(wèn)被攔截的方法或字段時(shí),Struts2攔截器鏈中的攔截器就會(huì)按其之前定義的順序被調(diào)用。
二、實(shí)現(xiàn)Struts2攔截器原理
Struts2攔截器的實(shí)現(xiàn)原理相對(duì)簡(jiǎn)單,當(dāng)請(qǐng)求struts2的action時(shí),Struts 2會(huì)查找配置文件,并根據(jù)其配置實(shí)例化相對(duì)的 攔截器對(duì)象,然后串成一個(gè)列表,最后一個(gè)一個(gè)地調(diào)用列表中的攔截器
三、定義Struts2攔截器。
Struts2規(guī)定用戶自定義攔截器必須實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.Interceptor接口。該接口聲明了3個(gè)方法,
|
其中,init和destroy方法會(huì)在程序開始和結(jié)束時(shí)各執(zhí)行一遍,不管使用了該攔截器與否,只要在struts.xml中聲明了該Struts2攔截器就會(huì)被執(zhí)行。
intercept方法就是攔截的主體了,每次攔截器生效時(shí)都會(huì)執(zhí)行其中的邏輯。
不過(guò),struts中又提供了幾個(gè)抽象類來(lái)簡(jiǎn)化這一步驟。
public abstract class AbstractInterceptor implements Interceptor; |
都是模板方法實(shí)現(xiàn)的。
其中AbstractInterceptor提供了init()和destroy()的空實(shí)現(xiàn),使用時(shí)只需要覆蓋intercept()方法;
而MethodFilterInterceptor則提供了includeMethods和excludeMethods兩個(gè)屬性,用來(lái)過(guò)濾執(zhí)行該過(guò)濾器的action的方法??梢酝ㄟ^(guò)param來(lái)加入或者排除需要過(guò)濾的方法。
一般來(lái)說(shuō),攔截器的寫法都差不多??聪旅娴氖纠?/P>
package interceptor; |
四、配置Struts2攔截器
Struts2攔截器需要在struts.xml中聲明,如下struts.xml配置文件
攔截器 |
名字 |
說(shuō)明 |
Alias Interceptor |
alias |
在不同請(qǐng)求之間將請(qǐng)求參數(shù)在不同名字件轉(zhuǎn)換,請(qǐng)求內(nèi)容不變 |
Chaining Interceptor |
chain |
讓前一個(gè)Action的屬性可以被后一個(gè)Action訪問(wèn),現(xiàn)在和chain類型的result( |
Checkbox Interceptor |
checkbox |
添加了checkbox自動(dòng)處理代碼,將沒(méi)有選中的checkbox的內(nèi)容設(shè)定為false,而html默認(rèn)情況下不提交沒(méi)有選中的checkbox。 |
Cookies Interceptor |
cookies |
使用配置的name,value來(lái)是指cookies |
Conversion Error Interceptor |
conversionError |
將錯(cuò)誤從ActionContext中添加到Action的屬性字段中。 |
Create Session Interceptor |
createSession |
自動(dòng)的創(chuàng)建HttpSession,用來(lái)為需要使用到HttpSession的攔截器服務(wù)。 |
Debugging Interceptor |
debugging |
提供不同的調(diào)試用的頁(yè)面來(lái)展現(xiàn)內(nèi)部的數(shù)據(jù)狀況。 |
Execute and Wait Interceptor |
execAndWait |
在后臺(tái)執(zhí)行Action,同時(shí)將用戶帶到一個(gè)中間的等待頁(yè)面。 |
Exception Interceptor |
exception |
將異常定位到一個(gè)畫面 |
File Upload Interceptor |
fileUpload |
提供文件上傳功能 |
I18n Interceptor |
i18n |
記錄用戶選擇的locale |
Logger Interceptor |
logger |
輸出Action的名字 |
Message Store Interceptor |
store |
存儲(chǔ)或者訪問(wèn)實(shí)現(xiàn)ValidationAware接口的Action類出現(xiàn)的消息,錯(cuò)誤,字段錯(cuò)誤等。 |
Model Driven Interceptor |
model-driven |
如果一個(gè)類實(shí)現(xiàn)了ModelDriven,將getModel得到的結(jié)果放在Value Stack中。 |
Scoped Model Driven |
scoped-model-driven |
如果一個(gè)Action實(shí)現(xiàn)了ScopedModelDriven,則這個(gè)攔截器會(huì)從相應(yīng)的Scope中取出model調(diào)用Action的setModel方法將其放入Action內(nèi)部。 |
Parameters Interceptor |
params |
將請(qǐng)求中的參數(shù)設(shè)置到Action中去。 |
Prepare Interceptor |
prepare |
如果Acton實(shí)現(xiàn)了Preparable,則該攔截器調(diào)用Action類的prepare方法。 |
Scope Interceptor |
scope |
將Action狀態(tài)存入session和application的簡(jiǎn)單方法。 |
Servlet Config Interceptor |
servletConfig |
提供訪問(wèn)HttpServletRequest和HttpServletResponse的方法,以Map的方式訪問(wèn)。 |
Static Parameters Interceptor |
staticParams |
從struts.xml文件中將 |
Roles Interceptor |
roles |
確定用戶是否具有JAAS指定的Role,否則不予執(zhí)行。 |
Timer Interceptor |
timer |
輸出Action執(zhí)行的時(shí)間 |
Token Interceptor |
token |
通過(guò)Token來(lái)避免雙擊 |
Token Session Interceptor |
tokenSession |
和Token Interceptor一樣,不過(guò)雙擊的時(shí)候把請(qǐng)求的數(shù)據(jù)存儲(chǔ)在Session中 |
Validation Interceptor |
validation |
使用action-validation.xml文件中定義的內(nèi)容校驗(yàn)提交的數(shù)據(jù)。 |
Workflow Interceptor |
workflow |
調(diào)用Action的validate方法,一旦有錯(cuò)誤返回,重新定位到INPUT畫面 |
Parameter Filter Interceptor |
N/A |
從參數(shù)列表中刪除不必要的參數(shù) |
Profiling Interceptor |
profiling |
通過(guò)參數(shù)激活profile |
【編輯推薦】