struts2中拦截器的使用
攔截器的使用 實現(xiàn)AOP
轉(zhuǎn)自http://www.cnblogs.com/fmricky/archive/2010/05/24/1742514.html
1、什么是攔截器(Interceptor)
攔截器是動態(tài)攔截Action調(diào)用的對象。它提供了一種機制,使開發(fā)者可以定義一段代碼,在Action執(zhí)行之前或之后被調(diào)用執(zhí)行,也可以在一個Action執(zhí)行前阻止其執(zhí)行,同時也可以提取Action中可重用部分的方式。通俗一點說,攔截器是一個實現(xiàn)了一定功能的類,它以一種可插拔的方式被定義在某個Action執(zhí)行的之前或之后,用來完成特定的功能。
?
2、攔截器的結(jié)構(gòu)
先來看下攔截器的聲明周期
圖中,我們可以發(fā)現(xiàn),Struts2的Interceptor一層一層,把Action包裹在最里面。這樣的結(jié)構(gòu),大概有以下一些特點:
- 中止整個執(zhí)行,直接返回一個字符串作為resultCode
- 通過遞歸調(diào)用負責(zé)調(diào)用堆棧中下一個Interceptor的執(zhí)行
- 如果在堆棧內(nèi)已經(jīng)不存在任何的Interceptor,調(diào)用Action
?
3、如何使用攔截器
1、Struts 2內(nèi)置攔截器的介紹
2、部署攔截器
為了能夠正常使用攔截器,首先我們必須在struts.xml中正確部署攔截器。具體的做法為在<interceptor></interceptor>標(biāo)簽對內(nèi)使用<interceptor/>標(biāo)簽引入具體攔截器,需要指定攔截器的名稱和類:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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> ????<package?name="wwfy" extends="struts-default"> ????????<interceptors> ????????????<interceptor?name="攔截器名稱1" class="攔截器類1" /> ????????????<interceptor?name="攔截器名稱2" class="攔截器類2" /> ????????????……………………………………………………………………………………………………………………………………………… ????????????<interceptor?name="攔截器名稱N" class="攔截器類N" /> ????????</interceptors> ????????? ????????<!--省略Action配置信息--> ????</package> </struts> |
?
3、為Action應(yīng)用添加攔截器配置
部署好攔截器之后,我們就可以利用攔截器為指定的Action應(yīng)用添加功能了。具體的做法是在<action></action>標(biāo)簽對內(nèi)使用<interceptor-ref/>標(biāo)簽引入所需的攔截器:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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> ????<package?name="wwfy" extends="struts-default"> ????????<!--省略攔截器配置信息--> ????????? ????????<action?name="Action名" class="Action類"> ????????????<!--省略Action結(jié)果配置信息--> ????????????<interceptor-ref?name="defaultStack"/> ????????????<interceptor-ref?name="攔截器名1"/> ????????????<interceptor-ref?name="攔截器名2"/> ????????????…………………………………………………………………………………………………… ????????????<interceptor-ref?name="攔截器名N"/> ????????</action> ????</package> </struts> |
?
當(dāng)我們單獨為某個Action單獨配置攔截器的時候,系統(tǒng)默認的攔截器將會失效。此時為了Action仍然能夠順利執(zhí)行,我們一般要通過<interceptor-ref name="defaultStack"/>手動引入defaultStack。
?
4、攔截器參數(shù)設(shè)置
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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> ????<package?name="wwfy" extends="struts-default"> ????????<interceptors> ????????????<interceptor?name="攔截器名稱" class="攔截器類" > ????????????????<param?name="參數(shù)名">參數(shù)值</param> ????????????</interceptor> ????????</interceptors> ????????? ????????<action?name="Action名" class="Action類"> ????????????<!--省略Action結(jié)果配置信息--> ????????????<interceptor-ref?name="攔截器名"> ????????????????<param?name="參數(shù)名">參數(shù)值</param> ????????????</interceptor-ref> ????????</action>???? ????</package> </struts> |
?
4、如何使用攔截器棧
1、部署攔截器棧
一個攔截器棧可以包括一個或者多個攔截器,也可以包括其他攔截器棧。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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> ????<package?name="wwfy" extends="struts-default"> ????????<interceptors> ????????????<interceptor?name="攔截器名稱1" class="攔截器類1" /> ????????????<interceptor?name="攔截器名稱2" class="攔截器類2" /> ????????????……………………………………………………………………………………………………………………………………………… ????????????<interceptor?name="攔截器名稱N" class="攔截器類N" /> ????????????? ????????????<interceptor-stack?name="攔截器棧1"> ????????????????<interceptor-ref?name="攔截器名稱1"/> ????????????????<interceptor-ref?name="攔截器名稱2"/> ????????????????<interceptor-ref?name="攔截器名稱N"/> ????????????</interceptor-stack> ????????????? ????????????<interceptor-stack?name="攔截器棧2"> ????????????????<interceptor-ref?name="攔截器棧1"/> ????????????????<interceptor-ref?name="攔截器名稱3"/> ????????????</interceptor-stack> ????????</interceptors>?? ????</package> </struts> |
?
2、為Action應(yīng)用添加攔截器棧配置
這部分和攔截器的配置一樣,可通過<interceptor-ref/>標(biāo)簽引入
?
5、如何自定義攔截器
自定義攔截器有2種實現(xiàn)方式,一種是實現(xiàn)Interceptor接口,另一種是繼承AbstractInterceptor類
1、實現(xiàn)Interceptor接口
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package?wwfy.interceptor; import?com.opensymphony.xwork2.ActionInvocation; import?com.opensymphony.xwork2.interceptor.Interceptor; @SuppressWarnings("serial") public?class?CustomInterceptor1 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("--------攔截器開始----------"); ????????String result = invocation.invoke(); ????????System.out.println("--------攔截器結(jié)束----------"); ????????return?result; ????} } |
?
2、繼承AbstractInterceptor類
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package?wwfy.interceptor; import?com.opensymphony.xwork2.ActionInvocation; import?com.opensymphony.xwork2.interceptor.AbstractInterceptor; @SuppressWarnings("serial") public?class?CustomInterceptor2 extends?AbstractInterceptor { ????@Override ????public?String intercept(ActionInvocation invocation) throws?Exception { ????????System.out.println("--------攔截器開始----------"); ????????String result = invocation.invoke(); ????????System.out.println("--------攔截器結(jié)束----------"); ????????return?result; ????} } |
?
3、invocation.invoke()的作用
在這個實現(xiàn)類中,實際上已經(jīng)實現(xiàn)了最簡單的攔截器的雛形。或許大家對這樣的代碼還比較陌生,這沒有關(guān)系。我在這里需要指出的是一個很重要的方法invocation.invoke()。這是ActionInvocation中的方法,而ActionInvocation是Action調(diào)度者,所以這個方法具備以下2層含義:
所以,我們可以發(fā)現(xiàn),invocation.invoke()這個方法其實是整個攔截器框架的實現(xiàn)核心。基于這樣的實現(xiàn)機制,我們還可以得到下面2個非常重要的推論:
由此,我們就可以通過invocation.invoke()作為Action代碼真正的攔截點,從而實現(xiàn)AOP。
?
4、攔截器的執(zhí)行順序
定義中有這樣一個攔截器棧
| 1 2 3 4 5 6 | <interceptor-stack?name="xaStack">?? ??<interceptor-ref?name="thisWillRunFirstInterceptor"/>?? ??<interceptor-ref?name="thisWillRunNextInterceptor"/>?? ??<interceptor-ref?name="followedByThisInterceptor"/>?? ??<interceptor-ref?name="thisWillRunLastInterceptor"/>?? </interceptor-stack> |
?
整個攔截器棧的執(zhí)行順序為
轉(zhuǎn)載于:https://www.cnblogs.com/wwicked/articles/4455467.html
總結(jié)
以上是生活随笔為你收集整理的struts2中拦截器的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 2962 最短路+二分
- 下一篇: ArcGIS学习记录—KMZ KML与S