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

Struts2拦截器原理
Struts2拦截器是在访问某个Action或Action的方法之前或之后实施拦截。在请求Struts2的Action时,Struts2会查找配置文件,并根据配置文件实例化相应的拦截器对象。


Struts2拦截器配置
struts.xml





<struts>
    <package name="default" extends="struts-default">
        
        <interceptors>
            <interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor">
                
                <param name="value" >YENparam>
            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>
            
            <interceptor-ref name="defaultStack">interceptor-ref>
            <result name="success">success.jspresult>
        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>

Struts2拦截器、拦截器栈(Interceptor Stack)、全局拦截器与方法拦截器_第1张图片
Struts2拦截器、拦截器栈(Interceptor Stack)、全局拦截器与方法拦截器_第2张图片

由此也可以看出,拦截器执行的顺序与我们在配置文件中定义的顺序是一支的,即由外到内,而结束时则是从内到外。


拦截器栈

拦截器栈(Interceptor Stack)。Struts2拦截器栈就是将拦截器按一定的顺序连接成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用。

可以把上面的struts.xml配置文件格式修改为下述,这样大大提高了Action的简洁性:





<struts>
    <package name="default" extends="struts-default">
        
        <interceptors>
            <interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor">
                <param name="value" >YENparam>
            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.jspresult>
        action>


    package>
struts>

Struts2拦截器、拦截器栈(Interceptor Stack)、全局拦截器与方法拦截器_第3张图片
执行结果是一样的。


全局拦截器





<struts>
    <package name="default" extends="struts-default">
        
        <interceptors>
            <interceptor name="FirstInterceptor" class="Interceptor.FirstInterceptor">
                <param name="value" >YENparam>
            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.jspresult>
        global-results>

        
        <default-action-ref name="LoginAction">default-action-ref>

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


    package>
struts>

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





<struts>
    <package name="default" extends="struts-default">
        
        <interceptors>
            
            <interceptor name="MethodInterceptor" class="Interceptor.MethodFirstceptor">
                
                
                <param name="includeMethods">add,deleteparam>
                
                <param name="excludeMethods">updateparam>
        interceptors>

        <action name="LoginAction" class="Action.LoginAction">
            <interceptor-ref name="MethodInterceptor">interceptor-ref>
            <interceptor-ref name="defaultStack">interceptor-ref>
            <result name="success">success.jspresult>
        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;
    }
}

你可能感兴趣的:(struts2.0,拦截器,拦截器栈,方法拦截器,全局拦截器,----【SSM框架】,JavaEE专业技能)