
攔截器的工作原理如上圖,每一個Action請求都包裝在一系列的攔截器的內(nèi)部。攔截器可以在Action執(zhí)行直線做相似的操作也可以在Action執(zhí)行直后做回收操作。
每一個Action既可以將操作轉(zhuǎn)交給下面的攔截器,Action也可以直接退出操作返回客戶既定的畫面。
如何自定義一個攔截器?
自定義一個攔截器需要三步:
1 自定義一個實現(xiàn)Interceptor接口(或者繼承自AbstractInterceptor)的類。
2 在strutx.xml中注冊上一步中定義的攔截器。
3 在需要使用的Action中引用上述定義的攔截器,為了方便也可將攔截器定義為默認(rèn)的攔截器,這樣在不加特殊聲明的情況下所有的Action都被這個攔截器攔截。
Interceptor接口聲明了三個方法:
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
Init方法在攔截器類被創(chuàng)建之后,在對Action鏡像攔截之前調(diào)用,相當(dāng)于一個post-constructor方法,使用這個方法可以給攔截器類做必要的初始話操作。
Destroy方法在攔截器被垃圾回收之前調(diào)用,用來回收init方法初始化的資源。
Intercept是攔截器的主要攔截方法,如果需要調(diào)用后續(xù)的Action或者攔截器,只需要在該方法中調(diào)用invocation.invoke()方法即可,在該方法調(diào)用的前后可以插入Action調(diào)用前后攔截器需要做的方法。如果不需要調(diào)用后續(xù)的方法,則返回一個String類型的對象即可,例如Action.SUCCESS。
另外AbstractInterceptor提供了一個簡單的Interceptor的實現(xiàn),這個實現(xiàn)為:
public abstract class AbstractInterceptor implements Interceptor {
public void init() {
}
public void destroy() {
}
public abstract String intercept(ActionInvocation invocation) throws Exception;
}
在不需要編寫init和destroy方法的時候,只需要從AbstractInterceptor繼承而來,實現(xiàn)intercept方法即可。
我們嘗試編寫一個Session過濾用的攔截器,該攔截器查看用戶Session中是否存在特定的屬性(LOGIN屬性)如果不存在,中止后續(xù)操作定位到LOGIN,否則執(zhí)行原定操作,代碼為:
public class CheckLoginInterceptor extends AbstractInterceptor {
public static final String LOGIN_KEY = "LOGIN";
public static final String LOGIN_PAGE = "global.login";
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!");
// 對LoginAction不做該項攔截
Object action = actionInvocation.getAction();
if (action instanceof LoginAction) {
System.out.println("exit check login, because this is login action.");
return actionInvocation.invoke();
}
// 確認(rèn)Session中是否存在LOGIN
Map session = actionInvocation.getInvocationContext().getSession();
String login = (String) session.get(LOGIN_KEY);
if (login != null && login.length() > 0) {
// 存在的情況下進行后續(xù)操作。
System.out.println("already login!");
return actionInvocation.invoke();
} else {
// 否則終止后續(xù)操作,返回LOGIN
System.out.println("no login, forward login page!");
return LOGIN_PAGE;
}
}
}
注冊攔截器
<interceptors>
<interceptor
name="login"
class="com.jpleasure.teamware.util.CheckLoginInterceptor"/>
<interceptor-stack name="teamwareStack">
<interceptor-ref name="login"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
將上述攔截器設(shè)定為默認(rèn)攔截器:
<default-interceptor-ref name="teamwareStack"/>
這樣在后續(xù)同一個package內(nèi)部的所有Action執(zhí)行之前都會被login攔截。
Struts2(XWork)提供的攔截器的功能說明:
攔截器
|
名字
|
說明
|
Alias Interceptor
|
alias
|
在不同請求之間將請求參數(shù)在不同名字件轉(zhuǎn)換,請求內(nèi)容不變
|
Chaining Interceptor
|
chain
|
讓前一個Action的屬性可以被后一個Action訪問,現(xiàn)在和chain類型的result(<result type=”chain”>)結(jié)合使用。
|
Checkbox Interceptor
|
checkbox
|
添加了checkbox自動處理代碼,將沒有選中的checkbox的內(nèi)容設(shè)定為false,而html默認(rèn)情況下不提交沒有選中的checkbox。
|
Cookies Interceptor
|
cookies
|
使用配置的name,value來是指cookies
|
Conversion Error Interceptor
|
conversionError
|
將錯誤從ActionContext中添加到Action的屬性字段中。
|
Create Session Interceptor
|
createSession
|
自動的創(chuàng)建HttpSession,用來為需要使用到HttpSession的攔截器服務(wù)。
|
Debugging Interceptor
|
debugging
|
提供不同的調(diào)試用的頁面來展現(xiàn)內(nèi)部的數(shù)據(jù)狀況。
|
Execute and Wait Interceptor
|
execAndWait
|
在后臺執(zhí)行Action,同時將用戶帶到一個中間的等待頁面。
|
Exception Interceptor
|
exception
|
將異常定位到一個畫面
|
File Upload Interceptor
|
fileUpload
|
提供文件上傳功能
|
I18n Interceptor
|
i18n
|
記錄用戶選擇的locale
|
Logger Interceptor
|
logger
|
輸出Action的名字
|
Message Store Interceptor
|
store
|
存儲或者訪問實現(xiàn)ValidationAware接口的Action類出現(xiàn)的消息,錯誤,字段錯誤等。
|
Model Driven Interceptor
|
model-driven
|
如果一個類實現(xiàn)了ModelDriven,將getModel得到的結(jié)果放在Value Stack中。
|
Scoped Model Driven
|
scoped-model-driven
|
如果一個Action實現(xiàn)了ScopedModelDriven,則這個攔截器會從相應(yīng)的Scope中取出model調(diào)用Action的setModel方法將其放入Action內(nèi)部。
|
Parameters Interceptor
|
params
|
將請求中的參數(shù)設(shè)置到Action中去。
|
Prepare Interceptor
|
prepare
|
如果Acton實現(xiàn)了Preparable,則該攔截器調(diào)用Action類的prepare方法。
|
Scope Interceptor
|
scope
|
將Action狀態(tài)存入session和application的簡單方法。
|
Servlet Config Interceptor
|
servletConfig
|
提供訪問HttpServletRequest和HttpServletResponse的方法,以Map的方式訪問。
|
Static Parameters Interceptor
|
staticParams
|
從struts.xml文件中將<action>中的<param>中的內(nèi)容設(shè)置到對應(yīng)的Action中。
|
Roles Interceptor
|
roles
|
確定用戶是否具有JAAS指定的Role,否則不予執(zhí)行。
|
Timer Interceptor
|
timer
|
輸出Action執(zhí)行的時間
|
Token Interceptor
|
token
|
通過Token來避免雙擊
|
Token Session Interceptor
|
tokenSession
|
和Token Interceptor一樣,不過雙擊的時候把請求的數(shù)據(jù)存儲在Session中
|
Validation Interceptor
|
validation
|
使用action-validation.xml文件中定義的內(nèi)容校驗提交的數(shù)據(jù)。
|
Workflow Interceptor
|
workflow
|
調(diào)用Action的validate方法,一旦有錯誤返回,重新定位到INPUT畫面
|
Parameter Filter Interceptor
|
N/A
|
從參數(shù)列表中刪除不必要的參數(shù)
|
Profiling Interceptor
|
profiling
|
通過參數(shù)激活profile
|
注冊并引用Interceptor
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".."/>
<interceptor name="logger" class=".."/>
</interceptors>
<action name="login" class="tutorial.Login">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
<result name="input">login.jsp</result>
<result name="success"
type="redirect-action">/secure/home</result>
</action>
</package>
可以將多個攔截器合并在一起作為一個堆棧調(diào)用,當(dāng)一個攔截器堆棧被附加到一個Action的時候,要想Action執(zhí)行,必須執(zhí)行攔截器堆棧中的每一個攔截器。
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".."/>
<interceptor name="logger" class=".."/>
<interceptor-stack name="myStack">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
</interceptor-stack>
</interceptors>
<action name="login" class="tutuorial.Login">
<interceptor-ref name="myStack"/>
<result name="input">login.jsp</result>
<result name="success"
type="redirect-action">/secure/home</result>
</action>
</package>
上述說明的攔截器在默認(rèn)的Struts2應(yīng)用中,根據(jù)慣例配置了若干個攔截器堆棧,詳細情參看struts-default.xml
其中有一個攔截器堆棧比較特殊,他會應(yīng)用在默認(rèn)的每一個Action上。
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo"..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
每一個攔截器都可以配置參數(shù),有兩種方式配置參數(shù),一是針對每一個攔截器定義參數(shù),二是針對一個攔截器堆棧統(tǒng)一定義所有的參數(shù),例如:
<interceptor-ref name="validation">
<param name="excludeMethods">myValidationExcudeMethod</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">myWorkflowExcludeMethod</param>
</interceptor-ref>
或者
<interceptor-ref name="defaultStack">
<param name="validation.excludeMethods">myValidationExcludeMethod</param>
<param name="workflow.excludeMethods">myWorkflowExcludeMethod</param>
</interceptor-ref>
每一個攔截器都有兩個默認(rèn)的參數(shù):
excludeMethods - 過濾掉不使用攔截器的方法和
includeMethods – 使用攔截器的方法。
需要說明的幾點:
1 攔截器執(zhí)行的順序按照定義的順序執(zhí)行,例如:
<interceptor-stack name="xaStack">
<interceptor-ref name="thisWillRunFirstInterceptor"/>
<interceptor-ref name="thisWillRunNextInterceptor"/>
<interceptor-ref name="followedByThisInterceptor"/>
<interceptor-ref name="thisWillRunLastInterceptor"/>
</interceptor-stack>
的執(zhí)行順序為:
thisWillRunFirstInterceptor
thisWillRunNextInterceptor
followedByThisInterceptor
thisWillRunLastInterceptor
MyAction1
MyAction2 (chain)
MyPreResultListener
MyResult (result)
thisWillRunLastInterceptor
followedByThisInterceptor
thisWillRunNextInterceptor
thisWillRunFirstInterceptor
2 使用默認(rèn)攔截器配置每個Action都需要的攔截器堆棧,例如:
<action name="login" class="tutorial.Login">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
<interceptor-ref name="default-stack"/>
<result name="input">login.jsp</result>
<result type="redirect-action">/secure/home</result>
</action>
可以按照如下的方式定義:
<interceptors>
<interceptor-stack name="myStack">
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
<interceptor-ref name="default-stack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<action name="login" class="tutorial.Login">
<result name="input">login.jsp</result>
<result type="redirect-action">/secure/home</result>
</action>
3 如何訪問HttpServletRequest,HttpServletResponse或者HttpSession
有兩種方法可以達到效果,使用ActionContext:
Map attibutes = ActionContext.getContext().getSession();
或者實現(xiàn)相應(yīng)的接口:
HttpSession SessionAware
HttpServletRequest ServletRequestAware
HttpServletResponse ServletResponseAware