Struts2 预定义拦截器&拦截器栈 和自定义拦截器简单实例

Struts2 拦截器是在访问某个 Action 或 Action 的某个方法,字段之前或之后实施拦截,并且 Struts2 拦截器是可
插拔的,拦截器是AOP的一种实现.
优点:通用功能的封装,提供了可重用性;

二、Struts2预定义拦截器&拦截器栈

struts-default.xml中就有很多预定义的拦截器;

在执行action之前和之后,拦截器进行了操作;

拦截器栈:

interceptor-stack;

定义了一批拦截器;相当于把几个拦截器串起来;

struts-default.xml中定义了默认使用的拦截器栈:

<default-interceptor-ref name="defaultStack" />

三、自定义拦截器简单实例

com.cy.action.HelloAction.java:

package com.cy.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("执行了HelloAction的默认方法");
        return SUCCESS;
    }
    
}

com.cy.interceptor.MyInterceptor.java:

package com.cy.interceptor;

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

public class MyInterceptor implements Interceptor{
    
    //拦截器销毁的时候调用
    public void destroy() {
        System.out.println("MyInterceptor销毁");
    }
    
    //初始化的时候调用
    public void init() {
        System.out.println("MyInterceptor初始化");
    }
    
    //真正的调用
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("在Action执行之前");
        String result = invocation.invoke();    //这里就是success,error啊啥的
        System.out.println("在Action执行之后");
        return result;                            //这个result也是要返回的,给配置文件看的。比如action中返回的是success,这边也返回success
    }

}

struts.xml:

<?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>
    
    <package name="manage" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="myInterceptor" class="com.cy.interceptor.MyInterceptor"></interceptor>
        </interceptors>
    
        <action name="hello" class="com.cy.action.HelloAction">
            <result name="success">success.jsp</result>
            
            <!-- 系统看到我们引用了拦截器myInterceptor就不会引用其他的拦截器/栈了,因此加上默认的拦截器
                 myInterceptor相当于最外层的,defaultStack相当于最里层的,顺序是先进入myInterceptor再是defaultStack
             -->
            <interceptor-ref name="myInterceptor"></interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>
        
    </package>
</struts>

success.jsp:

<body>
    name: ${name}
</body>

测试结果:

启动tomcat,浏览器访问:http://localhost:8080/Struts2Chap01/hello?name=aaa

控制台打印结果:

系统运行的时候,通过读取配置文件,就有一个拦截器的初始化过程;

网址输入hello?name=aaa后,程序的执行过程:

1.进入MyInterceptor的intercept方法,打印:“在Action执行之前”;

2.执行HelloAction中的execute方法;执行完成;

3.MyInterceptor的intercept方法继续执行,打印:"在Action执行之后",返回结果;

猜你喜欢

转载自blog.csdn.net/qq_40135955/article/details/89084732