Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解
實(shí)現(xiàn)自定義攔截器
在實(shí)際的項(xiàng)目開(kāi)發(fā)中,雖然 Struts2 的內(nèi)建攔截器可以完成大部分的攔截任務(wù),但是,一些與系統(tǒng)邏輯相關(guān)的通用功能(如權(quán)限的控制和用戶(hù)登錄控制等),則需要通過(guò)自定義攔截器實(shí)現(xiàn)。
1.實(shí)現(xiàn)Interceptor接口
在 Struts2 框架中,通常開(kāi)發(fā)人員所編寫(xiě)的自定義攔截器類(lèi)都會(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í)例被銷(xiāo)毀之前,將調(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 的引用,可以通過(guò)該參數(shù)的 invoke() 方法,將控制權(quán)轉(zhuǎn)給下一個(gè)攔截器或者轉(zhuǎn)給 Action 的 execute() 方法。
2.繼承抽象類(lèi)AbstractInterceptor
AbstractIntercepter 類(lèi)實(shí)現(xiàn)了 Interceptor 接口,并且提供了 init() 方法和 destroy() 方法的空實(shí)現(xiàn)。使用時(shí),可以直接繼承該抽象類(lèi),而不用實(shí)現(xiàn)那些不必要的方法。AbstractInterceptor 類(lèi)中定義的方法如下所示:
public abstract class AbstractInterceptor implements Interceptor{
public void init(){}
public void destroy(){}
public abstract String intercept (ActionInvocation invocation) throws Exception;
}AbstractInterceptor 類(lèi)已經(jīng)實(shí)現(xiàn)了 Interceptor 接口的所有方法,一般情況下,只需繼承 AbstractInterceptor 類(lèi),實(shí)現(xiàn) interceptor() 方法就可以創(chuàng)建自定義攔截器。
需要注意的是,只有當(dāng)自定義的攔截器需要打開(kāi)系統(tǒng)資源時(shí),才需要覆蓋 AbstractInterceptor 類(lèi)的 init() 方法和 destroy() 方法。
與實(shí)現(xiàn) Interceptor 接口相比,繼承 AbstractInterceptor 類(lèi)的方法更為簡(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 方法過(guò)濾攔截器
//功能: 定制攔截器攔截的方法.
// 定制哪些方法需要攔截.
// 定制哪些方法不需要攔截
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)文章
myBatis組件教程之緩存的實(shí)現(xiàn)與使用
這篇文章主要給大家介紹了關(guān)于myBatis組件教程之緩存的實(shí)現(xiàn)與使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
SpringBoot淺析緩存機(jī)制之Redis單機(jī)緩存應(yīng)用
在上文中我介紹了Spring Boot使用EhCache 2.x來(lái)作為緩存的實(shí)現(xiàn),本文接著介紹使用單機(jī)版的Redis作為緩存的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
SpringBoot項(xiàng)目中使用Jasypt實(shí)現(xiàn)配置文件敏感信息加密
這篇文章主要介紹了如何使用Jasypt庫(kù)對(duì)SpringBoot項(xiàng)目中的敏感信息進(jìn)行加密和解密,首先,生成加密密文,然后在SpringBoot配置文件中集成Jasypt,實(shí)現(xiàn)自動(dòng)加密和解密,最后,強(qiáng)調(diào)了加密密鑰的安全配置,需要的朋友可以參考下2026-05-05
SpringBoot多環(huán)境打包與配置文件排除實(shí)踐記錄
本文介紹了SpringBoot項(xiàng)目多環(huán)境打包與配置文件排除實(shí)踐,包括多環(huán)境配置的實(shí)現(xiàn)方法、打包時(shí)排除配置文件的方法以及動(dòng)態(tài)加載外部配置文件的最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2024-11-11
Spring Boot中使用JSR-303實(shí)現(xiàn)請(qǐng)求參數(shù)校驗(yàn)
這篇文章主要介紹了Spring Boot中使用JSR-303實(shí)現(xiàn)請(qǐng)求參數(shù)校驗(yàn),JSR-303校驗(yàn)我們一般都是對(duì)Java的實(shí)體類(lèi)對(duì)象進(jìn)行校驗(yàn),主要檢驗(yàn)JSR-303是Java中的一個(gè)規(guī)范,用于實(shí)現(xiàn)請(qǐng)求參數(shù)校驗(yàn)在我們的實(shí)體類(lèi)對(duì)象的屬性上,感興趣的朋友跟隨小編一起看看吧2023-10-10
一次線(xiàn)上websocket返回400問(wèn)題排查的實(shí)戰(zhàn)記錄
最近項(xiàng)目中有端對(duì)端通信場(chǎng)景,實(shí)時(shí)性要求較高,考慮后選用了websocket 這一通信協(xié)議,下面這篇文章主要給大家介紹了一次線(xiàn)上websocket返回400問(wèn)題排查的實(shí)戰(zhàn)記錄,需要的朋友可以參考下2022-04-04
sftp和ftp 根據(jù)配置遠(yuǎn)程服務(wù)器地址下載文件到當(dāng)前服務(wù)
這篇文章主要介紹了sftp和ftp 根據(jù)配置遠(yuǎn)程服務(wù)器地址下載文件到當(dāng)前服務(wù)的相關(guān)資料本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Java實(shí)現(xiàn)汽車(chē)租賃系統(tǒng)
這篇文章介紹了Java實(shí)現(xiàn)汽車(chē)租賃系統(tǒng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Mapper類(lèi)中存在名稱(chēng)相同的方法重載報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了Mapper類(lèi)中存在名稱(chēng)相同的方法重載報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

