第九章 攔截器

 

攔截器的工作原理如上圖,每一個(gè)Action請(qǐng)求都包裝在一系列的攔截器的內(nèi)部。攔截器可以在Action執(zhí)行直線做相似的操作也可以在Action執(zhí)行直后做回收操作。

 

每一個(gè)Action既可以將操作轉(zhuǎn)交給下面的攔截器,Action也可以直接退出操作返回客戶既定的畫面。

 

如何自定義一個(gè)攔截器?

自定義一個(gè)攔截器需要三步:

1 自定義一個(gè)實(shí)現(xiàn)Interceptor接口(或者繼承自AbstractInterceptor)的類。

2 strutx.xml中注冊(cè)上一步中定義的攔截器。

3 在需要使用的Action中引用上述定義的攔截器,為了方便也可將攔截器定義為默認(rèn)的攔截器,這樣在不加特殊聲明的情況下所有的Action都被這個(gè)攔截器攔截。

 

Interceptor接口聲明了三個(gè)方法:

 

public interface Interceptor extends Serializable {

 

    void destroy();

 

    void init();

 

    String intercept(ActionInvocation invocation) throws Exception;

}

 

Init方法在攔截器類被創(chuàng)建之后,在對(duì)Action鏡像攔截之前調(diào)用,相當(dāng)于一個(gè)post-constructor方法,使用這個(gè)方法可以給攔截器類做必要的初始話操作。

 

Destroy方法在攔截器被垃圾回收之前調(diào)用,用來(lái)回收init方法初始化的資源。

 

Intercept是攔截器的主要攔截方法,如果需要調(diào)用后續(xù)的Action或者攔截器,只需要在該方法中調(diào)用invocation.invoke()方法即可,在該方法調(diào)用的前后可以插入Action調(diào)用前后攔截器需要做的方法。如果不需要調(diào)用后續(xù)的方法,則返回一個(gè)String類型的對(duì)象即可,例如Action.SUCCESS。

另外AbstractInterceptor提供了一個(gè)簡(jiǎn)單的Interceptor的實(shí)現(xiàn),這個(gè)實(shí)現(xiàn)為:

public abstract class AbstractInterceptor implements Interceptor {

 

     public void init() {

    }

   

    public void destroy() {

    }

 

 

    public abstract String intercept(ActionInvocation invocation) throws Exception;

}

在不需要編寫initdestroy方法的時(shí)候,只需要從AbstractInterceptor繼承而來(lái),實(shí)現(xiàn)intercept方法即可。

 

我們嘗試編寫一個(gè)Session過(guò)濾用的攔截器,該攔截器查看用戶Session中是否存在特定的屬性(LOGIN屬性)如果不存在,中止后續(xù)操作定位到LOGIN,否則執(zhí)行原定操作,代碼為:

public class CheckLoginInterceptor extends AbstractInterceptor {

    public static final String LOGIN_KEY = "LOGIN";

    public static final String LOGIN_PAGE = "global.login";

 

    public String intercept(ActionInvocation actionInvocation) throws Exception {

 

        System.out.println("begin check login interceptor!");

        // 對(duì)LoginAction不做該項(xiàng)攔截

        Object action = actionInvocation.getAction();

        if (action instanceof LoginAction) {

            System.out.println("exit check login, because this is login action.");

            return actionInvocation.invoke();

        }

 

        // 確認(rèn)Session中是否存在LOGIN

        Map session = actionInvocation.getInvocationContext().getSession();

        String login = (String) session.get(LOGIN_KEY);

        if (login != null && login.length() > 0) {

            // 存在的情況下進(jìn)行后續(xù)操作。

            System.out.println("already login!");

            return actionInvocation.invoke();

        } else {

            // 否則終止后續(xù)操作,返回LOGIN

            System.out.println("no login, forward login page!");

            return LOGIN_PAGE;

        }

    }

}

 

注冊(cè)攔截器

<interceptors>

            <interceptor

name="login" 

class="com.jpleasure.teamware.util.CheckLoginInterceptor"/>

            <interceptor-stack name="teamwareStack">

                <interceptor-ref name="login"/>

                <interceptor-ref name="defaultStack"/>

            </interceptor-stack>

</interceptors>

 

將上述攔截器設(shè)定為默認(rèn)攔截器:

<default-interceptor-ref name="teamwareStack"/>

這樣在后續(xù)同一個(gè)package內(nèi)部的所有Action執(zhí)行之前都會(huì)被login攔截。

 

 

Struts2XWork)提供的攔截器的功能說(shuō)明:

 

攔截器

名字

說(shuō)明

Alias Interceptor

alias

在不同請(qǐng)求之間將請(qǐng)求參數(shù)在不同名字件轉(zhuǎn)換,請(qǐng)求內(nèi)容不變

Chaining Interceptor

chain

讓前一個(gè)Action的屬性可以被后一個(gè)Action訪問(wèn),現(xiàn)在和chain類型的result<result type=”chain”>)結(jié)合使用。

Checkbox Interceptor

checkbox

添加了checkbox自動(dòng)處理代碼,將沒(méi)有選中的checkbox的內(nèi)容設(shè)定為false,而html默認(rèn)情況下不提交沒(méi)有選中的checkbox

Cookies Interceptor

cookies

使用配置的name,value來(lái)是指cookies

Conversion Error Interceptor

conversionError

將錯(cuò)誤從ActionContext中添加到Action的屬性字段中。

Create Session Interceptor

createSession

自動(dòng)的創(chuàng)建HttpSession,用來(lái)為需要使用到HttpSession的攔截器服務(wù)。

Debugging Interceptor

debugging

提供不同的調(diào)試用的頁(yè)面來(lái)展現(xiàn)內(nèi)部的數(shù)據(jù)狀況。

Execute and Wait Interceptor

execAndWait

在后臺(tái)執(zhí)行Action,同時(shí)將用戶帶到一個(gè)中間的等待頁(yè)面。

Exception Interceptor

exception

將異常定位到一個(gè)畫面

File Upload Interceptor

fileUpload

提供文件上傳功能

I18n Interceptor

i18n

記錄用戶選擇的locale

Logger Interceptor

logger

輸出Action的名字

Message Store Interceptor

store

存儲(chǔ)或者訪問(wèn)實(shí)現(xiàn)ValidationAware接口的Action類出現(xiàn)的消息,錯(cuò)誤,字段錯(cuò)誤等。

Model Driven Interceptor

model-driven

如果一個(gè)類實(shí)現(xiàn)了ModelDriven,將getModel得到的結(jié)果放在Value Stack中。

Scoped Model Driven

scoped-model-driven

如果一個(gè)Action實(shí)現(xiàn)了ScopedModelDriven,則這個(gè)攔截器會(huì)從相應(yīng)的Scope中取出model調(diào)用ActionsetModel方法將其放入Action內(nèi)部。

Parameters Interceptor

params

將請(qǐng)求中的參數(shù)設(shè)置到Action中去。

Prepare Interceptor

prepare

如果Acton實(shí)現(xiàn)了Preparable,則該攔截器調(diào)用Action類的prepare方法。

Scope Interceptor

scope

Action狀態(tài)存入sessionapplication的簡(jiǎn)單方法。

Servlet Config Interceptor

servletConfig

提供訪問(wèn)HttpServletRequestHttpServletResponse的方法,以Map的方式訪問(wèn)。

Static Parameters Interceptor

staticParams

struts.xml文件中將<action>中的<param>中的內(nèi)容設(shè)置到對(duì)應(yīng)的Action中。

Roles Interceptor

roles

確定用戶是否具有JAAS指定的Role,否則不予執(zhí)行。

Timer Interceptor

timer

輸出Action執(zhí)行的時(shí)間

Token Interceptor

token

通過(guò)Token來(lái)避免雙擊

Token Session Interceptor

tokenSession

Token Interceptor一樣,不過(guò)雙擊的時(shí)候把請(qǐng)求的數(shù)據(jù)存儲(chǔ)在Session

Validation Interceptor

validation

使用action-validation.xml文件中定義的內(nèi)容校驗(yàn)提交的數(shù)據(jù)。

Workflow Interceptor

workflow

調(diào)用Actionvalidate方法,一旦有錯(cuò)誤返回,重新定位到INPUT畫面

Parameter Filter Interceptor

N/A

從參數(shù)列表中刪除不必要的參數(shù)

Profiling Interceptor

profiling

通過(guò)參數(shù)激活profile

 

注冊(cè)并引用Interceptor

<package name="default" extends="struts-default">

   <interceptors>

       <interceptor name="timer" class=".."/>

       <interceptor name="logger" class=".."/>

   </interceptors>

 

   <action name="login" class="tutorial.Login">

        <interceptor-ref name="timer"/>

        <interceptor-ref name="logger"/>

        <result name="input">login.jsp</result>

        <result name="success"

            type="redirect-action">/secure/home</result>

   </action>

</package>

 

可以將多個(gè)攔截器合并在一起作為一個(gè)堆棧調(diào)用,當(dāng)一個(gè)攔截器堆棧被附加到一個(gè)Action的時(shí)候,要想Action執(zhí)行,必須執(zhí)行攔截器堆棧中的每一個(gè)攔截器。

<package name="default" extends="struts-default">

   <interceptors>

        <interceptor name="timer" class=".."/>

        <interceptor name="logger" class=".."/>

        <interceptor-stack name="myStack">

           <interceptor-ref name="timer"/>

           <interceptor-ref name="logger"/>

        </interceptor-stack>

    </interceptors>

 

    <action name="login" class="tutuorial.Login">

         <interceptor-ref name="myStack"/>

         <result name="input">login.jsp</result>

         <result name="success"

             type="redirect-action">/secure/home</result>

    </action>

</package>

 

上述說(shuō)明的攔截器在默認(rèn)的Struts2應(yīng)用中,根據(jù)慣例配置了若干個(gè)攔截器堆棧,詳細(xì)情參看struts-default.xml

其中有一個(gè)攔截器堆棧比較特殊,他會(huì)應(yīng)用在默認(rèn)的每一個(gè)Action上。

<interceptor-stack name="defaultStack">

    <interceptor-ref name="exception"/>

    <interceptor-ref name="alias"/>

    <interceptor-ref name="servletConfig"/>

    <interceptor-ref name="prepare"/>

    <interceptor-ref name="i18n"/>

    <interceptor-ref name="chain"/>

    <interceptor-ref name="debugging"/>

    <interceptor-ref name="profiling"/>

    <interceptor-ref name="scopedModelDriven"/>

    <interceptor-ref name="modelDriven"/>

    <interceptor-ref name="fileUpload"/>

    <interceptor-ref name="checkbox"/>

    <interceptor-ref name="staticParams"/>

    <interceptor-ref name="params">

      <param name="excludeParams">dojo\..*</param>

    </interceptor-ref>

    <interceptor-ref name="conversionError"/>

    <interceptor-ref name="validation">

        <param name="excludeMethods">input,back,cancel,browse</param>

    </interceptor-ref>

    <interceptor-ref name="workflow">

        <param name="excludeMethods">input,back,cancel,browse</param>

    </interceptor-ref>

</interceptor-stack>

 

每一個(gè)攔截器都可以配置參數(shù),有兩種方式配置參數(shù),一是針對(duì)每一個(gè)攔截器定義參數(shù),二是針對(duì)一個(gè)攔截器堆棧統(tǒng)一定義所有的參數(shù),例如:

<interceptor-ref name="validation">

  <param name="excludeMethods">myValidationExcudeMethod</param>

</interceptor-ref>

<interceptor-ref name="workflow">

  <param name="excludeMethods">myWorkflowExcludeMethod</param>

</interceptor-ref>

或者

<interceptor-ref name="defaultStack">

    <param name="validation.excludeMethods">myValidationExcludeMethod</param>

    <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param>

</interceptor-ref>

 

每一個(gè)攔截器都有兩個(gè)默認(rèn)的參數(shù):

excludeMethods - 過(guò)濾掉不使用攔截器的方法和

includeMethods – 使用攔截器的方法。

 

需要說(shuō)明的幾點(diǎn):

1 攔截器執(zhí)行的順序按照定義的順序執(zhí)行,例如:

<interceptor-stack name="xaStack">

  <interceptor-ref name="thisWillRunFirstInterceptor"/>

  <interceptor-ref name="thisWillRunNextInterceptor"/>

  <interceptor-ref name="followedByThisInterceptor"/>

  <interceptor-ref name="thisWillRunLastInterceptor"/>

</interceptor-stack>

的執(zhí)行順序?yàn)椋?/font>

thisWillRunFirstInterceptor

  thisWillRunNextInterceptor

    followedByThisInterceptor

      thisWillRunLastInterceptor

        MyAction1

        MyAction2 (chain)

        MyPreResultListener

        MyResult (result)

      thisWillRunLastInterceptor

    followedByThisInterceptor

  thisWillRunNextInterceptor

thisWillRunFirstInterceptor

 

2 使用默認(rèn)攔截器配置每個(gè)Action都需要的攔截器堆棧,例如:

<action name="login"  class="tutorial.Login">

     <interceptor-ref name="timer"/>

     <interceptor-ref name="logger"/>

     <interceptor-ref name="default-stack"/>

 

     <result name="input">login.jsp</result>

     <result type="redirect-action">/secure/home</result>

</action>

可以按照如下的方式定義:

<interceptors>

     <interceptor-stack name="myStack">

        <interceptor-ref name="timer"/>

        <interceptor-ref name="logger"/>

        <interceptor-ref name="default-stack"/>

     </interceptor-stack>

</interceptors>

 

<default-interceptor-ref name="myStack"/>

 

<action name="login"  class="tutorial.Login">

       <result name="input">login.jsp</result>

       <result type="redirect-action">/secure/home</result>

</action>

 

3 如何訪問(wèn)HttpServletRequest,HttpServletResponse或者HttpSession

有兩種方法可以達(dá)到效果,使用ActionContext

Map attibutes = ActionContext.getContext().getSession();

或者實(shí)現(xiàn)相應(yīng)的接口:

HttpSession            SessionAware

HttpServletRequest     ServletRequestAware

HttpServletResponse    ServletResponseAware



ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程