一.spring整体结构
首先俯视一下spring mvc的整体结构
二.处理流程
1.请求处理的第一站就是DispatcherServlet.它是整个spring mvc的控制核心.与大多数的java web框架一样,spring mvc所有的请求都会经过一个核心的前端控制器(Servlet).DispatcherServlet通过一个或者多个处理程序映射,将每个请求映射到处理程序中。处理程序映射配置在web应用程序的上下文中,是实现了HandlerMapping接口的Bean。它负责为请求返回一个适当的处理程序(也就是Controller)。处理程序映射通常根据请求的URL将请求映射到处理程序(Controller)。
在开始dispatcher的开始之前已经完成了handlerMappings的装配
public void setDetectAllHandlerMappings(boolean detectAllHandlerMappings) {
this.detectAllHandlerMappings = detectAllHandlerMappings;
}
在handlerMappings中匹配可用的handlerMapping
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
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;
}
获取handler
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
Object handler = getHandlerInternal(request);
if (handler == null) {
handler = getDefaultHandler();
}
if (handler == null) {
return null;
}
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
} HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
if (CorsUtils.isCorsRequest(request)) {
CorsConfiguration globalConfig = this.corsConfigSource.getCorsConfiguration(request);
CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
}
return executionChain;
}
2. 一旦DispatcherServlet选择了适当的控制器,它就会调用这个控制器来处理请求。
protected ModelAndView handleInternal(HttpServletRequest request,
HttpServletResponse response, HandlerMethod handlerMethod) throws Exception { checkRequest(request); if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
}
else {
prepareResponse(response);
} // Execute invokeHandlerMethod in synchronized block if required.
if (this.synchronizeOnSession) {
HttpSession session = request.getSession(false);
if (session != null) {
Object mutex = WebUtils.getSessionMutex(session);
synchronized (mutex) {
return invokeHandlerMethod(request, response, handlerMethod);
}
}
} return invokeHandlerMethod(request, response, handlerMethod);
}
返回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将视图名称解析称为试图对象,它就会呈现视图对象,并传递控制器返回的模型。视图的任务是将模型属性展示给用户。
protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest) {
if (this.exposeContextBeansAsAttributes || this.exposedContextBeanNames != null) {
return new ContextExposingHttpServletRequest(
originalRequest, getWebApplicationContext(), this.exposedContextBeanNames);
}
return originalRequest;
}
针对第一张图的处理流程解释