SpringMVC interceptor source code analysis

Go directly to the source code

// doDispatch method  
 // 1. Preprocessing of processor interceptors (positive order execution)   
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, record the index of the current successful preprocessing   
}  
}  
// 2. The handler adapter calls our handler   
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());  
 // The default view name translation when we return null or no logical view name (detailed 4.15 .5 RequestToViewNameTranslator)   
if (mv != null && ! mv.hasView()) {  
    mv.setViewName(getDefaultViewName(request));  
}  
// 3. Post-processing of processor interceptors (reverse order)   
if (interceptors != null ) {  
 for ( int i = interceptors.length - 1; i >= 0; i-- ) {  
      HandlerInterceptor interceptor = interceptors[i];  
      interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);  
}  
}  
// 4. View rendering   
if (mv != null && ! mv.wasCleared()) {  
render(mv, processedRequest, response);  
    if (errorView) {  
        WebUtils.clearErrorRequestAttributes(request);  
}  
// 5. Trigger the callback method afterCompletion after the entire request is processed   
triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null );

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325227129&siteId=291194637