1 protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { 2 /** 3 * 声明变量 HttpServletRequest HandlerExecutionChain Handler执行链包含和最扣执行的Handler 4 */ 5 HttpServletRequest processedRequest = request; 6 HandlerExecutionChain mappedHandler = null; 7 //是不是一个多组件请求 8 boolean multipartRequestParsed = false; 9 //异步管理器 10 WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); 11 12 try { 13 //视图 14 ModelAndView mv = null; 15 //异常 16 Exception dispatchException = null; 17 18 try { 19 /** 20 * 1.检查是否上传请求 21 */ 22 processedRequest = checkMultipart(request); 23 multipartRequestParsed = (processedRequest != request); 24 25 // Determine handler for the current request. 26 /** 27 * 2.根据processedRequest获取映射的Handler执行链 HandlerExecutionChain 28 * 有当前请求的Handler和Inteceptor 29 */ 30 mappedHandler = getHandler(processedRequest); 31 if (mappedHandler == null) { 32 /** 33 * 如果mappedHandler为空就返回404 34 */ 35 noHandlerFound(processedRequest, response); 36 return; 37 } 38 39 /** 40 * 3.根据mappedHandler HandlerExecutionChain HandlerAdapter适配器 41 */ 42 // 确定当前请求的处理程序适配器。 43 HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); 44 45 /** 46 * 获取请求方法 47 * 处理last-modified 请求头 48 */ 49 // Process last-modified header, if supported by the handler. 50 String method = request.getMethod(); 51 boolean isGet = "GET".equals(method); 52 if (isGet || "HEAD".equals(method)) { 53 //获取最近修改时间,缓存 54 long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); 55 if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) { 56 return; 57 } 58 } 59 60 /** 61 * 4.预处理,执行拦截器等 62 */ 63 if (!mappedHandler.applyPreHandle(processedRequest, response)) { 64 return; 65 } 66 67 /** 68 * 5.实现执行Controller中(Handler)的方法,返回ModelAndView视图 69 */ 70 // Actually invoke the handler. 71 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); 72 73 if (asyncManager.isConcurrentHandlingStarted()) { 74 /** 75 * 判断 是不是异步请求,是就返回了 76 */ 77 return; 78 } 79 /** 80 * 6.对象视图对象的处理 81 */ 82 applyDefaultViewName(processedRequest, mv); 83 /** 84 * 7.拦截器后后置处理 85 */ 86 mappedHandler.applyPostHandle(processedRequest, response, mv); 87 } 88 catch (Exception ex) { 89 dispatchException = ex; 90 } 91 catch (Throwable err) { 92 // As of 4.3, we're processing Errors thrown from handler methods as well, 93 // 使它们可用于@异常处理程序方法和其他场景。 94 dispatchException = new NestedServletException("Handler dispatch failed", err); 95 } 96 /** 97 * 8.对页面渲染 98 */ 99 processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); 100 } 101 catch (Exception ex) { 102 /** 103 * 9.对页面渲染完成里调用拦截器中的AfterCompletion方法 104 */ 105 triggerAfterCompletion(processedRequest, response, mappedHandler, ex); 106 } 107 catch (Throwable err) { 108 /** 109 * 最终对页面渲染完成里调用拦截器中的AfterCompletion方法 110 */ 111 triggerAfterCompletion(processedRequest, response, mappedHandler, 112 new NestedServletException("Handler processing failed", err)); 113 } 114 finally { 115 if (asyncManager.isConcurrentHandlingStarted()) { 116 // Instead of postHandle and afterCompletion 117 if (mappedHandler != null) { 118 mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); 119 } 120 } 121 else { 122 //清除由多个部分组成的请求使用的所有资源。 123 if (multipartRequestParsed) { 124 cleanupMultipart(processedRequest); 125 } 126 } 127 } 128 }
标签:mappedHandler,mv,request,Handler,test,response,processedRequest From: https://www.cnblogs.com/Met32/p/16603152.html