成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Struts2教程:實現自已的攔截器

開發 后端
本文為Struts2教程,本部分教你如何實現自已的攔截器。Struts2雖然在大版本號上是第二個版本,但基本上在配置和使用上已經完全顛覆了Struts1.x的方式。

在上一篇中介紹了Struts2攔截器的原理,在這一篇中我們將學習一下如何編寫自己的攔截器。

一、攔截器的實現

實現一個攔截器非常簡單。實際上,一個攔截器就是一個普通的類,只是這個類必須實現com.opensymphony.xwork2.interceptor.Interceptor接口。Interceptor接口有如下三個方法:

  1. public interface Interceptor extends Serializable   
  2. {  
  3.     void destroy();  
  4.     void init();  
  5.     String intercept(ActionInvocation invocation) throws Exception;  

其中init和destroy方法只在攔截器加載和釋放(都由Struts2自身處理)時執行一次。而intercept方法在每次訪問動作時都會被調用。Struts2在調用攔截器時,每個攔截器類只有一個對象實例,而所有引用這個攔截器的動作都共享這一個攔截器類的對象實例,因此,在實現Interceptor接口的類中如果使用類變量,要注意同步問題。

下面我們來實現一個簡單的攔截器,這個攔截器通過請求參數action指定一個攔截器類中的方法,并調用這個方法(我們可以使用這個攔截器對某一特定的動作進行預處理)。如果方法不存在,或是action參數不存在,則繼續執行下面的代碼。如下面的URL:

http://localhost:8080/struts2/test/interceptor.action?action=test

訪問上面的url后,攔截器會就會調用攔截器中的test方法,如果這個方法不存在,則調用invocation.invoke方法,invoke方法和Servlet過濾器中調用FilterChain.doFilter方法類似,如果在當前攔截器后面還有其他的攔截器,則invoke方法就是調用后面攔截器的intercept方法,否則,invoke會調用Action類的execute方法(或其他的執行方法)。

下面我們先來實現一個攔截器的父類ActionInterceptor。這個類主要實現了根據action參數值來調用方法的功能,代碼如下:

  1. package interceptor;  
  2.  
  3. import com.opensymphony.xwork2.ActionInvocation;  
  4. import com.opensymphony.xwork2.interceptor.Interceptor;  
  5. import javax.servlet.http.*;  
  6. import org.apache.struts2.*;  
  7.  
  8. public class ActionInterceptor implements Interceptor  
  9. {  
  10.     protected final String INVOKE = "##invoke";  
  11.      
  12.     public void destroy()  
  13.     {  
  14.         System.out.println("destroy");  
  15.     }  
  16.  
  17.     public void init()  
  18.     {  
  19.         System.out.println("init");  
  20.     }  
  21.  
  22.     public String intercept(ActionInvocation invocation) throws Exception  
  23.     {      
  24.         HttpServletRequest request = ServletActionContext.getRequest();  
  25.         String action = request.getParameter("action");  
  26.         System.out.println(this.hashCode());  
  27.         if (action != null)  
  28.         {  
  29.             try 
  30.             {  
  31.                 java.lang.reflect.Method method = this.getClass().getMethod(action);  
  32.                 String result = (String)method.invoke(this);  
  33.                 if(result != null)  
  34.                 {  
  35.                     if(!result.equals(INVOKE))  
  36.                         return result;  
  37.                 }  
  38.                 else 
  39.                     return null;  
  40.             }  
  41.             catch (Exception e)  
  42.             {  
  43.             }  
  44.         }  
  45.         return invocation.invoke();  
  46.     }  
  47. }  

從上面代碼中的intercept方法可以看出,在調用action所指定的方法后,來判斷返回值。可能發生的情況有三種:

1. 返回值為null,執行return null。

2. 返回值為INVOKE,執行return invockation.invoke()。

3. 其他情況,執行return result。 result表示指定方法的返回值,如上面代碼所示。

在實現完上面的攔截器父類后,任何繼承于ActionInterceptor類的攔截器都可以自動根據action的參數值調用自身的相應方法。下面我們來實現一個擁有兩個動作方法test和print的攔截器類。代碼如下:

  1. package interceptor;  
  2.  
  3. import javax.servlet.http.HttpServletResponse;  
  4. import org.apache.struts2.ServletActionContext;  
  5.  
  6. public class MultiMethodInterceptor extends ActionInterceptor  
  7. {  
  8.     public String test() throws Exception  
  9.     {  
  10.         HttpServletResponse response = ServletActionContext.getResponse();  
  11.         response.getWriter().println("invoke test");  
  12.         return this.INVOKE;  
  13.     }  
  14.  
  15.     public String print() throws Exception  
  16.     {  
  17.         HttpServletResponse response = ServletActionContext.getResponse();  
  18.         response.getWriter().println("invoke print");  
  19.  
  20.         return null;  
  21.     }  
  22. }  

test方法返回了INVOKE,因此,在執行完這個方法后,Struts2會接著調用其他攔截器的intercept方法或Action類的execute方法。而print方法在執行完后,只是返回了null,而不再調用其他的方法了,也就是訪問如下的url時,動作的execute方法將不會執行:

http://localhost:8080/struts2/test/ddd.action?action=print

下面我們來實現一個Action類,代碼如下:

  1. package action;  
  2.  
  3. import org.apache.struts2.*;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5.  
  6. public class InterceptorAction extends ActionSupport  
  7. {  
  8.     public String abcd() throws Exception  
  9.     {  
  10.         ServletActionContext.getResponse().getWriter()  
  11.                 .println("invoke abcd");  
  12.         return null;  
  13.     }  
  14. }  

在這個Action類中,只有一個abcd方法,實際上,這個方法相當于execute方法,在下面會設置動作的method屬性為abcd。下面我們來在struts.xml中定義攔截器類和動作,代碼如下:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. < struts> 
  6.     < package name="demo" extends="struts-default" namespace="/test"> 
  7.         < interceptors> 
  8.             < interceptor name="method" class="interceptor.MultiMethodInterceptor" /> 
  9.                 < interceptor-stack name="methodStack"> 
  10.                     < interceptor-ref name="method" /> 
  11.                     < interceptor-ref name="defaultStack" /> 
  12.                 < /interceptor-stack> 
  13.         < /interceptors> 
  14.  
  15.         < action name="interceptor" class="action.InterceptorAction" method="abcd"> 
  16.             < interceptor-ref name="methodStack" /> 
  17.         < /action> 
  18.     < /package> 
  19. < /struts> 

在配置上面的methodStack攔截器時要注意,***在后面引用defaultStack,否則很多通過攔截器提供的功能將失去。

OK,現在訪問如下的URL:

http://localhost:8080/struts2/test/ddd.action?action=test

在瀏覽器中將會出現如下的字符串:

invoke test

invoke abcd

而如果訪問http://localhost:8080/struts2/test/ddd.action?action=print,將會只出現如下的字符串:

invoke print

大家可以看出,訪問這個url時并沒有調用abcd方法。如果隨便指定的action值的話,則只調用abcd方法,如訪問http://localhost:8080/struts2/test/ddd.action?action=aaa,就只會輸出invoke abcd。

二、攔截器的參數

我們在使用很多Struts2內置的攔截器時會發現有很多攔截器都帶參數,當然。我們自己做的攔截器也可以加上同樣的參數。有兩個參數比較常用,這兩個參數是includeMethods和excludeMethods,其中includeMethods指定了攔截器要調用的Action類的執行方法(默認是execute),也就是說,只有在includeMethods中指定的方法才會被Struts2調用,而excludeMethods恰恰相反,在這個參數中指定的執行方法不會被Struts2調用。如果有多個方法,中間用逗號(,)分隔。在Struts2中提供了一個抽象類來處理這兩個參數。這個類如下:

com.opensymphony.xwork2.interceptor.MethodFilterInterceptor

如有繼承于這個類的攔截器類都會自動處理includeMethods和excludeMethods參數,如下面的攔截器類所示:

  1. package interceptor;  
  2.  
  3. import com.opensymphony.xwork2.ActionInvocation;  
  4. import com.opensymphony.xwork2.interceptor.*;  
  5.  
  6. public class MyFilterInterceptor extends MethodFilterInterceptor  
  7. {  
  8.     private String name;  
  9.     public String getName()  
  10.     {  
  11.         return name;  
  12.     }  
  13.     public void setName(String name)  
  14.     {  
  15.         this.name = name;  
  16.     }  
  17.     @Override 
  18.     protected String doIntercept(ActionInvocation invocation) throws Exception  
  19.     {  
  20.         System.out.println("doIntercept");  
  21.         System.out.println(name);  
  22.         return invocation.invoke();  
  23.     }  
  24. }  

MethodFilterInterceptor的子類需要實現doIntercept方法(相當于Interceptor的intercept方法),如上面代碼所示。在上面的代碼中還有一個name屬性,是為了讀取攔截器的name屬性而設置的,如下面的配置代碼所示:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. < struts> 
  6.     < package name="demo" extends="struts-default" namespace="/test"> 
  7.         < interceptors> 
  8.             < interceptor name="method" class="interceptor.MultiMethodInterceptor" /> 
  9.                 < interceptor name="filter" 
  10.                     class="interceptor.MyFilterInterceptor"> 
  11.                     < param name="includeMethods">abcd< /param> 
  12.                     < param name="name">中國< /param> 
  13.                 < /interceptor> 
  14.                 < interceptor-stack name="methodStack"> 
  15.                     < interceptor-ref name="method" /> 
  16.                     < interceptor-ref name="filter" /> 
  17.                     < interceptor-ref name="defaultStack" /> 
  18.                 < /interceptor-stack> 
  19.         < /interceptors> 
  20.  
  21.         < action name="interceptor" class="action.InterceptorAction" method="abcd"> 
  22.             < interceptor-ref name="methodStack" /> 
  23.         < /action> 
  24.     < /package> 
  25. < /struts> 

再次訪問http://localhost:8080/struts2/test/ddd.action?action=test, Struts2就會調用MyFilterInterceptor的doIntercept方法來輸出name屬性值。如果將上面的includeMethods參數值中的abcd去掉,則Action類的abcd方法不會被執行。

【編輯推薦】

  1. Struts2教程:攔截器概述
  2. Struts2教程:上傳任意多個文件
  3. Struts2教程:在Action類中獲得HttpServletResponse對象
  4. Struts2教程:使用Validation框架驗證數據
  5. Struts2教程:使用validate方法驗證數據
責任編輯:yangsai 來源: BlogJava
相關推薦

2009-02-04 14:45:06

2009-06-25 15:54:42

Struts2教程攔截器

2009-02-04 14:19:38

2009-06-04 08:01:25

Struts2攔截器原理

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 10:51:07

2009-06-25 16:04:30

2009-06-03 14:19:34

Struts2Guice

2009-06-25 15:26:25

Struts2教程struts.xml常

2011-04-28 09:52:04

Struts2

2009-02-04 15:04:13

2009-06-25 15:50:03

Struts2教程上傳任意多個文件

2010-01-06 14:36:04

JSON插件

2009-07-29 09:54:34

struts2和str

2009-02-04 11:37:15

2009-02-04 14:00:59

2009-06-25 15:33:12

Struts2教程使用validate驗證數據

2009-06-25 15:37:12

Struts2教程Validation框

2012-04-25 10:14:40

JavaStruts

2009-07-08 17:02:11

JDK實現調用攔截器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品久久久久久久久久久久久 | 成人精品高清 | 青青草精品视频 | 神马久久av | 欧美日韩精品一区二区三区四区 | 亚洲精品久久国产高清情趣图文 | 亚洲国产精品网站 | 免费看片在线播放 | 亚洲精品欧美 | 欧美成人免费在线 | 亚洲精品1区2区3区 91免费看片 | 999热精品 | 我爱操| 91精品免费视频 | 国产视频一区二区 | 黄色毛片免费看 | 日本黄色片免费在线观看 | 丁香色婷婷 | 日韩a在线 | 国产综合精品一区二区三区 | 国产精品区二区三区日本 | 99久久精品国产毛片 | 成年免费大片黄在线观看岛国 | 亚洲国产网站 | 日本大片在线播放 | 国产高清一区二区三区 | 国产一区二区三区色淫影院 | 中文字幕乱码视频32 | 日韩欧美中文在线 | 秋霞a级毛片在线看 | av中文天堂 | 免费一级淫片aaa片毛片a级 | 久久久久国产成人精品亚洲午夜 | 国产高清在线精品 | 欧美日韩国产精品一区 | 国产极品91 | 99精品久久 | 一级a毛片 | 九九久久这里只有精品 | 中文字幕第三页 | 91精品国产综合久久久久 |