SSH Struts2攔截器

2018-09-28 18:55 更新

Struts2 攔截器

什么是攔截器

攔截器(Interceptor)是Struts 2的一個強有力的工具,有許多功能都是構(gòu)建于它之上,如國際化(前兩篇博客介紹過)、轉(zhuǎn)換器,校驗等。攔截器是動態(tài)攔截Action調(diào)用的對象。它提供了一種機制可以使開發(fā)者可以定義在一個action執(zhí)行的前后執(zhí)行的代碼,也可以在一個action執(zhí)行前阻止其執(zhí)行。同時也是提供了一種可以提取action中可重用的部分的方式。說到攔截器有一個東西不能落下——攔截器鏈(Interceptor Chain,在Struts 2中稱為攔截器棧Interceptor Stack)。攔截器鏈就是將攔截器按一定的順序聯(lián)結(jié)成一條鏈。在訪問被攔截的方法或字段時,攔截器鏈中的攔截器就會按其之前定義的順序被調(diào)用。

實現(xiàn)原理

Struts 2的攔截器實現(xiàn)相對簡單。當(dāng)請求到達Struts 2的ServletDispatcher時,Struts 2會查找配置文件,并根據(jù)其配置實例化相對的攔截器對象,然后串成一個列表(list),最后一個一個地調(diào)用列表中的攔截器。



Struts 2提供了豐富多樣的,功能齊全的攔截器實現(xiàn)。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看關(guān)于默認(rèn)的攔截器與攔截器鏈的配置。

相關(guān)說明如下:

攔截器名字說明
Alias Interceptoralias在不同請求之間將請求參數(shù)在不同名字件轉(zhuǎn)換,請求內(nèi)容不變
Chaining Interceptorchain讓前一個Action的屬性可以被后一個Action訪問,現(xiàn)在和chain類型的result()結(jié)合使用。
Checkbox Interceptorcheckbox添加了checkbox自動處理代碼,將沒有選中的checkbox的內(nèi)容設(shè)定為false,而html默認(rèn)情況下不提交沒有選中的checkbox。
Cookies Interceptorcookies使用配置的name,value來是指cookies
Conversion Error InterceptorconversionError將錯誤從ActionContext中添加到Action的屬性字段中。
Create Session InterceptorcreateSession自動的創(chuàng)建HttpSession,用來為需要使用到HttpSession的攔截器服務(wù)。
Debugging Interceptordebugging提供不同的調(diào)試用的頁面來展現(xiàn)內(nèi)部的數(shù)據(jù)狀況。
Execute and Wait InterceptorexecAndWait在后臺執(zhí)行Action,同時將用戶帶到一個中間的等待頁面。
Exception Interceptorexception將異常定位到一個畫面
File Upload InterceptorfileUpload提供文件上傳功能
I18n Interceptori18n記錄用戶選擇的locale
Logger Interceptorlogger輸出Action的名字
Message Store Interceptorstore存儲或者訪問實現(xiàn)ValidationAware接口的Action類出現(xiàn)的消息,錯誤,字段錯誤等。
Model Driven Interceptormodel-driven如果一個類實現(xiàn)了ModelDriven,將getModel得到的結(jié)果放在Value Stack中。
Scoped Model Drivenscoped-model-driven如果一個Action實現(xiàn)了ScopedModelDriven,則這個攔截器會從相應(yīng)的Scope中取出model調(diào)用Action的setModel方法將其放入Action內(nèi)部。
Parameters Interceptorparams將請求中的參數(shù)設(shè)置到Action中去。
Prepare Interceptorprepare如果Acton實現(xiàn)了Preparable,則該攔截器調(diào)用Action類的prepare方法。
Scope Interceptorscope將Action狀態(tài)存入session和application的簡單方法。
Servlet Config InterceptorservletConfig提供訪問HttpServletRequest和HttpServletResponse的方法,以Map的方式訪問。
Static Parameters InterceptorstaticParams從struts.xml文件中將中的中的內(nèi)容設(shè)置到對應(yīng)的Action中。
Roles Interceptorroles確定用戶是否具有JAAS指定的Role,否則不予執(zhí)行。
Timer Interceptortimer輸出Action執(zhí)行的時間
Token Interceptortoken通過Token來避免雙擊
Token Session InterceptortokenSession和Token Interceptor一樣,不過雙擊的時候把請求的數(shù)據(jù)存儲在Session中
Validation Interceptorvalidation使用action-validation.xml文件中定義的內(nèi)容校驗提交的數(shù)據(jù)。
Workflow Interceptorworkflow調(diào)用Action的validate方法,一旦有錯誤返回,重新定位到INPUT畫面
Parameter Filter InterceptorN/A從參數(shù)列表中刪除不必要的參數(shù)
Profiling Interceptorprofiling通過參數(shù)激活profile

配置攔截器

定義攔截器

<package name="MyInterceptor" extends="struts-default" namespace="/">  
    <interceptors>  
        <!-- 配置攔截器 -->  
        <interceptor name="攔截器名" class="攔截器實現(xiàn)類完整路徑"/>  
        <!-- 配置攔截器棧 -->  
        <interceptor-stack name="攔截器棧名">  
             <interceptor-ref name="攔截器一"/>  
             <interceptor-ref name="攔截器二"/>  
            |..................................  
        </interceptor-stack>  
    </interceptors>  
</package>  

將攔截器配置到action

<action name="login" class="com.action.LoginAction">  
    <result name="success">/success.jsp</result>  
    <result name="error">/error.jsp</result>  
    <!-- 引用攔截器,一般配置在result后面 -->    
    <interceptor-ref name="攔截器名或攔截器棧名"/>  
    <!-- 引用Struts默認(rèn)攔截器 -->  
    <interceptor-ref name="defaultStack"/>  
</action>  

需要注意的是,必須要引用Struts默認(rèn)的攔截器,否則會報錯。

實現(xiàn)自定義攔截器:

通過實現(xiàn)接口com.opensymphony.xwork2.interceptor.Interceptor可自定義攔截器。該接口提供了三個方法:

1) void init(); 在該攔截器被初始化之后,在該攔截器執(zhí)行攔截之前,系統(tǒng)回調(diào)該方法。對于每個攔截器而言,此方法只執(zhí)行一次。

2) void destroy();該方法跟init()方法對應(yīng)。在攔截器實例被銷毀之前,系統(tǒng)將回調(diào)該方法。

3) String intercept(ActionInvocation invocation) throws Exception; 該方法是用戶需要實現(xiàn)的攔截動作。該方法會返回一個字符串作為邏輯視圖。

除此之外,通過繼承類com.opensymphony.xwork2.interceptor.AbstractInterceptor等方法也可自定義攔截器,這里不過多介紹。

攔截器實例

自定義攔截器MyInterceptor,實現(xiàn)Interceptor接口

package com.interceptor;  

import java.util.Map;  

import com.opensymphony.xwork2.ActionInvocation;  
import com.opensymphony.xwork2.interceptor.Interceptor;  
import com.sun.org.apache.xml.internal.security.keys.content.RetrievalMethod;  

public class MyInterceptor implements Interceptor {  

    @Override  
    public void destroy() {  
        System.out.println("---------destroy()---------");  
    }  

    @Override  
    public void init() {  
        System.out.println("---------init()---------");  
    }  

    @Override  
    public String intercept(ActionInvocation invocation) throws Exception {  
        System.out.println("---------intercept()---------");  
        Map session = invocation.getInvocationContext().getSession();  
        if (session.get("username") != null) {  
            return invocation.invoke();  
        } else {  
            return "login";  
        }  

    }  

}  

攔截器的配置

<?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>  
    <constant name="struts.action.extension" value=","></constant>  
    <package name="testLogin" namespace="/" extends="struts-default"  >  
        <interceptors>  

            <interceptor name="MyInterceptor" class="com.interceptor.MyInterceptor"></interceptor>  

            <interceptor-stack name="defaultInterceptorStack">  
                <interceptor-ref name="MyInterceptor"></interceptor-ref>  
                <interceptor-ref name="defaultStack"></interceptor-ref>  
            </interceptor-stack>  
        </interceptors>  

        <!--配置默認(rèn)攔截器,所有該包內(nèi)的action如果沒有單獨配置攔截器,則默認(rèn)執(zhí)行默認(rèn)攔截器-->  
        <default-interceptor-ref name="defaultInterceptorStack"></default-interceptor-ref>  

        <action name="login" class="com.action.LoginAction">  
            <result name="success" type="redirect">/success.jsp</result>  
            <result name="error" type="redirect">/error.jsp</result>  
            <result name="login">login.jsp</result>  
        </action>  
    </package>  
</struts>  

進入系統(tǒng)的時候,攔截器檢查是否登錄,如未登錄,轉(zhuǎn)到登錄頁;如已登錄,轉(zhuǎn)到成功頁。

攔截器是Struts 2比較重要的一個功能。通過正確地使用攔截器,我們可以編寫可復(fù)用性很高的代碼。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號