java中的Struts2攔截器詳解
最近在學(xué)習(xí)struts的攔截器,現(xiàn)在來(lái)總結(jié)一下。
1、攔截器是什么?
攔截器相當(dāng)于過(guò)濾器:就是將不想要的去掉,想要的留下。攔截器抽象出一部分代碼可以用來(lái)完善原來(lái)的action。同時(shí)可以減輕代碼冗余,提高重用率。通俗地講就是一張網(wǎng),過(guò)濾掉不需要的沙子,留下水。
2、攔截器的作用:
攔截器可以構(gòu)成特定的功能。比如權(quán)限認(rèn)證、日志記錄和登陸判斷。
3、攔截器的原理:

其每一個(gè)Action請(qǐng)求都在攔截器中,每一個(gè)action可以將操作轉(zhuǎn)交給下面的攔截器,也可以直接退出到界面上。
4、定義攔截器:
(1)自定義一個(gè)實(shí)現(xiàn)Interceptor接口(不過(guò)我初學(xué)者一般直接實(shí)現(xiàn)框架中的Interceptor)
(2)在struts.xml中注冊(cè)定義的攔截器
(3)可以需要的action中引用攔截器
Interceptor接口聲明了三個(gè)方法
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
Init方法是在action作用之前調(diào)用,就是開(kāi)始給爛機(jī)器的初始化操作。
Destory方法在攔截器被垃圾回收之前調(diào)用,用來(lái)回收init方法初始化的資源。
interceptor方法是攔截器的主要操作。如果需要調(diào)用后續(xù)的Action或者攔截器,只需要在該方法中調(diào)用invocation.invoke()方法即可,在該方法調(diào)用的前后可以插入Action調(diào)用前后攔截器需要做的方法。
現(xiàn)在對(duì)用戶(hù)登錄進(jìn)行攔截,代碼如下:
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("在action執(zhí)行之前");
ActionContext actionContext=invocation.getInvocationContext();
Map<String,Object> session=actionContext.getSession();
Object currentUser=session.get("currentUser");
String result=null;
if(currentUser!=null){
result=invocation.invoke();
}else{
HttpServletRequest request=(HttpServletRequest)invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
request.setAttribute("error", "請(qǐng)先登錄");
result="error";
}
System.out.println("result+"+result);
System.out.println("在action執(zhí)行之后");
return result;
}
注冊(cè)攔截器:
<interceptors>
<interceptor name="myInterceptor"
class="com.fangchao.interceptor.MyInterceptor"></interceptor>
<interceptor name="loginInterceptor"
class="com.fangchao.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
上述代碼中的interceptor-stack是個(gè)攔截器棧。到目前為止,就是在下面引用時(shí),比較方便。一般來(lái)講,每個(gè)action都會(huì)使用defaultStack。
攔截器參數(shù):
配置參數(shù):
- excludeMethods:過(guò)濾掉不使用攔截器的方法
- includeMethods:使用攔截器的方法。
有兩種配置方式:
<interceptor-ref name="validation"> <param name="excludeMethods">myValidationExcudeMethod</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>
或者
<interceptor-ref name="defaultStack"> <param name="validation.excludeMethods">myValidationExcludeMethod</param> <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何基于java語(yǔ)言實(shí)現(xiàn)八皇后問(wèn)題
這篇文章主要介紹了如何基于java語(yǔ)言實(shí)現(xiàn)八皇后問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
詳解Springboot之Logback的使用學(xué)習(xí)
Logback是SpringBoot內(nèi)置的日志處理框架,你會(huì)發(fā)現(xiàn)spring-boot-starter其中包含了spring-boot-starter-logging,該依賴(lài)內(nèi)容就是Spring Boot默認(rèn)的日志框架logback,本文詳細(xì)介紹了該框架 ,需要的朋友可以參考下2021-05-05
Springboot詳解RocketMQ實(shí)現(xiàn)廣播消息流程
RocketMQ作為一款純java、分布式、隊(duì)列模型的開(kāi)源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時(shí)消息、消息回溯等,本篇我們了解如何實(shí)現(xiàn)廣播消息2022-06-06
SpringBoot中如何對(duì)actuator進(jìn)行關(guān)閉
這篇文章主要介紹了SpringBoot中如何對(duì)actuator進(jìn)行關(guān)閉問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
如何解決創(chuàng)建maven工程時(shí),產(chǎn)生“找不到插件的錯(cuò)誤”問(wèn)題
這篇文章主要介紹了如何解決創(chuàng)建maven工程時(shí),產(chǎn)生“找不到插件的錯(cuò)誤”問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
springboot結(jié)合redis實(shí)現(xiàn)搜索欄熱搜功能及文字過(guò)濾
本文主要介紹了springboot結(jié)合redis實(shí)現(xiàn)搜索欄熱搜功能及文字過(guò)濾,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能
本文主要介紹了springboot結(jié)合mybatis-plus基于session模擬短信注冊(cè)功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
@RequestParam 接收參數(shù)的值為null的處理方式
這篇文章主要介紹了@RequestParam 接收參數(shù)的值為null的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
idea hibernate jpa 生成實(shí)體類(lèi)的實(shí)現(xiàn)
這篇文章主要介紹了idea hibernate jpa 生成實(shí)體類(lèi)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

