springMVC 拦截器源码解析

直接上源码

//doDispatch方法  
//1、处理器拦截器的预处理(正序执行)  
HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();  
if (interceptors != null) {  
    for (int i = 0; i < interceptors.length; i++) {  
    HandlerInterceptor interceptor = interceptors[i];  
        if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {  
            //1.1、失败时触发afterCompletion的调用  
            triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);  
            return;  
        }  
        interceptorIndex = i;//1.2、记录当前预处理成功的索引  
}  
}  
//2、处理器适配器调用我们的处理器  
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());  
//当我们返回null或没有返回逻辑视图名时的默认视图名翻译(详解4.15.5 RequestToViewNameTranslator)  
if (mv != null && !mv.hasView()) {  
    mv.setViewName(getDefaultViewName(request));  
}  
//3、处理器拦截器的后处理(逆序)  
if (interceptors != null) {  
for (int i = interceptors.length - 1; i >= 0; i--) {  
      HandlerInterceptor interceptor = interceptors[i];  
      interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);  
}  
}  
//4、视图的渲染  
if (mv != null && !mv.wasCleared()) {  
render(mv, processedRequest, response);  
    if (errorView) {  
        WebUtils.clearErrorRequestAttributes(request);  
}  
//5、触发整个请求处理完毕回调方法afterCompletion  
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null); 

 

posted @ 2018-05-01 22:41  灬Silence灬  阅读(113)  评论(0编辑  收藏  举报