国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

SSH Struts2攔截器

2018-09-28 18:55 更新

Struts2 攔截器

什么是攔截器

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

實(shí)現(xiàn)原理

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



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

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

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

配置攔截器

定義攔截器

<package name="MyInterceptor" extends="struts-default" namespace="/">  
    <interceptors>  
        <!-- 配置攔截器 -->  
        <interceptor name="攔截器名" class="攔截器實(shí)現(xiàn)類(lèi)完整路徑"/>  
        <!-- 配置攔截器棧 -->  
        <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)的攔截器,否則會(huì)報(bào)錯(cuò)。

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

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

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

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

3) String intercept(ActionInvocation invocation) throws Exception; 該方法是用戶(hù)需要實(shí)現(xiàn)的攔截動(dòng)作。該方法會(huì)返回一個(gè)字符串作為邏輯視圖。

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

攔截器實(shí)例

自定義攔截器MyInterceptor,實(shí)現(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如果沒(méi)有單獨(dú)配置攔截器,則默認(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>  

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

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

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)