Component Overview
Handler Mapping
Handler and find the corresponding processor according Interceptors request. Internal only one way
HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
Handler Adapter
Adapter Handler, the following internal method:
boolean supports(Object handler);//判断是否可以使用某个 Handler
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; //具体使用
long getLastModified(HttpServletRequest request, Object handler);//获取资源上一次修改的时间
HandlerExceptionResolver
According exception settings ModelAndView, and then to the render method for rendering.
ModelAndView resolveException(
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)
ViewResolver
Used to String type of view, and resolved to View Locale types of views.
View resolveViewName(String viewName, Locale locale) throws Exception;
It is an implementation class BeanNameViewResolver, rewrites it resolveViewName follows:
public View resolveViewName(String viewName, Locale locale) throws BeansException {
ApplicationContext context = getApplicationContext();
//如果应用上下文没有找到视图,返回 null
if (!context.containsBean(viewName)) {
if (logger.isDebugEnabled()) {
logger.debug("No matching bean found for view name '" + viewName + "'");
}
// Allow for ViewResolver chaining...
return null;
}
//如果找到的视图类型不匹配,也返回 null
if (!context.isTypeMatch(viewName, View.class)) {
if (logger.isDebugEnabled()) {
logger.debug("Found matching bean for view name '" + viewName +
"' - to be ignored since it does not implement View");
}
// Since we're looking into the general ApplicationContext here,
// let's accept this as a non-match and allow for chaining as well...
return null;
}
//根据视图名称从 Spring 容器中查找 Bean,返回找到的 bean
return context.getBean(viewName, View.class);
}
RequestToViewNameTranslator
Get the view name in the request. Interface which is only one way:
String getViewName(HttpServletRequest request) throws Exception; //根据 request 查找视图名
LocaleResolver
Locale for parsing the request.
public interface LocaleResolver {
//从 request 解析出 Locale
Locale resolveLocale(HttpServletRequest request);
//根据 request 设置 locale
void setLocale(HttpServletRequest request, HttpServletResponse response, @Nullable Locale locale);
}
ThemeResolver
Resolving themes
public interface ThemeResolver {
//通过给定的 request 查找主题名
String resolveThemeName(HttpServletRequest request);
//根据给定的 request 设置主题名
void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName);
}
The topics for the RequestContext.java file:
public String getThemeMessage(String code, String defaultMessage) {
//获取主题的信息
return getTheme().getMessageSource().getMessage(code, null, defaultMessage, this.locale);
}
public Theme getTheme() {
//判断主题是否为空
if (this.theme == null) {
// 通过 RequestContextUtils 获取 request 中的主题名
this.theme = RequestContextUtils.getTheme(this.request);
if (this.theme == null) { //如果还是为空的话
//那就是没有有效的主题解析器和主题
this.theme = getFallbackTheme();
}
}
return this.theme;
}
RequestContextUtils.getTheme() 方法:
public static Theme getTheme(HttpServletRequest request) {
ThemeResolver themeResolver = getThemeResolver(request);
ThemeSource themeSource = getThemeSource(request);
if (themeResolver != null && themeSource != null) {
String themeName = themeResolver.resolveThemeName(request);
return themeSource.getTheme(themeName);
}
else {
return null;
}
}
MultipartResolver
The upload request for processing, a processing method: Common packaging request to MultipartHttpServletRequest
public interface MultipartResolver {
//根据 request 判断是否是上传请求
boolean isMultipart(HttpServletRequest request);
//将 request 包装成 MultipartHttpServletRequest
MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
//清理上传过程中产生的临时资源
void cleanupMultipart(MultipartHttpServletRequest request);
}
FlashMapManager
FlashMap redirect the primary delivery parameters, FlashMapManager FlashMap is used to manage.
public interface FlashMapManager {
//恢复参数,并将恢复过的和超时的参数从保存介质中删除
@Nullable
FlashMap retrieveAndUpdate(HttpServletRequest request, HttpServletResponse response);
//将参数保存起来
void saveOutputFlashMap(FlashMap flashMap, HttpServletRequest request, HttpServletResponse response);
}
summary
Introduction Spring MVC in the nine components of the interface, the role and function of internal methods to achieve a brief introduction, we all need a detailed look at the source code.
to sum up
Spring MVC principle summary
Is essentially a Servlet, the Servlet inherited from HttpServlet. Spring MVC provides three levels of Servlet: HttpServletBean, FrameworkServlet and DispatcherServlet. They inherit each other, HttpServletBean directly inherited from the Java HttpServlet. HttpServletBean parameters for the Servlet Servlet configured to set a respective attribute, FrameworkServlet initialized WebApplicationContext Spring MVC used, nine specific component in the processing request is to initialize the DispatcherServlet entire succession is as follows: