Struts2拦截器、拦截器栈(Interceptor Stack)、全局拦截器与方法拦截器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YEN_CSDN/article/details/52103233

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;
    }
}
阅读更多

没有更多推荐了,返回首页