從源碼角度看spring mvc的請求處理過程
在分析spring mvc源碼之前,先看一張圖:
請求處理的過程:
1.DispatcherServelt作為前端控制器,攔截request對象。
2.DispatcherServlet接收到request對象之后,查詢HandlerMapping,得到一個HandlerExecutionChain對象。
3.DispatcherServlet將Handler對象交由HandlerAdapter(適配器模式的典型應用),調用相應的controller方法。
4.Controller方法返回ModelAndView對象,DispatcherServlet將view交由ViewResolver進行解析,得到相應的視圖。用model渲染視圖。
5.返回響應結果。
整個過程的流程其實就是DispatcherServelt中doDispatch()方法的調用過程。
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { //第一步攔截request對象,doDispatch()方法在doService()方法中被調用,request對象是經(jīng)過處理的。 HttpServletRequest processedRequest = request; HandlerExecutionChain mappedHandler = null; boolean multipartRequestParsed = false; WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); try { ModelAndView mv = null; Exception dispatchException = null; try { //和文件的上傳和下載有關系,判斷請求的類型是否是multipart類型 processedRequest = checkMultipart(request); multipartRequestParsed = (processedRequest != request); // Determine handler for the current request. //主要看這里,這里是得到HandlerExecutionChain的方法。關于Handler()方法向下看 mappedHandler = getHandler(processedRequest); if (mappedHandler == null || mappedHandler.getHandler() == null) { noHandlerFound(processedRequest, response); return; } // Determine handler adapter for the current request. //這里已經(jīng)獲取到HandlerExecutionChain對象,接下來就要獲取HandlerAdapter對象,調用Handler對象的方法。 HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); // Process last-modified header, if supported by the handler //有關瀏覽器緩存的設定(304) String method = request.getMethod(); boolean isGet = "GET".equals(method); if (isGet || "HEAD".equals(method)) { long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); if (logger.isDebugEnabled()) { logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified); } if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) { return; } } //pan'du if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } // Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) { return; } //解析視圖,數(shù)據(jù)渲染 applyDefaultViewName(request, mv); mappedHandler.applyPostHandle(processedRequest, response, mv); } catch (Exception ex) { dispatchException = ex; } processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); } catch (Exception ex) { triggerAfterCompletion(processedRequest, response, mappedHandler, ex); } catch (Error err) { triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err); } finally { if (asyncManager.isConcurrentHandlingStarted()) { // Instead of postHandle and afterCompletion if (mappedHandler != null) { mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); } } else { // Clean up any resources used by a multipart request. if (multipartRequestParsed) { cleanupMultipart(processedRequest); } } } } protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { //遍歷HandlerMappingList對象(存儲若干個HandlerMapping對象),不斷調用,直到不為空為止,否則返回null for (HandlerMapping hm : this.handlerMappings) { if (logger.isTraceEnabled()) { logger.trace( "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'"); } HandlerExecutionChain handler = hm.getHandler(request); if (handler != null) { return handler; } } return null; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Springboot獲取文件內容如何將MultipartFile轉File
本文給大家介紹Springboot獲取文件內容,將MultipartFile轉File方法,本文結合示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-01-01java String類功能、原理與應用案例【統(tǒng)計、判斷、轉換等】
這篇文章主要介紹了java String類功能、原理與應用案例,結合實例形式詳細分析了java String類的基本功能、構造方法,以及使用String類實現(xiàn)統(tǒng)計、判斷、轉換等功能相關操作技巧,需要的朋友可以參考下2019-03-03Java利用StringBuffer替換特殊字符的方法實現(xiàn)
這篇文章主要介紹了Java利用StringBuffer替換特殊字符的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04