Struts(二十八):自定义拦截器

  • Struts2拦截器

  1. 拦截器(Interceptor)是Struts2的核心部分。
  2. Struts2很多功能都是构建在拦截器基础之上,比如:文件上传、国际化、数据类型转化、数据校验等。
  3. Struts2拦截器是在访问某个Action方法之前和之后实施拦截的。
  4. Struts2拦截器是可插拔的,拦截器是AOP(面向切面编程)的一种实现。
  5. 拦截器栈(Interceptor Stack):将拦截器按一定的顺序联合在一条链,在访问被拦截的方法时,Struts2拦截器栈中的拦截器就会按照之前定义的顺序被调用。
  • Interceptor接口

每个拦截器都必须实现com.opensymphony.xwork2.interceptor.Interceptor接口

package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import java.io.Serializable;

public interface Interceptor extends Serializable {

    /**
     * Called to let an interceptor clean up any resources it has allocated.
     */
    void destroy();

    /**
     * Called after an interceptor is created, but before any requests are processed using
     * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
     * the Interceptor a chance to initialize any needed resources.
     */
    void init();

    /**
     * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
     * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
     *
     * @param invocation the action invocation
     * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
     * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
     */
    String intercept(ActionInvocation invocation) throws Exception;

}
  1. Struts2会依次调用为某个Action而注册的每个拦截器的intercept方法
  2. 每次调用intercept方法时,Struts2会传递一个ActionInvocation接口的实例。
  3. ActionInvocation:代表一个给定Action的执行状态,拦截器可以从该类的对象里获的与该Action相关的Action对象和Result对象。在完成拦截器自己的任务之后,拦截器调用ActionInvocation对象的invoke方法前进到Action处理流程的下一个环节。
  4. AbstractInterceptor类实现了Interceptor接口,并为init,destory提供了空白的实现。
  • 用法示例:

 定义一个PermissionInterceptor拦截器

package com.dx.struts2.interceptor;

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

public class PremissionInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 1L;

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        System.out.println("Before execute ActionInvocation.invoke()");
        String result = actionInvocation.invoke();
        System.out.println("After execute ActionInvocation.invoke()");

        return result;
    }
}

在struts.xml中声明拦截器并在testToken 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>
    <constant name="struts.custom.i18n.resources" value="i18n"></constant>
    <package name="default" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="premission"
                class="com.dx.struts2.interceptor.PremissionInterceptor"></interceptor>
        </interceptors>

        <action name="testToken" class="com.dx.struts2.actions.MemberAction">
            <interceptor-ref name="premission"></interceptor-ref>
            <interceptor-ref name="tokenSession"></interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result>/success.jsp</result>
        </action>
    </package>
</struts>

 注意事项:

如果不调用String result = actionInvocation.invoke();或者返回的结果不是调用 actionInvocation.invoke()结果(比如:return "success";),那么,后续的拦截器及Action将不会被调用,而是直接去渲染result.

根据上边的特性,我们可以在实际项目中实现权限控制等操作。

原文地址:https://www.cnblogs.com/yy3b2007com/p/6699663.html