Struts2的攔截器和Servlet的Filter過濾器及其相似,Struts2的攔截器只會處理action類,而servlet的過濾器可處理 servlet,jsp,html等等
攔截器可以說是Struts2的核心,大部分功能都是通過攔截器來實現的,只要我們的包繼承了 struts-default 包
<package name="struts2" extends="struts-default">,就可以使用struts-defaul 里的攔截器
自定義攔截器的步驟:
1) 編寫攔截器類,需要實現 Interceptor接口,並實現該接口的三個方法:init ,intercept ,destroy
或者繼承 AbstractInterceptor抽象類 ,並實現 intercept 方法就夠了
或者繼承 MethodFilterInterceptor抽象類,並實現 doIntercept方法
分析:Interceptor接口、 AbstractInterceptor抽象類、MethodFilterInterceptor抽象類,三者是存在關系的,
MethodFilterInterceptor抽象類繼承 AbstractInterceptor抽象類 實現 Interceptor接口
2) 在struts.xml 文件中定義攔截器(action標簽外)
3) 在struts.xml 文件中 的 action標簽內引用 攔截器
如果使用了自定義的攔截器,則在攔截器的最后還要加個
默認攔截器 <interceptor-ref name="defaultStack"></interceptor-ref> ,當自定義攔截器執行了,則默認攔截器不會執行
如果在action內沒加任何攔截器,則默認使用默認攔截器
舉例:
1.1自定義攔截器的第一種方式:實現 Interceptor接口
package com.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation; //定義一個攔截器1 public class Interceptor1 implements com.opensymphony.xwork2.interceptor.Interceptor { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("Interceptor1 before"); String str = invocation.invoke() ; System.out.println("Interceptor1 after"); return str; } }
1.2自定義攔截器的第二種方式:繼承AbstractInterceptor抽象類
package com.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; //定義一個攔截器2 public class Interceptor2 extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("Interceptor2 before"); String str = invocation.invoke() ; System.out.println("Interceptor2 after"); return str; } }
1.3自定義攔截器的第三種方式:繼承MethodFilterInterceptor抽象類
package com.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; //定義一個攔截器3,可以攔截自定義的方法,而其他攔截器只攔截 execute方法 public class Interceptor3 extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("Interceptor3 before"); String str = invocation.invoke() ; System.out.println("Interceptor3 after"); return str; } }
2.在struts.xml 文件中定義攔截器(action標簽外)
<struts> <package name="struts2" extends="struts-default"> <!-- 定義攔截器 --> <interceptors> <interceptor name="interceptor1" class="com.struts2.interceptor.Interceptor1"> </interceptor> <interceptor name="interceptor2" class="com.struts2.interceptor.Interceptor2"> </interceptor> <interceptor name="interceptor3" class="com.struts2.interceptor.Interceptor3"></interceptor> </interceptors>
3.在struts.xml 文件中 的 action標簽內引用 攔截器
假如用第三種方式定義攔截器的話,引用攔截器的時候還可以指定攔截的方法,excludeMethods 指定排除方法的攔截,includeMethods 把要攔截的方法包含進來,二者都可以排除多個方法,和包含多個方法,方法名用逗號‘ ,’隔開。
就如上面引用攔截器的第三個來說,action 是執行自定義的myExecute()方法的,但 includeMethods包含是execute方法,所以interceptor3攔截器不會執行
如果沒有指定攔截器的參數,默認會攔截全部方法
應用一(登錄,判斷用戶是否登錄):
1.定義一個攔截器類:
2.在struts.xml中定義 攔截器,和一個攔截器棧,注意 攔截器棧后也要加一個默認攔截器,
接着再把我們定義的攔截器棧,設為默認的攔截器,這樣的作用是攔截全部action,很顯然攔截全部action不是我們所要的,我們還需要一些action不被攔截,上面的攔截器已經處理了,就在 intercept方法內,加了
if(LoginAction.class == invocation.getAction().getClass() )
{
return invocation.invoke();
}
就可以不對其進行 判斷有沒session
在struts.xml中定義 攔截器,和一個攔截器棧,默認的攔截器:
注意 默認攔截器 定義的位置,是在interceptor外的
3.定義一個全局的結果,在package標簽下定義:
這個全局結果 <result name="login">/logingo.jsp</result> 會被 LoginInterceptor攔截器的 Action.LOGIN; 找到
4.具體處理登錄的action類
5.登陸界面logingo.jsp