一、Spring MVC 派发
1、 校验是否包含文件
2、查找处理器HandlerExecutionChain(包含实际处理controller及对应拦截器链)
3、查找处理适配器AnnotationMethodHandlerAdapter
4、拦截器预处理
5、调用处理器
6、视图转换
7、拦截器后处理
8、视图渲染后处理
9、文件资源清理(实际执行的是删除文件:如果保存在内存,则清除对应的内存文件内容;若保存在临时文件,则删除对应的临时文件)
protectedvoid doDispatch(HttpServletRequest request,HttpServletResponse response)throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler =null;
int interceptorIndex = -1;
try {
ModelAndView mv;
boolean errorView =false;
try {
1、校验是否包含文件
processedRequest = checkMultipart(request);
2、查找处理器HandlerExecutionChain(包含实际处理controller及对应拦截器链)
// Determinehandler for the current request.
mappedHandler = getHandler(processedRequest, false);
if (mappedHandler ==null || mappedHandler.getHandler() ==null) {
noHandlerFound(processedRequest, response);
return;
}
3、查找处理适配器AnnotationMethodHandlerAdapter
// Determinehandler adapter for the current request.
HandlerAdapter ha =getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet ="GET".equals(method);
if (isGet ||"HEAD".equals(method)) {
long lastModified = ha.getLastModified(request,mappedHandler.getHandler());
if (logger.isDebugEnabled()) {
String requestUri =urlPathHelper.getRequestUri(request);
logger.debug("Last-Modifiedvalue for [" + requestUri +"] is: " + lastModified);
}
if (new ServletWebRequest(request,response).checkNotModified(lastModified) && isGet) {
return;
}
}
// ApplypreHandle methods of registered interceptors.
4、拦截器预处理
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())) {
triggerAfterCompletion(mappedHandler,interceptorIndex, processedRequest, response,null);
return;
}
interceptorIndex = i;
}
}
// Actuallyinvoke the handler.
5、调用处理器
mv = ha.handle(processedRequest, response,mappedHandler.getHandler());
// Do we needview name translation?
6、视图转换
if (mv !=null && !mv.hasView()) {
mv.setViewName(getDefaultViewName(request));
}
// ApplypostHandle methods of registered interceptors.
7、拦截器后处理
if (interceptors !=null) {
for (int i = interceptors.length - 1; i >= 0; i--) {
HandlerInterceptor interceptor =interceptors[i];
interceptor.postHandle(processedRequest,response, mappedHandler.getHandler(), mv);
}
}
}
catch (ModelAndViewDefiningException ex) {
logger.debug("ModelAndViewDefiningExceptionencountered", ex);
mv = ex.getModelAndView();
}
catch (Exception ex) {
Object handler = (mappedHandler !=null ?mappedHandler.getHandler() :null);
mv = processHandlerException(processedRequest,response, handler, ex);
errorView = (mv !=null);
}
// Did thehandler return a view to render?
if (mv !=null && !mv.wasCleared()) {
render(mv, processedRequest, response);
if (errorView) {
WebUtils.clearErrorRequestAttributes(request);
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("NullModelAndView returned to DispatcherServlet with name '" + getServletName() +
"':assuming HandlerAdapter completed request handling");
}
}
// Triggerafter-completion for successful outcome.
8、视图渲染后处理
triggerAfterCompletion(mappedHandler, interceptorIndex,processedRequest, response,null);
}
catch (Exception ex) {
// Triggerafter-completion for thrown exception.
triggerAfterCompletion(mappedHandler, interceptorIndex,processedRequest, response, ex);
throw ex;
}
catch (Error err) {
ServletException ex =new NestedServletException("Handler processing failed", err);
// Triggerafter-completion for thrown exception.
triggerAfterCompletion(mappedHandler, interceptorIndex,processedRequest, response, ex);
throw ex;
}
finally {
// Clean up anyresources used by a multipart request.
9、文件资源清理(实际执行的是删除文件:如果保存在内存,则清除对应的内存文件内容;若保存在临时文件,则删除对应的临时文件)
if (processedRequest != request) {
cleanupMultipart(processedRequest);
}
}
}