攔截器
1、什么是攔截器
攔截器,在AOP(Aspect-Oriented Programming)中用于在某個方法或字段被訪問之前,進行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實現策略。
在 Webwork的中文文檔的解釋為——攔截器是動態攔截Action調用的對象。它提供了一種機制可以使開發者可以定義在一個action執行的前后執行的代碼,也可以在一個action執行前阻止其執行。同時也是提供了一種可以提取action中可重用的部分的方式。
談到攔截器,還有一個詞大家應該知道——攔截器鏈(Interceptor Chain,在Struts 2中稱為攔截器棧Interceptor Stack)。攔截器鏈就是將攔截器按一定的順序聯結成一條鏈。在訪問被攔截的方法或字段時,攔截器鏈中的攔截器就會按其之前定義的順序被調用。
2、實現原理
Struts 2的攔截器實現相對簡單。當請求到達Struts 2的ServletDispatcher時,Struts 2會查找配置文件,并根據其配置實例化相對的攔截器對象,然后串成一個列表(list),最后一個一個地調用列表中的攔截器,如圖所示。
圖1 攔截器調用序列圖
?
?
3、已有的攔截器
Struts 2已經為您提供豐富多樣的,功能齊全的攔截器實現。大家可以到struts2-all-2.0.1.jar或struts2-core.jar包的
struts-default.xml查看關于默認的攔截器與攔截器鏈的配置
。
以下部分就是從struts-default.xml文件摘取的內容:
< interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" /> < interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" /> < interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" /> < interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" /> < interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" /> < interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" /> < interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" /> < interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" /> < interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" /> < interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" /> < interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" /> < interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" /> < interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" /> < interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" /> < interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" /> < interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" /> < interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" /> < interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" /> < interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" /> < interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" /> < interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" /> < interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" /> < interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" /> < interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" /> < interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" /> < interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" /> < interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" /> < interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />?
4、配置和使用攔截器
在struts-default.xml中已經配置了以上的攔截器。如果您想要使用上述攔截器,只需要在應用程序struts.xml文件中通過“<include file="struts-default.xml" />”將struts-default.xml文件包含進來,并繼承其中的struts-default包(package),最后在定義Action時,使用“<interceptor-ref name="xx" />”引用攔截器或攔截器棧(interceptor stack)。一旦您繼承了struts-default包(package),所有Action都會調用攔截器棧 ——defaultStack。當然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆蓋defaultStack。
5、自定義攔截器
作為“框架(framework)”,可擴展性是不可或缺的,因為世上沒有放之四海而皆準的東西。雖然,Struts 2為我們提供如此豐富的攔截器實現,但是這并不意味我們失去創建自定義攔截器的能力,恰恰相反,在Struts 2自定義攔截器是相當容易的一件事。
?
???
大家在開始著手創建自定義攔截器前,切記以下原則:
攔截器必須是無狀態的,不要使用在API提供的ActionInvocation之外的任何東西。
要求攔截器是無狀態的原因是Struts 2不能保證為每一個請求或者action創建一個實例,所以如果攔截器帶有狀態,會引發并發問題。
所有的Struts 2的攔截器都直接或間接實現接口
com.opensymphony.xwork2.interceptor.Interceptor
。除此之外,大家可能更喜歡繼承類com.opensymphony.xwork2.interceptor.
AbstractInterceptor
。
6、使用Token攔截器控制重復提交
jsp: <form action="user" method="post"> name:<input name="name"> age:<input name="age"> <input type="submit" value="add"> <s:token></s:token> </form> 生成的html: <form action="user" method="post"> name:<input name="name"> age:<input name="age"> <input type="submit" value="add"> <input type="hidden" name="struts.token.name" value="struts.token" /> <input type="hidden" name="struts.token" value="85PAL2KOFS09RKVAP1FFBB985BJEGEUQ" /> </form>
?
其中token為令牌,服務端生成存儲在session中并寫入客戶端頁面,等用戶提交form表單時會攜帶token,服務器比對token是否相同,相同則成功提交,并清除session中的token,如再重復提交服務端發現session中token已經沒有了即失效,便返回Token失效頁面。
?
Struts.xml:
<action name="user" class="com.bjsxt.action.UserAction"> <result>/addOK.jsp</result> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="token"></interceptor-ref> <result name="invalid.token">/error.jsp</result> </action>
?
其中增加token的攔截器,會覆蓋默認的defaultStack攔截器,所以需要再加上defaultStack在token前面,主要用來接收數據、聲明異常、國際化等等攔截器功能。
如果出現重復提交,即點擊多次add提交按鈕,提交了多次,則后臺認為token無效,返回error.jsp頁面。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
