【struts2】自定义拦截器

  1)什么是自定义的拦截器

  所谓自定义的拦截器,就是由我们自己定义并实现的拦截器,而不是由Struts2定义好的拦截器。虽然Struts2的预定义拦截器已经满足了大多数情况的需要。但在有些时候,我们可能会根据项目的实际需要而自定义一些拦截器,来实现一些特别的功能。

  2)开发自定义拦截器

  其实在Struts2里面,要实现自定义的拦截器是非常简单的,只要写一个实现Interceptor接口的类就可以了。也就是说,所有的拦截器都要实现com.opensymphony.xwork2.interceptor.Interceptor接口,这个接口中定义如下:

public interface Interceptor extends Serializable {  
    void destroy();  
    void init();  
    String intercept(ActionInvocation invocation) throws Exception;  
}  

  方法的基本说明如下:

  • init方法就类似于构造方法,用于初始化一些相关资源
  • destory方法类似于析构方法,用于释放资源
  • intercept方法,就是拦截器执行的处理方法,我们要实现的功能主要就写在这个方法里面。

  对于intercept方法,再说明几点:

  (1)在intercept方法中写“invocation.invoke();”,这句话的意思是继续运行拦截器后续的处理,如果这个拦截器后面还有拦截器,那么会继续运行,一直到运行Action,然后执行Result。如果intercept方法中没有写“invocation.invoke();”这句话,那就意味着对请求的运行处理到此为止,不再继续向后运行了,换句话说,后续的拦截器和Action就不再执行了。而是在这里返回Result字符串,直接去进行Result处理了。

  (2)在“invocation.invoke();”这句话之前写的功能,会在Action运行之前执行

  (3)在“invocation.invoke();”这句话之后写的功能,会在Result运行之后执行

  (4)intercept方法的返回值就是最终要返回的Result字符串,这个只是在前面没有执行Result的时候才有效,也就是前面没有“invocation.invoke();”这句话的时候,这个返回值就相当于是最终要返回的Result字符串,然后才执行相应的Result处理。

  3)示例

  • 先来个最简单的,就是在Action运行之前,和Result运行之后输出一点信息,当然,有实际功能需求的时候,就写成实际功能的处理代码了,示例代码如下:
package cn.javass.hello.struts2impl.action;

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("在acton执行之前");  
        String result = invocation.invoke();  
        System.out.println("在Result运行之后");  
          
        return result;  
    }  
}  

  可以看到,这个Interceptor的init方法和destroy方法只是输出了一句信息,它的intercept方法用来执行响应,在“invocation.invoke();”这句话之前和之后分别输出了一句信息。最后返回的result,就是invocation.invoke()的返回值。

  • HelloWorldAction这个类不用修改
  • 需要到struts.xml里面配置拦截器的声明和引用,示例如下:
<package name="helloworld"  extends="struts-default">  
        <interceptors>  
            <interceptor name="testInteceptor" class="cn.javass.hello.struts2impl.action.MyInterceptor"/>  
            <interceptor-stack name="myStack">  
                <interceptor-ref name="timer"/>  
                <interceptor-ref name="testInteceptor"/>  
                <interceptor-ref name="defaultStack"/>  
            </interceptor-stack>  
        </interceptors>  
        <default-interceptor-ref name="myStack"/>  
        
        <global-results>  
            <result name="math-exception">/${folder}/error.jsp</result>  
        </global-results>  
        <global-exception-mappings>  
            <exception-mapping result="math-exception" exception="java.lang.ArithmeticException"/>  
            <exception-mapping result="math-exception" exception="java.lang.Exception"/>  
        </global-exception-mappings>  
        
        <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction"> 
            <result name="toWelcome">/${folder}/welcome.jsp</result> 
            <result name="input">/${folder}/login.jsp</result>   
        </action>  
    </package>  
  • 运行测试一下,后台输出:
在acton执行之前
用户输入的参数为===account=11,password=11111111111,submitFlag=login
在Result运行之后
2014-5-18 21:18:46 com.opensymphony.xwork2.interceptor.TimerInterceptor info
信息: Executed action [//helloworldAction!execute] took 152 ms.

  参考资料:http://www.iteye.com/topic/1124526

原文地址:https://www.cnblogs.com/ningvsban/p/3735714.html