Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解
實(shí)現(xiàn)自定義攔截器
在實(shí)際的項(xiàng)目開發(fā)中,雖然 Struts2 的內(nèi)建攔截器可以完成大部分的攔截任務(wù),但是,一些與系統(tǒng)邏輯相關(guān)的通用功能(如權(quán)限的控制和用戶登錄控制等),則需要通過自定義攔截器實(shí)現(xiàn)。
1.實(shí)現(xiàn)Interceptor接口
在 Struts2 框架中,通常開發(fā)人員所編寫的自定義攔截器類都會(huì)直接或間接地實(shí)現(xiàn) com.opensymphony.xwork2.interceptor.Interceptor 接口。Interceptor 接口中的主要代碼如下所示:
public interface Interceptor extends Serializable{ void init(); void destroy(); String intercept(ActionInvocation invocation) throws Exception; }
從上述代碼中可以看出,該接口共提供了以下三個(gè)方法。
1)void init() 該方法在攔截器被創(chuàng)建后會(huì)立即被調(diào)用,它在攔截器的生命周期內(nèi)只被調(diào)用一次??梢栽谠摲椒ㄖ袑?duì)相關(guān)資源進(jìn)行必要的初始化。
2)void destroy() 該方法與 init() 方法相對(duì)應(yīng),在攔截器實(shí)例被銷毀之前,將調(diào)用該方法釋放和攔截器相關(guān)的資源,它在攔截器的生命周期內(nèi),也只被調(diào)用一次。
3)String intercept(ActionInvocation invocation)throws Exception
該方法是攔截器的核心方法,用于添加真正執(zhí)行攔截工作的代碼,實(shí)現(xiàn)具體的攔截操作,它返回一個(gè)字符串作為邏輯視圖,系統(tǒng)根據(jù)返回的字符串跳轉(zhuǎn)到對(duì)應(yīng)的視圖資源。每攔截一個(gè)動(dòng)作請(qǐng)求,該方法就會(huì)被調(diào)用一次。
該方法的 ActionInvocation 參數(shù)包含了被攔截的 Action 的引用,可以通過該參數(shù)的 invoke() 方法,將控制權(quán)轉(zhuǎn)給下一個(gè)攔截器或者轉(zhuǎn)給 Action 的 execute() 方法。
2.繼承抽象類AbstractInterceptor
AbstractIntercepter 類實(shí)現(xiàn)了 Interceptor 接口,并且提供了 init() 方法和 destroy() 方法的空實(shí)現(xiàn)。使用時(shí),可以直接繼承該抽象類,而不用實(shí)現(xiàn)那些不必要的方法。AbstractInterceptor 類中定義的方法如下所示:
public abstract class AbstractInterceptor implements Interceptor{ public void init(){} public void destroy(){} public abstract String intercept (ActionInvocation invocation) throws Exception; }
AbstractInterceptor 類已經(jīng)實(shí)現(xiàn)了 Interceptor 接口的所有方法,一般情況下,只需繼承 AbstractInterceptor 類,實(shí)現(xiàn) interceptor() 方法就可以創(chuàng)建自定義攔截器。
需要注意的是,只有當(dāng)自定義的攔截器需要打開系統(tǒng)資源時(shí),才需要覆蓋 AbstractInterceptor 類的 init() 方法和 destroy() 方法。
與實(shí)現(xiàn) Interceptor 接口相比,繼承 AbstractInterceptor 類的方法更為簡(jiǎn)單。
3.繼承MethodFilterInterceptor
MethodFilterInterceptor提供了一個(gè)doIntercept方法供我們實(shí)現(xiàn)攔截器功能。
public abstract class MethodFilterInterceptor extends AbstractInterceptor {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E--> /** * Subclasses must override to implement the interceptor logic. * * @param invocation the action invocation * @return the result of invocation * @throws Exception */ protected abstract String doIntercept(ActionInvocation invocation) throws Exception; } public abstract class MethodFilterInterceptor extends AbstractInterceptor { /** * Subclasses must override to implement the interceptor logic. * * @param invocation the action invocation * @return the result of invocation * @throws Exception */ protected abstract String doIntercept(ActionInvocation invocation) throws Exception; }
示例:
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; //繼承:MethodFilterInterceptor 方法過濾攔截器 //功能: 定制攔截器攔截的方法. // 定制哪些方法需要攔截. // 定制哪些方法不需要攔截 public class MyInterceptor3 extends MethodFilterInterceptor{ @Override protected String doIntercept(ActionInvocation invocation) throws Exception { //前處理 System.out.println("MyInterceptor3 的前處理!"); //放行 String result = invocation.invoke(); //后處理 System.out.println("MyInterceptor3 的后處理!"); return result; } }
到此這篇關(guān)于Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解的文章就介紹到這了,更多相關(guān)Struts2自定義攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Spring不同類型的注入方式 p-namespace,c-namespace
這篇文章主要介紹了Spring不同類型的注入方式 p-namespace,c-namespace。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09RocketMQ設(shè)計(jì)之主從復(fù)制和讀寫分離
這篇文章主要介紹了RocketMQ設(shè)計(jì)之主從復(fù)制和讀寫分離,RocketMQ提高消費(fèi)避免Broker發(fā)生單點(diǎn)故障引起B(yǎng)roker上的消息無法及時(shí)消費(fèi),下文關(guān)于了RocketMQ的相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-03-03SpringBoot自定義FailureAnalyzer過程解析
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09Maven本地倉(cāng)庫的配置以及修改默認(rèn).m2倉(cāng)庫位置
今天小編就為大家分享一篇關(guān)于Maven本地倉(cāng)庫的配置以及修改默認(rèn).m2倉(cāng)庫位置的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10SpringBoot如何IDEA中實(shí)現(xiàn)熱部署
這篇文章主要介紹了SpringBoot如何IDEA中實(shí)現(xiàn)熱部署,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04如何解決java.util.zip.ZipFile解壓后被java占用問題
這篇文章主要介紹了如何解決java.util.zip.ZipFile解壓后被java占用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06