Struts2實現(xiàn)自定義攔截器的三種方式詳解
實現(xiàn)自定義攔截器
在實際的項目開發(fā)中,雖然 Struts2 的內建攔截器可以完成大部分的攔截任務,但是,一些與系統(tǒng)邏輯相關的通用功能(如權限的控制和用戶登錄控制等),則需要通過自定義攔截器實現(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)建后會立即被調用,它在攔截器的生命周期內只被調用一次。可以在該方法中對相關資源進行必要的初始化。
2)void destroy() 該方法與 init() 方法相對應,在攔截器實例被銷毀之前,將調用該方法釋放和攔截器相關的資源,它在攔截器的生命周期內,也只被調用一次。
3)String intercept(ActionInvocation invocation)throws Exception
該方法是攔截器的核心方法,用于添加真正執(zhí)行攔截工作的代碼,實現(xiàn)具體的攔截操作,它返回一個字符串作為邏輯視圖,系統(tǒng)根據返回的字符串跳轉到對應的視圖資源。每攔截一個動作請求,該方法就會被調用一次。
該方法的 ActionInvocation 參數包含了被攔截的 Action 的引用,可以通過該參數的 invoke() 方法,將控制權轉給下一個攔截器或者轉給 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 類已經實現(xiàn)了 Interceptor 接口的所有方法,一般情況下,只需繼承 AbstractInterceptor 類,實現(xiàn) interceptor() 方法就可以創(chuà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;
}
}到此這篇關于Struts2實現(xiàn)自定義攔截器的三種方式詳解的文章就介紹到這了,更多相關Struts2自定義攔截器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot項目中使用Jasypt實現(xiàn)配置文件敏感信息加密
這篇文章主要介紹了如何使用Jasypt庫對SpringBoot項目中的敏感信息進行加密和解密,首先,生成加密密文,然后在SpringBoot配置文件中集成Jasypt,實現(xiàn)自動加密和解密,最后,強調了加密密鑰的安全配置,需要的朋友可以參考下2026-05-05
SpringBoot多環(huán)境打包與配置文件排除實踐記錄
本文介紹了SpringBoot項目多環(huán)境打包與配置文件排除實踐,包括多環(huán)境配置的實現(xiàn)方法、打包時排除配置文件的方法以及動態(tài)加載外部配置文件的最佳實踐,感興趣的朋友跟隨小編一起看看吧2024-11-11
Spring Boot中使用JSR-303實現(xiàn)請求參數校驗
這篇文章主要介紹了Spring Boot中使用JSR-303實現(xiàn)請求參數校驗,JSR-303校驗我們一般都是對Java的實體類對象進行校驗,主要檢驗JSR-303是Java中的一個規(guī)范,用于實現(xiàn)請求參數校驗在我們的實體類對象的屬性上,感興趣的朋友跟隨小編一起看看吧2023-10-10
一次線上websocket返回400問題排查的實戰(zhàn)記錄
最近項目中有端對端通信場景,實時性要求較高,考慮后選用了websocket 這一通信協(xié)議,下面這篇文章主要給大家介紹了一次線上websocket返回400問題排查的實戰(zhàn)記錄,需要的朋友可以參考下2022-04-04

