Struts2實現自定義攔截器的三種方式詳解
實現自定義攔截器
在實際的項目開發(fā)中,雖然 Struts2 的內建攔截器可以完成大部分的攔截任務,但是,一些與系統(tǒng)邏輯相關的通用功能(如權限的控制和用戶登錄控制等),則需要通過自定義攔截器實現。
1.實現Interceptor接口
在 Struts2 框架中,通常開發(fā)人員所編寫的自定義攔截器類都會直接或間接地實現 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)建后會立即被調用,它在攔截器的生命周期內只被調用一次??梢栽谠摲椒ㄖ袑ο嚓P資源進行必要的初始化。
2)void destroy() 該方法與 init() 方法相對應,在攔截器實例被銷毀之前,將調用該方法釋放和攔截器相關的資源,它在攔截器的生命周期內,也只被調用一次。
3)String intercept(ActionInvocation invocation)throws Exception
該方法是攔截器的核心方法,用于添加真正執(zhí)行攔截工作的代碼,實現具體的攔截操作,它返回一個字符串作為邏輯視圖,系統(tǒng)根據返回的字符串跳轉到對應的視圖資源。每攔截一個動作請求,該方法就會被調用一次。
該方法的 ActionInvocation 參數包含了被攔截的 Action 的引用,可以通過該參數的 invoke() 方法,將控制權轉給下一個攔截器或者轉給 Action 的 execute() 方法。
2.繼承抽象類AbstractInterceptor
AbstractIntercepter 類實現了 Interceptor 接口,并且提供了 init() 方法和 destroy() 方法的空實現。使用時,可以直接繼承該抽象類,而不用實現那些不必要的方法。AbstractInterceptor 類中定義的方法如下所示:
public abstract class AbstractInterceptor implements Interceptor{ public void init(){} public void destroy(){} public abstract String intercept (ActionInvocation invocation) throws Exception; }
AbstractInterceptor 類已經實現了 Interceptor 接口的所有方法,一般情況下,只需繼承 AbstractInterceptor 類,實現 interceptor() 方法就可以創(chuàng)建自定義攔截器。
需要注意的是,只有當自定義的攔截器需要打開系統(tǒng)資源時,才需要覆蓋 AbstractInterceptor 類的 init() 方法和 destroy() 方法。
與實現 Interceptor 接口相比,繼承 AbstractInterceptor 類的方法更為簡單。
3.繼承MethodFilterInterceptor
MethodFilterInterceptor提供了一個doIntercept方法供我們實現攔截器功能。
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; } }
到此這篇關于Struts2實現自定義攔截器的三種方式詳解的文章就介紹到這了,更多相關Struts2自定義攔截器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于Spring不同類型的注入方式 p-namespace,c-namespace
這篇文章主要介紹了Spring不同類型的注入方式 p-namespace,c-namespace。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot自定義FailureAnalyzer過程解析
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11如何解決java.util.zip.ZipFile解壓后被java占用問題
這篇文章主要介紹了如何解決java.util.zip.ZipFile解壓后被java占用問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06