标签:
一.spring整体结构
首先俯视一下spring mvc的整体结构
二.处理流程
1.请求处理的第一站就是DispatcherServlet.它是整个spring mvc的控制核心.与大多数的java web框架一样,spring mvc所有的请求都会经过一个核心的前端控制器(Servlet).DispatcherServlet通过一个或者多个处理程序映射,将每个请求映射到处理程序中。处理程序映射配置在web应用程序的上下文中,是实现了HandlerMapping接口的Bean。它负责为请求返回一个适当的处理程序(也就是Controller)。处理程序映射通常根据请求的URL将请求映射到处理程序(Controller)。
在开始dispatcher的开始之前已经完成了handlerMappings的装配
1 public void setDetectAllHandlerMappings(boolean detectAllHandlerMappings) { 2 this.detectAllHandlerMappings = detectAllHandlerMappings; 3 }
在handlerMappings中匹配可用的handlerMapping
1 protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { 2 for (HandlerMapping hm : this.handlerMappings) { 3 if (logger.isTraceEnabled()) { 4 logger.trace( 5 "Testing handler map [" + hm + "] in DispatcherServlet with name ‘" + getServletName() + "‘"); 6 } 7 HandlerExecutionChain handler = hm.getHandler(request); 8 if (handler != null) { 9 return handler; 10 } 11 } 12 return null; 13 }
获取handler
1 public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { 2 Object handler = getHandlerInternal(request); 3 if (handler == null) { 4 handler = getDefaultHandler(); 5 } 6 if (handler == null) { 7 return null; 8 } 9 // Bean name or resolved handler? 10 if (handler instanceof String) { 11 String handlerName = (String) handler; 12 handler = getApplicationContext().getBean(handlerName); 13 } 14 15 HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request); 16 if (CorsUtils.isCorsRequest(request)) { 17 CorsConfiguration globalConfig = this.corsConfigSource.getCorsConfiguration(request); 18 CorsConfiguration handlerConfig = getCorsConfiguration(handler, request); 19 CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig); 20 executionChain = getCorsHandlerExecutionChain(request, executionChain, config); 21 } 22 return executionChain; 23 }
2. 一旦DispatcherServlet选择了适当的控制器,它就会调用这个控制器来处理请求。
1 protected ModelAndView handleInternal(HttpServletRequest request, 2 HttpServletResponse response, HandlerMethod handlerMethod) throws Exception { 3 4 checkRequest(request); 5 6 if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) { 7 applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers); 8 } 9 else { 10 prepareResponse(response); 11 } 12 13 // Execute invokeHandlerMethod in synchronized block if required. 14 if (this.synchronizeOnSession) { 15 HttpSession session = request.getSession(false); 16 if (session != null) { 17 Object mutex = WebUtils.getSessionMutex(session); 18 synchronized (mutex) { 19 return invokeHandlerMethod(request, response, handlerMethod); 20 } 21 } 22 } 23 24 return invokeHandlerMethod(request, response, handlerMethod); 25 }
返回ModelAndView对象,该对象中包含请求的view
return getModelAndView(mavContainer, modelFactory, webRequest);
3. 控制器处理完请求后,会将模型和视图名(有时候是视图对象)返回给DispatcherServlet。模型包含了控制器要传递给视图进行显示的属性。如果返回的是视图名称,它会被解析成视图对象再进行呈现。绑定模型和视图的基本类是ModelAndView
4. 当DispatcherServlet接收到模型和视图名称时,它会将逻辑视图名称解析成视图对象再进行呈现。DispatcherServlet从一个或者多个视图解析器中解析视图。视图解析器配置在Web应用程序上下文中,是实现了ViewResolver接口的Bean。它的任务是根据逻辑视图名称返回试图对象。
protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale, HttpServletRequest request) throws Exception { for (ViewResolver viewResolver : this.viewResolvers) { View view = viewResolver.resolveViewName(viewName, locale); if (view != null) { return view; } } return null; }
5. 一旦DispatcherServlet将视图名称解析称为试图对象,它就会呈现视图对象,并传递控制器返回的模型。视图的任务是将模型属性展示给用户。
1 protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest) { 2 if (this.exposeContextBeansAsAttributes || this.exposedContextBeanNames != null) { 3 return new ContextExposingHttpServletRequest( 4 originalRequest, getWebApplicationContext(), this.exposedContextBeanNames); 5 } 6 return originalRequest; 7 }
针对第一张图的处理流程解释
标签:
原文地址:http://www.cnblogs.com/plxx/p/5400467.html