1. 程式人生 > >【推薦】SpringMVC原始碼總結(一)HandlerMapping和HandlerAdapter入門

【推薦】SpringMVC原始碼總結(一)HandlerMapping和HandlerAdapter入門

  1. protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  2.         HttpServletRequest processedRequest = request;  
  3.         HandlerExecutionChain mappedHandler = null;  
  4.         boolean multipartRequestParsed = false;  
  5.         WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);  
  6.         try {  
  7.             ModelAndView mv = null;  
  8.             Exception dispatchException = null;  
  9.             try {  
  10.                 processedRequest = checkMultipart(request);  
  11.                 multipartRequestParsed = (processedRequest != request);  
  12.                       //這個是重點,第一步由HandlerMapping找到對應的handler  
  13.                 // Determine handler for the current request.  
  14.                 mappedHandler = getHandler(processedRequest);  
  15.                 if (mappedHandler == null || mappedHandler.getHandler() == null) {  
  16.                     noHandlerFound(processedRequest, response);  
  17.                     return;  
  18.                 }  
  19.                 // Determine handler adapter for the current request.  
  20.                        //這是第二步,找到合適的HandlerAdapter,然後由它來排程執行handler的方法  
  21.                 HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());  
  22.                 // Process last-modified header, if supported by the handler.  
  23.                 String method = request.getMethod();  
  24.                 boolean isGet = "GET".equals(method);  
  25.                 if (isGet || "HEAD".equals(method)) {  
  26.                     long lastModified = ha.getLastModified(request, mappedHandler.getHandler());  
  27.                     if (logger.isDebugEnabled()) {  
  28.                         logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);  
  29.                     }  
  30.                     if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {  
  31.                         return;  
  32.                     }  
  33.                 }  
  34.                 if (!mappedHandler.applyPreHandle(processedRequest, response)) {  
  35.                     return;  
  36.                 }  
  37.                 try {  
  38.                     // Actually invoke the handler.  
  39.                     mv = ha.handle(processedRequest, response, mappedHandler.getHandler());  
  40.                 }  
  41.                 finally {  
  42.                     if (asyncManager.isConcurrentHandlingStarted()) {  
  43.                         return;  
  44.                     }  
  45.                 }  
  46.                 applyDefaultViewName(request, mv);  
  47.                 mappedHandler.applyPostHandle(processedRequest, response, mv);  
  48.             }  
  49.             catch (Exception ex) {  
  50.                 dispatchException = ex;  
  51.             }  
  52.             processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);  
  53.         }  
  54.         catch (Exception ex) {  
  55.             triggerAfterCompletion(processedRequest, response, mappedHandler, ex);  
  56.         }  
  57.         catch (Error err) {  
  58.             triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err);  
  59.         }  
  60.         finally {  
  61.             if (asyncManager.isConcurrentHandlingStarted()) {  
  62.                 // Instead of postHandle and afterCompletion  
  63.                 mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);  
  64.                 return;  
  65.             }  
  66.             // Clean up any resources used by a multipart request.  
  67.             if (multipartRequestParsed) {  
  68.                 cleanupMultipart(processedRequest);  
  69.             }  
  70.         }  
  71.     }