欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解SpringMVC的攔截器參數(shù)及攔截器鏈配置

 更新時(shí)間:2022年07月05日 10:16:46   作者:夏志121  
攔截器(Interceptor)是一種動(dòng)態(tài)攔截方法調(diào)用的機(jī)制,在SpringMVC中動(dòng)態(tài)攔截控制器方法的執(zhí)行。本文將詳細(xì)講講SpringMVC中攔截器參數(shù)及攔截器鏈配置,感興趣的可以嘗試一下

一、攔截器參數(shù)

前置處理

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle...");
        return true;
    }

參數(shù):

■ request:請(qǐng)求對(duì)象

■ response:響應(yīng)對(duì)象

■ handler:被調(diào)用的處理器對(duì)象,本質(zhì)上是一個(gè)方法對(duì)象,對(duì)反射技術(shù)中的Method對(duì)象進(jìn)行再包裝

返回值:

■ 返回值為false,被攔截的處理器將不執(zhí)行

后置處理

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
    }

參數(shù):

modeAanView:如果處理器執(zhí)行完成具有返回結(jié)果,可以讀取到對(duì)應(yīng)數(shù)據(jù)與頁(yè)面信息,并進(jìn)行調(diào)整

完成后處理

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }

參數(shù):

ex:如果處理器執(zhí)行過(guò)程中出現(xiàn)異常對(duì)象,可以針對(duì)異常情況進(jìn)行單獨(dú)處理

二、攔截器鏈配置

配置第一個(gè)攔截器

@Component
//定義攔截器類,實(shí)現(xiàn)HandlerInterceptor接口
//注意當(dāng)前類必須受Spring容器控制
public class ProjectInterceptor implements HandlerInterceptor {
    @Override
    //原始方法調(diào)用前執(zhí)行的內(nèi)容
    //返回值類型可以攔截控制的執(zhí)行,true放行,false終止
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String contentType = request.getHeader("Content-Type");
        HandlerMethod hm = (HandlerMethod)handler;
        System.out.println("preHandle..."+contentType);
        return true;
    }
 
    @Override
    //原始方法調(diào)用后執(zhí)行的內(nèi)容
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
    }
 
    @Override
    //原始方法調(diào)用完成后執(zhí)行的內(nèi)容
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }
}

 配置第二個(gè)攔截器

@Component
public class ProjectInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle...222");
        return true;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...222");
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...222");
    }
}

當(dāng)配置多個(gè)攔截器時(shí),形成攔截器鏈

@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc
//實(shí)現(xiàn)WebMvcConfigurer接口可以簡(jiǎn)化開(kāi)發(fā),但具有一定的侵入性
public class SpringMvcConfig implements WebMvcConfigurer {
    @Autowired
    private ProjectInterceptor projectInterceptor;
    @Autowired
    private ProjectInterceptor2 projectInterceptor2;
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置多攔截器
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
        registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");
    }
}

 三個(gè)攔截器構(gòu)成的攔截器鏈圖例

攔截器鏈的運(yùn)行規(guī)律參照攔截器添加順序?yàn)闇?zhǔn)

當(dāng)攔截器中出現(xiàn)對(duì)原始處理器的攔截,后面的攔截器均終止運(yùn)行

當(dāng)攔截器運(yùn)行中斷,僅運(yùn)行配置在前面的攔截器的afterCompletion操作

以上就是詳解SpringMVC的攔截器參數(shù)及攔截器鏈配置的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC攔截器配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論