struts2內建了大量的攔截器,這些攔截器以name-class對的形式配置在struts-default.xml文件中,name爲攔截器的名字,至關於該攔截器的惟一標識。class指定了該攔截器的實現類。在package中繼承了Struts2的默認struts-default包,則能夠自由使用這些內建攔截器。java
1)alias:實如今不一樣請求中類似參數別名的轉換;spring
2)autowiring:自動裝配的攔截器,用於與spring整合;session
3)chain:構建一個Action鏈,使當前Action能夠訪問前一個Action的屬性;app
4)conversionError:負責處理類型轉換錯誤的攔截器,負責將類型轉換錯誤從ActionContext中取出,並轉換成Action的FieldError錯誤;ide
5)createSession:負責建立一個HttpSession對象。this
6)debugging:當使用Struts2的開發模式,該攔截器提供更多的提示信息;spa
7)execAndWait:後臺執行Action,負責將等待畫面發送給用戶。debug
8)exception:負責處理異常,它將異常映射爲結果。日誌
9)fileUpload:負責文件上傳,解析表單中文件域的內容。code
10)i18n:支持國際化的攔截器,把所選的語言、區域放入用戶Session中;
11)model-driven:用於模型驅動的攔截器,當某個Action類實現了ModelDrien接口時,它負責把getModel()方法的結果放入堆ValueStack中;
12)logger:負責日誌記錄的攔截器,主要輸出Action的名字;
13)scoped-model-driven:若是一個Action實現了一個ScopedModelDriven接口,該攔截器負責從指定範圍中找到指定的Model,並經過setModel方法將該Model傳給Action實列;
14)params:負責解析Http請求中的參數,並將參數值設置爲Action中的參數;
15)prepare:若是Action實現了Preparable接口,將會調用該攔截器的prepare()方法;
16)static-params:將xml中action標籤下的<param>標籤中的參數傳入action。
17)scope:範圍轉換攔截器,它能夠將Action狀態信息保存到HttpSession範圍內,或者保存到ServletContext範圍;
18)servlet-config:若是某個Action須要直接訪問Servlet API ,就是經過這個攔截器實現。
19)roles:只有當瀏覽者取得適合的受權後,才能夠調用被該攔截器攔截的Action;
20)timer:負責輸出執行Action的執行時間;
21)token:用於阻止重複提交,
22)token-session:負責把token保存至HttpSession中;
23)validation:經過執行在xxxAction-validation.xml中定義的校驗器從而完成數據校驗;
24)workflow:負責調用Action類中的validate方法,若是校驗失敗則返回input試圖;
<!-- 配置攔截器 --> <interceptor name="攔截器名稱" class="攔截器實現類"></interceptor>
除了以上配置外還能夠將多個攔截器合在一塊兒配置攔截棧
<interceptor-stack name="loggerStack"> <!--使用內置攔截器--> <interceptor-ref name="logger" /> <interceptor-ref name="debugging" /> </interceptor-stack>
在一個攔截器中能夠配置多個攔截器,也能夠引用其餘的攔截棧
當Action中沒有聲明使用攔截器時,該Action所在包的默認攔截器纔會生效。每一個package只能配置一個默認攔截器。
<default-interceptor-ref name="默認攔截器" />
1)繼承com.opensymphony.xwork2.ActionInvocation抽象類,重寫intercept(ActionInvocation arg0)方法。
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import com.shcredit.controller.appkind.AddAppKindInfoAction; public class SimpleInterceptor extends AbstractInterceptor { //攔截器的名稱 private String name; public void setName(String name) { this.name = name; } @Override public String intercept(ActionInvocation arg0) throws Exception { System.out.println("進入"+name+"攔截器。。。。。。。"); //取得被攔截的Action實列 AddAppKindInfoAction action = (AddAppKindInfoAction)arg0.getAction(); //若是該攔截器後沒有其餘攔截器,則執行AddAppKindInfoAction中的execute方法 String result = arg0.invoke(); return result; } }
2)xml文件中,在須要使用該攔截器的package中配置該攔截器,而後在action中引入該攔截器便可。
<interceptors> <!-- 爲攔截器指定參數值 --> <interceptor name="simple" class="com.shcredit.controller.interceptor.SimpleInterceptor"> <param name="name">mySimple</param> </interceptor> </interceptors>
<action name="add" class="com.shcredit.controller.appkind.AddAppKindInfoAction"> <!-- 使用Simple攔截器 --> <interceptor-ref name="simple"> </interceptor-ref> </action>
在默認狀況下,若是咱們爲某個Action定義了攔截器,該攔截器就會攔截action中的全部方法,在一些狀況下咱們須要攔截一些特定的方法,就可使用到方法攔截器進行攔截。
1)實現com.opensymphony.xwork2.interceptor.MethodFilterInterceptor類,重寫其doIntercept(ActionInvocation action)回調方法。
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import com.shcredit.controller.appkind.AddAppKindInfoAction; import com.shcredit.vo.info.AppKindInfoVO; public class MyfliterIntercept extends MethodFilterInterceptor { private String name;//攔截器的名字 public void setName(String name) { this.name = name; } /* 攔截方法的攔截器,繼承MethodFilterInterceptor * @see com.opensymphony.xwork2.interceptor.MethodFilterInterceptor#doIntercept(com.opensymphony.xwork2.ActionInvocation) */ @Override protected String doIntercept(ActionInvocation action) throws Exception { AddAppKindInfoAction appKindAction = (AddAppKindInfoAction)action.getAction(); System.out.println("進入appKindAction攔截器。。。。。。"); AppKindInfoVO appKind = appKindAction.getAppKind(); String invoke = action.invoke(); return invoke; } }
2)在xml中對方法攔截器的配置和以前的同樣,須要注意的是它能夠選擇配置攔截方法
<interceptor-ref name="myfilter"> <!--從新定義name屬性的屬性值--> <param name="name">更名後的攔截方法過濾器</param> <!--指明須要被攔截的方法--> <param name="includeMethods">execute,testFliter,setAppKind</param> <!--指明不被攔截的方法--> <param name="excludeMethods">testNoFliter</param> </interceptor-ref>
在Action中的execute執行以後,返回試圖以前可使用PreResultListener進行監聽,在攔截器中使用該監聽器,只要該攔截器被觸發,該監聽也將會被觸發。
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.PreResultListener; public class MyPreResultListener implements PreResultListener { @Override public void beforeResult(ActionInvocation arg0, String arg1) { System.out.println("進入beforeResult"); //會致使死循環,進入action中的execute方法後進入該攔截器,又進入execute... //arg0.invoke(); System.out.println("返回的邏輯試圖名:"+arg1); } }
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class BeforeResultInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation arg0) throws Exception { //爲該攔截器添加MyPreResultListener監聽器 arg0.addPreResultListener(new MyPreResultListener()); System.out.println("執行execute方法以前的攔截"); String invoke = arg0.invoke(); System.out.println("execute方法執行以後的攔截"); return invoke; } }
將BeforeResultInterceptor攔截器配置在xml中,便可。
注意:不要在MyPreResultListener中調用invoke(),會致使死循環。