Stust2的拦截器的运行流程及使用方法、注意事项
攔截器是Strust2的一大特色;攔截器是對(duì)用戶(hù)請(qǐng)求(.do,.action或者其他)的一種審查和額外的處理。攔截器與程序核心代碼分離,實(shí)現(xiàn)了AOP面向切面編程的技術(shù)原理;而且?guī)椭绦蛲瓿捎脩?hù)請(qǐng)求的安全機(jī)制等其他非功能性的審查和業(yè)務(wù)處理。所以說(shuō),攔截器是Struts2的一大特色。
1、(前期)程序啟動(dòng)時(shí),攔截器結(jié)合struts.xml配置文件,實(shí)現(xiàn)自身的初始化工作。
2、(請(qǐng)求)用戶(hù)在終端發(fā)送請(qǐng)求,會(huì)被web.xml配置文件的攔截,發(fā)送給struts.mxl中相應(yīng)的action標(biāo)簽。
3、action標(biāo)簽中設(shè)置的相應(yīng)的攔截器,會(huì)攔截用戶(hù)請(qǐng)求,進(jìn)入攔截器的intercept方法。
4、攔截器根據(jù)用戶(hù)請(qǐng)求的數(shù)據(jù),完成相對(duì)應(yīng)的攔截處理;
a、被攔截的請(qǐng)求,不執(zhí)行對(duì)應(yīng)action的操作;
b、通過(guò)的請(qǐng)求,會(huì)繼續(xù)執(zhí)行action的操作(也即arg0.invoke()方法就是執(zhí)行action的操作);
然后執(zhí)行完操作后,又會(huì)返回到攔截器;攔截器可以對(duì)action的返回結(jié)果做相應(yīng)的操作處理或者攔截處理;
5、然后返回一個(gè)String值給struts.xml配置文件對(duì)應(yīng)的<result>標(biāo)簽,做相應(yīng)的處理。
********************************分割線(xiàn)***************************************
如何使用struts2攔截器,或者自定義攔截器。特別注意,在使用攔截器的時(shí)候,在Action里面必須最后一定要引用struts2自帶的攔截器缺省堆棧defaultStack,如下(這里我是引用了struts2自帶的checkbox攔截器):
<interceptor-ref name="checkbox">
? <param name="uncheckedValue">0</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>(必須加,否則出錯(cuò))
也可以改為對(duì)全局Action設(shè)置自己需要的攔截器,如下:
在struts.xml里面定義全局的配置設(shè)置
? <package name="struts-shop"?extends="struts-default">
??? <interceptors>
????? <interceptor-stack name="myStack">
??????? <interceptor-ref name="checkbox">
????????? <param name="uncheckedValue">0</param>
?????? </interceptor-ref>
???????<interceptor-ref name="defaultStack"/>
????? </interceptor-stack>
??? </interceptors>
????<default-interceptor-ref name="myStack"/>(這句是設(shè)置所有Action自動(dòng)調(diào)用的攔截器堆棧)
? </package>
?
struts-action.xml里面配置Action如下:
? <package name="LogonAdmin"?extends="struts-shop">(這里擴(kuò)展struts.xml里面定義的配置就可以了)
??<action name="logon" class="logonAction">
???? <result>/jsp/smeishop/admin/index.jsp</result>
???? <result name="error">/jsp/smeishop/admin/logon.jsp</result>
???? <result name="input">/jsp/smeishop/admin/logon.jsp</result>
?? </action>
?? <action name="logout" class="logoutAction">
???? <result>/jsp/smeishop/admin/logon.jsp</result>
?? </action>
?</package>
你的攔截器可以正常工作了!!HOHO
以下是參考資料
struts2自帶的配置及其攔截器配置
Struts2 攔截器 [Interceptor]
攔截器的工作原理如上圖,每一個(gè)Action請(qǐng)求都包裝在一系列的攔截器的內(nèi)部。攔截器可以在Action執(zhí)行直線(xiàn)做相似的操作也可以在Action執(zhí)行直后做回收操作。
?
每一個(gè)Action既可以將操作轉(zhuǎn)交給下面的攔截器,Action也可以直接退出操作返回客戶(hù)既定的畫(huà)面。
?
如何自定義一個(gè)攔截器?
自定義一個(gè)攔截器需要三步:
1?自定義一個(gè)實(shí)現(xiàn)Interceptor接口(或者繼承自AbstractInterceptor)的類(lèi)。
2?在strutx.xml中注冊(cè)上一步中定義的攔截器。
3?在需要使用的Action中引用上述定義的攔截器,為了方便也可將攔截器定義為默認(rèn)的攔截器,這樣在不加特殊聲明的情況下所有的Action都被這個(gè)攔截器攔截。
?
Interceptor接口聲明了三個(gè)方法:
?
public interface Interceptor extends Serializable {
?
??? void destroy();
?
??? void init();
?
??? String intercept(ActionInvocation invocation) throws Exception;
}
?
Init方法在攔截器類(lèi)被創(chuàng)建之后,在對(duì)Action鏡像攔截之前調(diào)用,相當(dāng)于一個(gè)post-constructor方法,使用這個(gè)方法可以給攔截器類(lèi)做必要的初始話(huà)操作。
?
Destroy方法在攔截器被垃圾回收之前調(diào)用,用來(lái)回收init方法初始化的資源。
?
Intercept是攔截器的主要攔截方法,如果需要調(diào)用后續(xù)的Action或者攔截器,只需要在該方法中調(diào)用invocation.invoke()方法即可,在該方法調(diào)用的前后可以插入Action調(diào)用前后攔截器需要做的方法。如果不需要調(diào)用后續(xù)的方法,則返回一個(gè)String類(lèi)型的對(duì)象即可,例如Action.SUCCESS。
另外AbstractInterceptor提供了一個(gè)簡(jiǎn)單的Interceptor的實(shí)現(xiàn),這個(gè)實(shí)現(xiàn)為:
public abstract class AbstractInterceptor implements Interceptor {
?
???? public void init() {
??? }
???
??? public void destroy() {
??? }
?
?
??? public abstract String intercept(ActionInvocation invocation) throws Exception;
}
在不需要編寫(xiě)init和destroy方法的時(shí)候,只需要從AbstractInterceptor繼承而來(lái),實(shí)現(xiàn)intercept方法即可。
?
我們嘗試編寫(xiě)一個(gè)Session過(guò)濾用的攔截器,該攔截器查看用戶(hù)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!");
??????? //?對(duì)LoginAction不做該項(xiàng)攔截
??????? 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) {
??????????? //?存在的情況下進(jìn)行后續(xù)操作。
??????????? System.out.println("already login!");
??????????? return actionInvocation.invoke();
??????? } else {
??????????? //?否則終止后續(xù)操作,返回LOGIN
??????????? System.out.println("no login, forward login page!");
??????????? return LOGIN_PAGE;
??????? }
??? }
}
?
注冊(cè)攔截器
<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ù)同一個(gè)package內(nèi)部的所有Action執(zhí)行之前都會(huì)被login攔截。
?
?
Struts2(XWork)提供的攔截器的功能說(shuō)明:
?
| 攔截器 | 名字 | 說(shuō)明 |
| Alias Interceptor | alias | 在不同請(qǐng)求之間將請(qǐng)求參數(shù)在不同名字件轉(zhuǎn)換,請(qǐng)求內(nèi)容不變 |
| Chaining Interceptor | chain | 讓前一個(gè)Action的屬性可以被后一個(gè)Action訪(fǎng)問(wèn),現(xiàn)在和chain類(lèi)型的result(<result type=”chain”>)結(jié)合使用。 |
| 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í)將用戶(hù)帶到一個(gè)中間的等待頁(yè)面。 |
| Exception Interceptor | exception | 將異常定位到一個(gè)畫(huà)面 |
| File Upload Interceptor | fileUpload | 提供文件上傳功能 |
| I18n Interceptor | i18n | 記錄用戶(hù)選擇的locale |
| Logger Interceptor | logger | 輸出Action的名字 |
| Message Store Interceptor | store | 存儲(chǔ)或者訪(fǎng)問(wèn)實(shí)現(xiàn)ValidationAware接口的Action類(lèi)出現(xiàn)的消息,錯(cuò)誤,字段錯(cuò)誤等。 |
| Model Driven Interceptor | model-driven | 如果一個(gè)類(lèi)實(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類(lèi)的prepare方法。 |
| Scope Interceptor | scope | 將Action狀態(tài)存入session和application的簡(jiǎn)單方法。 |
| Servlet Config Interceptor | servletConfig | 提供訪(fǎng)問(wèn)HttpServletRequest和HttpServletResponse的方法,以Map的方式訪(fǎng)問(wèn)。 |
| Static Parameters Interceptor | staticParams | 從struts.xml文件中將<action>中的<param>中的內(nèi)容設(shè)置到對(duì)應(yīng)的Action中。 |
| Roles Interceptor | roles | 確定用戶(hù)是否具有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畫(huà)面 |
| Parameter Filter Interceptor | N/A | 從參數(shù)列表中刪除不必要的參數(shù) |
| Profiling Interceptor | profiling | 通過(guò)參數(shù)激活profile |
?
注冊(cè)并引用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>
?
可以將多個(gè)攔截器合并在一起作為一個(gè)堆棧調(diào)用,當(dāng)一個(gè)攔截器堆棧被附加到一個(gè)Action的時(shí)候,要想Action執(zhí)行,必須執(zhí)行攔截器堆棧中的每一個(gè)攔截器。
<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>
?
上述說(shuō)明的攔截器在默認(rèn)的Struts2應(yīng)用中,根據(jù)慣例配置了若干個(gè)攔截器堆棧,詳細(xì)情參看struts-default.xml
其中有一個(gè)攔截器堆棧比較特殊,他會(huì)應(yīng)用在默認(rèn)的每一個(gè)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>
?
每一個(gè)攔截器都可以配置參數(shù),有兩種方式配置參數(shù),一是針對(duì)每一個(gè)攔截器定義參數(shù),二是針對(duì)一個(gè)攔截器堆棧統(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>
?
每一個(gè)攔截器都有兩個(gè)默認(rèn)的參數(shù):
excludeMethods -?過(guò)濾掉不使用攔截器的方法和
includeMethods –?使用攔截器的方法。
?
需要說(shuō)明的幾點(diǎn):
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í)行順序?yàn)?#xff1a;
thisWillRunFirstInterceptor
?thisWillRunNextInterceptor
??? followedByThisInterceptor
????? thisWillRunLastInterceptor
??????? MyAction1
??????? MyAction2 (chain)
??????? MyPreResultListener
??????? MyResult (result)
????? thisWillRunLastInterceptor
??? followedByThisInterceptor
?thisWillRunNextInterceptor
thisWillRunFirstInterceptor
?
2?使用默認(rèn)攔截器配置每個(gè)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?如何訪(fǎng)問(wèn)HttpServletRequest,HttpServletResponse或者HttpSession
有兩種方法可以達(dá)到效果,使用ActionContext:
Map attibutes = ActionContext.getContext().getSession();
或者實(shí)現(xiàn)相應(yīng)的接口:
HttpSession??????????? SessionAware
HttpServletRequest???? ServletRequestAware
HttpServletResponse??? ServletResponseAware
?
轉(zhuǎn)載于:https://www.cnblogs.com/yangyabo/p/5365422.html
總結(jié)
以上是生活随笔為你收集整理的Stust2的拦截器的运行流程及使用方法、注意事项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 上传图片配置文件长度和宽度大小的说明
- 下一篇: LeetCode OJ -Happy N