模擬實現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編程思想,在實現相應邏輯的前後,加上其他邏輯的實現,而且沒有任何依賴關係。

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