模拟实现struts2原理

Struts2 中的拦截器和 servelt 中的过滤器是非常的相似的。如果学过过滤器的话,肯定能够感觉的到,尽管有些微的不同。可是struts2的拦截器到底如何使用呢,为什么会有这些配置呢? 接下来一一来看。 

过滤器和拦截器是非常相似的,过滤器 public interface Filter 接口里面有三个方法: 

  • init(FilterConfig filterConfig),
  • destroy(),
  • doFilter(ServletRequest request, ServletResponse response, FilterChain chain),

 

这里面的 doFilter() 方法是最重要的,在 struts2 中  String intercept(ActionInvocation invocation)就相当于此方法。 

如何完成一个拦截器呢?在 struts2 中要实现一个接口 这个接口是什么呢?在哪呢?,是否在哪听说过?是 webwork 是我们以前听的最多的关于拦截器的框架, struts2 用了其中一个核心的东西,这个东西在是什么呢?是 xwork 。恩,有了它才可以拦截,好了我们在哪找呢?在 com.opensymphony.xwork2.interceptor 中找,里面有个 Interceptor 这是个接口,里面也有三个方法,有 init, destroy 和 intercept 三个方法,而在 struts2 里面的所有的拦截器都继承这个接口! 

 

struts2接受客户端请求时,在执行action之前已经执行了自身默认的拦截器,这些拦截器完成struts2的大部分初始化的功能,比如设置属性值等。

 

执行完这些拦截器后,再执行action,返回客户端时,还得经过拦截器的拦截,最终返回客户端。下面我们开始模拟拦截器的实现过程:

 

1、新建自己的拦截器:

 

  1. package com.beckham.interceptors;  
  2. /** 
  3.  * @author Owner 
  4.  *  Jan 14, 2010   2:36:14 PM 
  5.  *  定义一个接口 
  6.  */  
  7. public interface MyInterceptor {  
  8.     public void inteceptor(Invocation invocation) ;  
  9. }  
 

 

2、拦截器的具体实现

 

  1. package com.beckham.interceptors;  
  2. /** 
  3.  *  @author Owner 
  4.  *  Jan 14, 2010   2:41:26 PM 
  5.  *   
  6.  *  spring 
  7.  *  com.beckham.interceptors 
  8.  *  FirstInterceptor.java 
  9.  */  
  10. public class FirstInterceptor implements MyInterceptor {  
  11.     public void inteceptor(Invocation invocation) {  
  12.         System.out.println("第一个拦截器开始。。。。");  
  13.         //调用invoke方法   
  14.         invocation.invoke() ;  
  15.         System.out.println("第一个拦截器结束.......");  
  16.     }  
  17. }  
 

 

  1. package com.beckham.interceptors;  
  2. /** 
  3.  *  @author Owner 
  4.  *  Jan 14, 2010   2:42:22 PM 
  5.  *   
  6.  *  spring 
  7.  *  com.beckham.interceptors 
  8.  *  SecondInterceptor.java 
  9.  */  
  10. public class SecondInterceptor implements MyInterceptor {  
  11.     public void inteceptor(Invocation invocation) {  
  12.         System.out.println("第二个拦截器开始.....");  
  13.         //调用invoke方法   
  14.         invocation.invoke() ;  
  15.         System.out.println("第二个拦截器结束......");  
  16.     }  
  17. }  
 

 

3、需要执行的action

 

  1. package com.beckham.interceptors;  
  2. /** 
  3.  *  @author Owner 
  4.  *  Jan 14, 2010   2:45:30 PM 
  5.  *   
  6.  *  spring 
  7.  *  com.beckham.interceptors 
  8.  *  Action.java 
  9.  */  
  10. public class Action {  
  11.     public String execute() {  
  12.         System.out.println("执行execute方法");  
  13.         return null ;  
  14.     }  
  15. }  
 

 

4、控制器,该控制器调配执行拦截器和action

 

  1. package com.beckham.interceptors;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. /** 
  5.  *  @author Owner 
  6.  *  Jan 14, 2010   2:46:44 PM 
  7.  *   
  8.  *  spring 
  9.  *  com.beckham.interceptors 
  10.  *  Invocation.java 
  11.  */  
  12. public class Invocation {  
  13.     List<MyInterceptor> interceptors = new ArrayList<MyInterceptor>();  
  14.     int index = -1;  
  15.     public Invocation() {  
  16.         //需要执行的拦截器   
  17.         this.interceptors.add(new FirstInterceptor());  
  18.         this.interceptors.add(new SecondInterceptor());  
  19.     }  
  20.     public void invoke() {  
  21.         index++;  
  22.         if (index < interceptors.size()) {  
  23.             //一次调用拦截器的inteceptor方法   
  24.             interceptors.get(index).inteceptor(this);  
  25.         } else {  
  26.             //拦截器拦截完毕后执行action   
  27.             new Action().execute();  
  28.         }  
  29.     }  
  30. }  
 

 

5、main方法执行,在struts2框架中,前台的一个请求被过滤器拦截后就执行拦截,然后再执行action

 

  1. package com.beckham.interceptors;  
  2. /** 
  3.  *  @author Owner 
  4.  *  Jan 14, 2010   2:47:32 PM 
  5.  *   
  6.  *  spring 
  7.  *  com.beckham.interceptors 
  8.  *  Test.java 
  9.  */  
  10. public class Test {  
  11.     public static void main(String[] args) {  
  12.         //struts2的拦截器入口就是invoke方法   
  13.         new Invocation().invoke() ;  
  14.     }  
  15. }  
 

 

 

模拟结果:

 

第一个拦截器开始。。。。

第二个拦截器开始.....

执行execute方法

第二个拦截器结束......

第一个拦截器结束.......

 

 

从模拟结果分析,拦截器相当于包裹在action外面的屏障,要达到执行action的目的,需要穿过屏障-------->执行action-------->穿出屏障

 

拦截器的实现其实是aop编程思想,在实现相应逻辑的前后,加上其他逻辑的实现,而且没有任何依赖关系。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章