Struts2攔截器再認識
攔截器(Interceptor)是 Struts 2 的核心組成部分。
- Struts2 很多功能都是構建在攔截器基礎之上的,例如檔案的上傳和下載、國際化、資料型別轉換和資料校驗等等。
- Struts2 攔截器在訪問某個 Action 方法之前或之後實施攔截 Struts2 攔截器是可插拔的, 攔截器是 AOP(面向切面程式設計) 的一種實現.
- 攔截器棧(Interceptor Stack): 將攔截器按一定的順序聯結成一條鏈. 在訪問被攔截的方法時,
- Struts2 攔截器鏈中的攔截器就會按其之前定義的順序被依次呼叫
下面說明了棧的呼叫次序:
1 <package name="default" extends="struts-default"> 2<!--配置攔截器--> 3<interceptors> 4<interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor"> 5<param name="value" >YEN</param> 6</interceptor> 7<interceptor name="SecondInterceptor" class="Interceptor.SecondInterceptor"></interceptor> 8 //次序為 FirstInterceptor==》SecondInterceptor===》defaultStack===》SecondInterceptor====》FirstInterceptor 9<interceptor-stack name="AllInterceptor"> 10<interceptor-ref name="FirstInterceptor"></interceptor-ref> 11<interceptor-ref name="SecondInterceptor"></interceptor-ref> 12<interceptor-ref name="defaultStack"></interceptor-ref> 13</interceptor-stack> 14</interceptors> 15 16<action name="LoginAction" class="Action.LoginAction"> 17<interceptor-ref name="AllInterceptor"></interceptor-ref> 18<result name="success">success.jsp</result> 19</action>
struts2自帶的攔截器:
自定義規則
struts2提供的 interceptor-stack 不是讓我們偷懶的,很多時候,我定義自己很多的攔截器,每一個攔截器在不同業務場景下是不同的,我們不能為了圖方便,把所以攔截器全部配置為一個攔截器棧來使用,這回極大的加強伺服器的負擔,
比如說: methodFirstceptor 是一個方法攔截器,我們配置需要攔截的方法,而 function 攔截器又不需要攔截相關的方法,所以就沒有必要把他們放在一起。
<!-- 攔截器棧配置屬於自己的攔截器規則 --> <interceptor-stack name="mystack"> <interceptor-ref name="methodFirstceptor"></interceptor-ref> <interceptor-ref name="funtion"></interceptor-ref> <interceptor-ref name="defaultStack"> <param name="fileUpload.maximumSize">2097152</param> <!-- <param name="fileUpload.allowedTypes">text/html,text/xml</param> --> <param name="fileUpload.allowedExtensions">html,dtd,xml</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack>
注意:預設攔截器 也叫全域性攔截器 (也就是說我們 一定要注意在配置 預設攔截器的時候 不要加過多無用的攔截器,只要那些都需要作用到每個Action上的攔截器,才能配置攔截器棧 以及預設攔截器)
1<!-- 為此包下的所有action應用攔截器 --> 2<default-interceptor-ref name="permissionStack" />
普通攔截器:
1 //可以實現Interceptor 2 public class MyInterceptor extends AbstractInterceptor { 3 4/** 5* 6*/ 7private static final long serialVersionUID = 1L; 8@Override 9public String intercept(ActionInvocation invocation) throws Exception { 10 11System.out.println("before invocation.invoke..."); 12 13//可以不呼叫 14String result = invocation.invoke(); 15 16System.out.println("after invocation.invoke..."); 17 18return "success"; 19} 20 }
方法攔截器:
1 public class MethodFirstceptor extends MethodFilterInterceptor { 2/** 3* 4*/ 5private static final long serialVersionUID = 1L; 6 7@Override 8protected String doIntercept(ActionInvocation aInvocation) throws Exception { 9System.out.println("進入方法攔截器"); 10String str = aInvocation.invoke(); 11System.out.println("退出方法攔截器"); 12return str; 13} 14 15@Override 16public Set<String> getExcludeMethodsSet() { 17// TODO Auto-generated method stub 18return super.getExcludeMethodsSet(); 19} 20 21@Override 22public Set<String> getIncludeMethodsSet() { 23// TODO Auto-generated method stub 24return super.getIncludeMethodsSet(); 25} 26 }
struts2配置檔案:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4"http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 9<!-- 配置國際化資原始檔 --> 10<constant name="struts.custom.i18n.resources" value="i18n"></constant> 11<constant name="struts.devMode" value="true"></constant> 12 13<package name="default" namespace="/" extends="struts-default"> 14 15<interceptors> 16<!--方法攔截器配置 --> 17<interceptor name="methodFirstceptor" 18class="interceptors.MethodFirstceptor"> 19<!--配置引數 :攔截什麼方法 --> 20<!--includeMethods控制能訪問哪些 --> 21<param name="includeMethods">add</param> 22<!--excludeMethods控制能訪問哪些 --> 23</interceptor> 24 25<!-- 自定義攔截器 --> 26<interceptor name="hello" 27class="interceptors.MyInterceptor"></interceptor> 28 29<!-- 攔截器棧配置屬於自己的攔截器規則 --> 30<interceptor-stack name="mystack"> 31<interceptor-ref name="methodFirstceptor"></interceptor-ref> 32<interceptor-ref name="hello"></interceptor-ref> 33<interceptor-ref name="defaultStack"> 34<param name="fileUpload.maximumSize">2097152</param> 35<!-- <param name="fileUpload.allowedTypes">text/html,text/xml</param> --> 36<param name="fileUpload.allowedExtensions">html,dtd,xml</param> 37</interceptor-ref> 38<interceptor-ref name="defaultStack"></interceptor-ref> 39</interceptor-stack> 40 41</interceptors> 42 43<default-interceptor-ref name="mystack"></default-interceptor-ref> 44 45<!-- 執行預設的方法的時候 是不會被攔截的 --> 46<action name="loginAction" class="action.LoginAction"> 47<interceptor-ref name="mystack"></interceptor-ref> 48<result>/success.jsp</result> 49</action> 50 51<!-- 執行要攔截的方法 --> 52<action name="addAction" class="action.LoginAction" method="add"> 53<interceptor-ref name="mystack"></interceptor-ref> 54<result>/success.jsp</result> 55</action> 56 57 58 59 60<action name="testUpload" class="upload.UploadAction"> 61<result>/success.jsp</result> 62<result name="input">/upload.jsp</result> 63</action> 64 65<action name="testUpload2" class="upload.MulUploadAction"> 66<result>/success.jsp</result> 67<result name="input">/uploadmul.jsp</result> 68</action> 69 70<action name="testDownload" class="download.DownLoadAction"> 71<result type="stream"> 72<param name="bufferSize">2048</param> 73<!-- <param name="contentType">${contentType}</param> --> 74<!-- 呼叫當前action中的getContentType()方法 --> 75<param name="contentDisposition">attachment;filename=${filename}</param> 76<param name="inputStream">${inputStream}</param> 77<!-- 呼叫當前action中的getInputStream()方法 --> 78</result> 79</action> 80 81<!-- 表單重複提交問題 --> 82 83<action name="testToken" class="token.TokenAction"> 84<interceptor-ref name="mystack"></interceptor-ref> 85<!-- 會轉到標記為invalid-token的頁面 --> 86<!-- <interceptor-ref name="token"></interceptor-ref> --> 87<!-- 停留在當前頁面,不做任何操作 --> 88<interceptor-ref name="tokenSession"></interceptor-ref> 89<!-- 必須呼叫預設的攔截器 --> 90<interceptor-ref name="defaultStack"></interceptor-ref> 91<result>/success.jsp</result> 92<result name="invalid.token">/token-error.jsp</result> 93</action> 94 95</package> 96 97 98 </struts>
< package name = "default" extends = "struts-default" > <!--配置攔截器--> < interceptors > < interceptor name = "FirstInterceptor" class = "Interceptor.FirstInterceptor" > < param name = "value" > YEN </ param > </ interceptor > < interceptor name = "SecondInterceptor" class = "Interceptor.SecondInterceptor" > </ interceptor > < interceptor-stack name = "AllInterceptor" > < interceptor-ref name = "FirstInterceptor" > </ interceptor-ref > < interceptor-ref name = "SecondInterceptor" > </ interceptor-ref > < interceptor-ref name = "defaultStack" > </ interceptor-ref > </ interceptor-stack > </ interceptors > < action name = "LoginAction" class = "Action.LoginAction" > < interceptor-ref name = "AllInterceptor" > </ interceptor-ref > < result name = "success" > success.jsp </ result > </ action >