按照上邊的執行流程圖,我們可以看出一個SpringMVC整體的一個執行輪廓,下麵我們具體來分析下 首先伺服器接收到一個請求,匹配並調用了我們的前端控制器(DispatcherServlet)也叫中央處理器的請求路徑,在web.xml文件中配置,我們來看下配置文件 因為DispatcherServle ...
按照上邊的執行流程圖,我們可以看出一個SpringMVC整體的一個執行輪廓,下麵我們具體來分析下
首先伺服器接收到一個請求,匹配並調用了我們的前端控制器(DispatcherServlet)也叫中央處理器的請求路徑,在web.xml文件中配置,我們來看下配置文件
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name>
<!-- / 代表匹配所有除了jsp文件的請求 --> <url-pattern>/</url-pattern> </servlet-mapping>
因為DispatcherServlet實際上是間接的繼承了我們的HttpServlet,所以它就相當於一個Servlet,SpringMVC幫我們封裝好的一個Servlet,下麵是繼承體系
所以說,當來了一個請求後,WEB容器將把請求交給我們的DispatcherServlet進行處理
DispatcherServlet接收到請求後,會把我們的請求信息交給HandlerMapping處理器映射器進行處理
HandlerMapping根據請求的URL信息去查找匹配的URL的Handler,如果查找成,並返回一個執行鏈,下麵來看下底層執行代碼
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
Iterator var2 = this.handlerMappings.iterator();
HandlerExecutionChain handler;
do {
if (!var2.hasNext()) {
return null;
}
HandlerMapping hm = (HandlerMapping)var2.next();
if (this.logger.isTraceEnabled()) {
this.logger.trace("Testing handler map [" + hm + "] in DispatcherServlet with name '" + this.getServletName() + "'");
}
handler = hm.getHandler(request);//根據request對象獲取handler,獲取到的話將返回一個對象,沒有的話將返回null 有興趣童鞋自行深入查看
} while(handler == null);//如果handler為null繼續執行迴圈
return handler;
}
如果沒有找到的話將執行以下代碼
protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (pageNotFoundLogger.isWarnEnabled()) {
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + getRequestUri(request) + "] in DispatcherServlet with name '" + this.getServletName() + "'");
}
if (this.throwExceptionIfNoHandlerFound) {
ServletServerHttpRequest sshr = new ServletServerHttpRequest(request);
throw new NoHandlerFoundException(sshr.getMethod().name(), sshr.getServletRequest().getRequestURI(), sshr.getHeaders());
} else {
response.sendError(404);//響應404
}
}
找到成功的情況下,DispatcherServlet再次請求 處理器適配器(HandlerAdapter)調用相應的Handler 進行處理返回 ModelAndView 給 DispatcherServlet,底層代碼,粗略看下即可,不是重點
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpServletRequest processedRequest = request; HandlerExecutionChain mappedHandler = null; boolean multipartRequestParsed = false; WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); try { ModelAndView mv = null; Exception dispatchException = null; try { processedRequest = this.checkMultipart(request); multipartRequestParsed = processedRequest != request; mappedHandler = this.getHandler(processedRequest); if (mappedHandler == null || mappedHandler.getHandler() == null) { this.noHandlerFound(processedRequest, response); return; } HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler()); String method = request.getMethod(); boolean isGet = "GET".equals(method); if (isGet || "HEAD".equals(method)) { long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); if (this.logger.isDebugEnabled()) { this.logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified); } if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet) { return; } } if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); //在這裡邊調用相應的Handler,並通過這個方法返回ModelAndModel給DispatcherServlet if (asyncManager.isConcurrentHandlingStarted()) { return; } this.applyDefaultViewName(request, mv); mappedHandler.applyPostHandle(processedRequest, response, mv); } catch (Exception var19) { dispatchException = var19; } this.processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); } catch (Exception var20) { this.triggerAfterCompletion(processedRequest, response, mappedHandler, var20); } catch (Error var21) { this.triggerAfterCompletionWithError(processedRequest, response, mappedHandler, var21); } finally { if (asyncManager.isConcurrentHandlingStarted()) { if (mappedHandler != null) { mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); } } else if (multipartRequestParsed) { this.cleanupMultipart(processedRequest); } } }
然後DispatcherServlet將ModelAndView請求對應的ViewResolver(視圖解析器)解析視圖,然後返回具體的View,底層實現
protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale, HttpServletRequest request) throws Exception { Iterator var5 = this.viewResolvers.iterator(); View view; do { if (!var5.hasNext()) { return null; } ViewResolver viewResolver = (ViewResolver)var5.next(); view = viewResolver.resolveViewName(viewName, locale); } while(view == null); return view; }
DispatcherServlet對View視圖進行渲染,也就是把模型數據填充到視圖裡邊,底層執行代碼
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { Locale locale = this.localeResolver.resolveLocale(request); response.setLocale(locale); View view; if (mv.isReference()) { view = this.resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); if (view == null) { throw new ServletException("Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + this.getServletName() + "'"); } } else { view = mv.getView(); if (view == null) { throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in servlet with name '" + this.getServletName() + "'"); } } if (this.logger.isDebugEnabled()) { this.logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + this.getServletName() + "'"); } try { view.render(mv.getModelInternal(), request, response); } catch (Exception var7) { if (this.logger.isDebugEnabled()) { this.logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" + this.getServletName() + "'", var7); } throw var7; } }
最後DispatcherServlet將視圖傳遞給前臺
PS:
Spring MVC核心組件:
1.DispatcherServlet(中央處理器):將請求轉發給控制器
2.Controller(控制器):處理請求的控制器
3.HandlerMapping(映射處理器):負責映射中央處理器轉發給Controller時的映射策略
4.ModelAndView:伺服器返回數據以及視圖層的封裝類
5.ViewResolver(視圖解析器):解析具體的視圖
當用戶發起請求後,執行DiapacherServlet,如果是JSP直接調用jsp頁面.如果不是JSP,DiapacherServlet調用HandlerMapping判斷請求URL是否合法,如果URL不存在報錯,如果URL存在使用HandlerAdapter調用具體的HandlerMethod,當Handler執行完成後會返回ModelAndView,會被ViewResovler解析,調用具體的物理視圖.
最終響應給客戶端瀏覽器.