首頁 > 軟體

詳解SpringMVC的攔截器引數及攔截器鏈設定

2022-07-05 14:05:43

一、攔截器引數

前置處理

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

引數:

■ request:請求物件

■ response:響應物件

■ handler:被呼叫的處理器物件,本質上是一個方法物件,對反射技術中的Method物件進行再包裝

返回值:

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

後置處理

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

引數:

modeAanView:如果處理器執行完成具有返回結果,可以讀取到對應資料與頁面資訊,並進行調整

完成後處理

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

引數:

ex:如果處理器執行過程中出現異常物件,可以針對異常情況進行單獨處理

二、攔截器鏈設定

設定第一個攔截器

@Component
//定義攔截器類,實現HandlerInterceptor介面
//注意當前類必須受Spring容器控制
public class ProjectInterceptor implements HandlerInterceptor {
    @Override
    //原始方法呼叫前執行的內容
    //返回值型別可以攔截控制的執行,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
    //原始方法呼叫後執行的內容
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
    }
 
    @Override
    //原始方法呼叫完成後執行的內容
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }
}

 設定第二個攔截器

@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");
    }
}

當設定多個攔截器時,形成攔截器鏈

@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc
//實現WebMvcConfigurer介面可以簡化開發,但具有一定的侵入性
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/*");
    }
}

 三個攔截器構成的攔截器鏈圖例

攔截器鏈的執行規律參照攔截器新增順序為準

當攔截器中出現對原始處理器的攔截,後面的攔截器均終止執行

當攔截器執行中斷,僅執行設定在前面的攔截器的afterCompletion操作

以上就是詳解SpringMVC的攔截器引數及攔截器鏈設定的詳細內容,更多關於SpringMVC攔截器設定的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com