Struts2的攔截器體系是一種AOP設(shè)計(jì)哲學(xué)。Strut2的攔截器可以動(dòng)態(tài)地?cái)r截發(fā)送到指定Action的請(qǐng)求,通過(guò)攔截器機(jī)制,可以在Action執(zhí)行前后插入某些代碼,通過(guò)這種方式可以把多個(gè)Action中需要重復(fù)指定的代碼提取出來(lái),在攔截器中定義,從而提供更好的代碼重用。攔截器體系是Struts2的一個(gè)重要組成部分,Struts2框架就是由大量的內(nèi)置攔截器來(lái)實(shí)現(xiàn)的。另外,它是即插即用的,即當(dāng)我們需要使用某個(gè)攔截器時(shí),只需要在配置文件中應(yīng)用中該攔截器即可,如果不需要使用攔截器,也只需要取消在配置文件中該攔截器,不管是否應(yīng)用某個(gè)攔截器,對(duì)于整個(gè)Strut2框架不會(huì)有任何影響的。

Struts框架提供了很多攔截器,它實(shí)現(xiàn)Struts2的大部分功能,能滿意大部分應(yīng)用的能用功能,但要實(shí)現(xiàn)系統(tǒng)邏輯相關(guān)的功能時(shí),需要使用用戶自定義攔截器:

如果用戶要開(kāi)發(fā)自己的攔截類,需要實(shí)現(xiàn)Interceptor接口,它包括三個(gè)方法:

Init()

destroy()

intercept(ActionInvocation action)

一般來(lái)說(shuō)只需要實(shí)現(xiàn)最后一個(gè)方法即可,所以Struts2提供了一個(gè)AbstractInterceptor類,它提供了initdestory方法的空實(shí)現(xiàn)。在實(shí)際的開(kāi)發(fā)中時(shí),只需要繼承AbstractInterceptor來(lái)實(shí)現(xiàn)自定義攔截器。下面實(shí)現(xiàn)一個(gè)簡(jiǎn)單的攔截器:

      實(shí)現(xiàn)攔截器類(SimpleInterceptor.java):
    
package my;
import java.util.Date;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.ActionInvocation;
public class SimpleInterceptor extends AbstractInterceptor
{
    
private String name;
    
public void setName(String name)
    
{
        
this.name = name;
    }

    
public String intercept(ActionInvocation invocation)throws Exception
    
{
        LoginAction action 
= (LoginAction)invocation.getAction();
        System.out.println(name
+"攔截器的動(dòng)作:開(kāi)始執(zhí)行登錄Action的時(shí)間為:"+new Date());
        
if(action.getTip().equals("flying"))
        
{
           System.out.println(
"user name error");
           
           
return "input";
        }

        
long start = System.currentTimeMillis();
        String result 
= invocation.invoke();
        System.out.println(name
+"攔截器的動(dòng)作:執(zhí)行完登錄Action的時(shí)間為:" + new Date());
        
long end = System.currentTimeMillis();
        System.out.println(
"攔截器的動(dòng)作:執(zhí)行action事件的時(shí)間是:"+(end-start)+"毫秒");
        
return result;
    }

}
配置攔截器(Struts.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
<struts>
    
    
<package name="strutsqs" extends="struts-default">
        
<interceptors>
            
<interceptor name="mysimple" class="my.SimpleInterceptor">
                
<param name="name">simple</param>
            
</interceptor>
        
</interceptors>
        
<action name="Login" class="my.LoginAction">

            
<result name="input">/login.jsp</result>
            
<result name="success">/success.jsp</result>
            
<interceptor-ref name="defaultStack"/>
            
<interceptor-ref name="mysimple">
                
<param name="name">my</param>
            
</interceptor-ref>
        
</action>
        
    
</package>

</struts>

這樣即實(shí)現(xiàn)一個(gè)簡(jiǎn)單的攔截器,默認(rèn)下,譔攔截器攔截了Action中的所有方法,如果我們只需要攔截某個(gè)方法,Struts2提供了一個(gè)MethodFilterInterceptor類對(duì)Action中方法過(guò)濾的功能,MethodFilterInterceptor是AbstractInterceptor類的子類,如果要實(shí)現(xiàn)攔截器方法過(guò)濾功能,則需要繼承MethodFilterInterceptor。用戶只需要重寫(xiě)MethodFilterInteceptor中的doInterceptor(ActionInvocation action)即可。其內(nèi)容實(shí)際上與interceptor一樣。
為了實(shí)現(xiàn)方法過(guò)濾只需要在interceptor的配置文件里設(shè)置兩個(gè)屬性:
    excludeMethods:指定不需要攔截的方法名
    includeMethods:指定需要攔截的方法名
若一個(gè)方法同時(shí)出現(xiàn)中,則該方法將被攔截。
一個(gè)方法過(guò)濾的攔截器實(shí)現(xiàn)(SimpleInterceptor.java)與上面的一樣:
package my;
import java.util.Date;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.opensymphony.xwork2.ActionInvocation;
public class SimpleInterceptor extends MethodFilterInterceptor
{
    
private String name;
    
public void setName(String name)
    
{
        
this.name = name;
    }

    
public String doIntercept(ActionInvocation invocation)throws Exception
    
{
        LoginAction action 
= (LoginAction)invocation.getAction();
        System.out.println(name
+"攔截器的動(dòng)作:開(kāi)始執(zhí)行登錄Action的時(shí)間為:"+new Date());
        
if(action.getTip().equals("flying"))
        
{
           System.out.println(
"user name error");
           
           
return "input";
        }

        
long start = System.currentTimeMillis();
        String result 
= invocation.invoke();
        System.out.println(name
+"攔截器的動(dòng)作:執(zhí)行完登錄Action的時(shí)間為:" + new Date());
        
long end = System.currentTimeMillis();
        System.out.println(
"攔截器的動(dòng)作:執(zhí)行action事件的時(shí)間是:"+(end-start)+"毫秒");
        
return result;
    }

}

配置方法過(guò)濾攔截器(Struts.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>
    
    
<package name="strutsqs" extends="struts-default">
        
<interceptors>
            
<interceptor name="mysimple" class="my.SimpleInterceptor">
                
<param name="name">simple</param>
            
</interceptor>
        
</interceptors>
        
<action name="Login" class="my.LoginAction">

            
<result name="input">/login.jsp</result>
            
<result name="success">/success.jsp</result>
            
<interceptor-ref name="defaultStack"/>
            
<interceptor-ref name="mysimple">
                
<param name="name">my</param>
                
<param name="excludeMethods">execute</param>
            
</interceptor-ref>
        
</action>
        
    
</package>

</struts>
上面只在引用攔截器時(shí)注入一個(gè)excludeMethods屬性,其它的與上面的配置一樣。