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