Struts拦截器

原创链接:http://my.oschina.net/chape/blog/136406

浅谈Struts2拦截器的原理与实现

拦截器与过滤器 能做什么,怎么做?

     很多业务逻辑都得靠拦截来实现,比如校验验证登录权限等等。拦截器是对调用的Action起作用,它提供了一种机制可以使开发者定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式

     过滤器是对整个的请求过程起作用。过滤器是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 strutsaction前统一设置字符集,或者去除掉一些非法字符(聊天室经常用到的,一些骂人的话,比如判断用户提交的数据是否存在非法字符等等。

一、理解Struts2拦截器

1. Struts2拦截器是在访问某个ActionAction的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现。

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

二、实现Struts2拦截器原理

      Struts2拦截器的实现原理相对简单,当请求struts2action时,Struts2会查找配置文件,并根据其配置实例化相对应的拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器。

三、定义Struts2拦截器

      Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口。该接口声明了3个方法

1 void init();

2 void destroy();

3 String intercept(ActionInvocation invocation) throws Exception;

      不过,struts中又提供了几个抽象类来简化这一步骤。其中,initdestroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。

intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑。

1 public abstract class AbstractInterceptor implements Interceptor;

2 public abstract class MethodFilterInterceptor extends AbstractInterceptor;

      其中AbstractInterceptor提供了init()destroy()的空实现,使用时只需要覆盖intercept()方法;都是模板方法实现的;而MethodFilterInterceptor则提供了includeMethodsexcludeMethods两个属性,用来过滤执行该过滤器的action的方法。可以通过param来加入或者排除需要过滤的方法。

 

一般来说,拦截器的写法都差不多。看下面的示例:

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

public class MyInterceptor implements Interceptor {
 public void destroy() {
 // TODO Auto-generated method stub
 }
 public void init() {
 // TODO Auto-generated method stub
 }
 public String intercept(ActionInvocation invocation) throws Exception {
     System.out.println("Action执行前插入 代码");     
 //执行目标方法 (调用下一个拦截器, 或执行Action)   
     final String res = invocation.invoke();   
     System.out.println("Action执行后插入 代码");   
     return res;   
 }


Struts2拦截器需要在struts.xml中声明,如下struts.xml配置文件,配置Struts2拦截器:

 <?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>
 <constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default">
   <interceptors>
    <interceptor name="MyInterceptor" class="interceptor.MyInterceptor"></interceptor>
    <interceptor-stack name="myInterceptorStack">
     <interceptor-ref name="MyInterceptor"/>
     <interceptor-ref name="defaultStack"/>
   </interceptor-stack>
  </interceptors>
 <action name="loginAction">
   <result name="fail">/index.jsp </result>
   <result name="success">/success.jsp</result>
   <interceptor-ref name="myInterceptorStack"></interceptor-ref>
 </action>
 </package>
 </struts>

spring 拦截器实现方式

原创链接:http://my.oschina.net/qicheng/blog/168085


你可能感兴趣的:(Struts拦截器)