1. 程式人生 > >Struts2攔截器、攔截器棧(Interceptor Stack)、全域性攔截器與方法攔截器

Struts2攔截器、攔截器棧(Interceptor Stack)、全域性攔截器與方法攔截器

Struts2攔截器原理
Struts2攔截器是在訪問某個Action或Action的方法之前或之後實施攔截。在請求Struts2的Action時,Struts2會查詢配置檔案,並根據配置檔案例項化相應的攔截器物件。

Struts2攔截器配置
struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="default" extends="struts-default"> <!--配置攔截器--> <interceptors> <interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor"> <!--初始化--> <param name="value" >
YEN</param> </interceptor> <interceptor name="SecondInterceptor" class="Interceptor.SecondInterceptor"> </interceptor> </interceptors> <action name="LoginAction" class="Action.LoginAction"> <!--加攔截器-->
<interceptor-ref name="FirstInterceptor"></interceptor-ref> <interceptor-ref name="SecondInterceptor"></interceptor-ref> <!--在使用攔截器的時候,在Action裡面必須最後一定要引用struts2自帶的攔截器預設堆疊defaultStack--> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="success">success.jsp</result> </action> </package> </struts>

FirstInterceptor

package Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * Created with IntelliJ IDEA.
 * User: YEN
 * Date: 2016/8/3
 * Time: 13:59
 */
public class FirstInterceptor implements Interceptor {
    private String value;
    /**
     * 銷燬方法
     */
    @Override
    public void destroy() {
        System.out.println("First攔截器銷燬了");
    }

    /**
     * 初始化方法 當程式啟動時攔截器就被初始化了
     */
    @Override
    public void init() {
        System.out.println("First攔截器初始化了");
    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        //actionInvocation.invoke() 讓到達攔截器的請求繼續前進 訪問需要訪問的資源(就是放過的意思)
        System.out.println("進入First攔截器");
        String returnName=actionInvocation.invoke();
        System.out.println("走出First攔截器");
        return returnName;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

SecondInterceptor

package Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * Created with IntelliJ IDEA.
 * User: YEN
 * Date: 2016/8/3
 * Time: 14:14
 */
public class SecondInterceptor implements Interceptor {
    @Override
    public void destroy() {
        System.out.println("Second攔截器銷燬了");
    }

    @Override
    public void init() {
        System.out.println("Second攔截器初始化了");
    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        //actionInvocation.invoke() 讓到達攔截器的請求繼續前進 訪問需要訪問的資源(就是放過的意思)
        System.out.println("進入Second攔截器");
        String returnName=actionInvocation.invoke();
        System.out.println("走出Second攔截器");
        return returnName;
    }
}

index.jsp

  <a href="LoginAction.action">點選呼叫攔截器</a>

這裡寫圖片描述
這裡寫圖片描述

由此也可以看出,攔截器執行的順序與我們在配置檔案中定義的順序是一支的,即由外到內,而結束時則是從內到外。

攔截器棧

攔截器棧(Interceptor Stack)。Struts2攔截器棧就是將攔截器按一定的順序連線成一條鏈。在訪問被攔截的方法或欄位時,Struts2攔截器鏈中的攔截器就會按其之前定義的順序被呼叫。

可以把上面的struts.xml配置檔案格式修改為下述,這樣大大提高了Action的簡潔性:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" extends="struts-default">
        <!--配置攔截器-->
        <interceptors>
            <interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor">
                <param name="value" >YEN</param>
            </interceptor>
            <interceptor name="SecondInterceptor" class="Interceptor.SecondInterceptor"></interceptor>

            <interceptor-stack name="AllInterceptor">
                <interceptor-ref name="FirstInterceptor"></interceptor-ref>
                <interceptor-ref name="SecondInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <action name="LoginAction" class="Action.LoginAction">
            <interceptor-ref name="AllInterceptor"></interceptor-ref>
            <result name="success">success.jsp</result>
        </action>


    </package>
</struts>

這裡寫圖片描述
執行結果是一樣的。

全域性攔截器

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" extends="struts-default">
        <!--配置攔截器-->
        <interceptors>
            <interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor">
                <param name="value" >YEN</param>
            </interceptor>
            <interceptor name="SecondInterceptor" class="Interceptor.SecondInterceptor"></interceptor>

            <interceptor-stack name="AllInterceptor">
                <interceptor-ref name="FirstInterceptor"></interceptor-ref>
                <interceptor-ref name="SecondInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <!--預設攔截器(全域性攔截器)-->
        <default-interceptor-ref name="AllInterceptor" ></default-interceptor-ref>

        <!--全域性返回頁面-->
        <global-results>
            <result>error.jsp</result>
        </global-results>

        <!--全域性Action-->
        <default-action-ref name="LoginAction"></default-action-ref>

        <action name="LoginAction" class="Action.LoginAction">
            <interceptor-ref name="AllInterceptor"></interceptor-ref>
            <result name="success">success.jsp</result>
        </action>


    </package>
</struts>

方法攔截器
eg:攔截add和delete方法

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" extends="struts-default">
        <!--配置攔截器-->
        <interceptors>
            <!--方法攔截器配置-->
            <interceptor name="MethodInterceptor" class="Interceptor.MethodFirstceptor">
                <!--配置引數 :攔截什麼方法-->
                <!--includeMethods控制能訪問哪些-->
                <param name="includeMethods">add,delete</param>
                <!--excludeMethods控制能訪問哪些-->
                <param name="excludeMethods">update</param>
        </interceptors>

        <action name="LoginAction" class="Action.LoginAction">
            <interceptor-ref name="MethodInterceptor"></interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">success.jsp</result>
        </action>


    </package>
</struts>
package Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * Created with IntelliJ IDEA.
 * User: YEN
 * Date: 2016/8/3
 * Time: 14:42
 */

/**
 * 方法攔截器 針對性非常強
 */
public class MethodFirstceptor extends MethodFilterInterceptor {
    @Override
    protected String doIntercept(ActionInvocation aInvocation) throws Exception {
        System.out.println("進入方法攔截器");
        String str=aInvocation.invoke();
        System.out.println("退出方法攔截器");
        return str;
    }
}