简单介绍了一个请求的处理过程, 简略描述了调用过程,并没有涉及过多细节,如url匹配,报文解析转换等。
<SpringMVC源码分析(2)DispatcherServlet的初始化>:介绍了servlet的初始化过程,尤其initStrategies方法。
本文主要总结DispatcherServlet几个重要组件的关系。
1.类图
该类图并没有全面的描述SpringMVC相关类,重点说明组件的关系。
该类图基于springmvc3.0.5版本。
很多类属性并没有完全罗列,进行了取舍。
基于DispatcherServlet,而且深度尽量适可而止,否则造成图复杂混乱。
---------------------------------------------------
2.HanlerMapping组件介绍
2.1 HanlerMapping类图
HanlerMapping是springmvc中完成url到controller映射的组件。
2.2 HanlerMappingAPI
1
2
3
4
|
public
interface
HandlerMapping {
//根据选择策略,返回Handler
HandlerExecutionChain getHandler(HttpServletRequest request)
throws
Exception;
}
|
3.HandlerAdapter
3.1类图
是MVC framework SPI ;负责具体干活。如调用方法,参数解析等。
3.2 API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
interface
HandlerAdapter {
/**
判断是否可以处理handler
*/
boolean
supports(Object handler);
/**
* 处理请求
*/
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws
Exception;
/**
* getLastModified
*/
long
getLastModified(HttpServletRequest request, Object handler);
}
|
4.HandlerExceptionResolver
4.1 HandlerExceptionResolver类图
专门处理异常情况,根据异常情况,构造ModelAndView
4.2 HandlerExceptionResolver API
1
2
3
4
5
6
7
8
9
|
public
interface
HandlerExceptionResolver {
/**
* 处理异常
*/
ModelAndView resolveException(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
}
|
5. ViewResolver
5.1 ViewResolver类图
作用:将string的视图名称解析为View类型的视图
5.2 ViewResolver API
1
2
3
4
5
6
7
8
9
|
public
interface
ViewResolver {
/**
* Resolve the given view by name.
解析视图
*/
View resolveViewName(String viewName, Locale locale)
throws
Exception;
}
|
6.RequestToViewNameTranslator
默认实现是DefaultRequestToViewNameTranslator.主要作用是根据请求信息获取视图名称。
1
2
3
4
5
|
public
interface
RequestToViewNameTranslator {
String getViewName(HttpServletRequest request)
throws
Exception;
}
|
7.LocaleResolver
7.1 LocaleResolver类图
从request解析Locale,实现本地化;一般使用在解析视图时使用;和使用国际化资源和主题时使用
7.2 LocaleResolver API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
interface
LocaleResolver {
/**
* Resolve the current locale via the given request.
* Should return a default locale as fallback in any case.
* @param request the request to resolve the locale for
* @return the current locale (never <code>null</code>)
*/
Locale resolveLocale(HttpServletRequest request);
/**
* Set the current locale to the given one.
* @param request the request to be used for locale modification
* @param response the response to be used for locale modification
* @param locale the new locale, or <code>null</code> to clear the locale
* @throws UnsupportedOperationException if the LocaleResolver implementation
* does not support dynamic changing of the theme
*/
void
setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale);
}
|
8.ThemeResolver
8.1 ThemeResolver类图
主要用来设置主题。
8.2 API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
interface
ThemeResolver {
/**
* Resolve the current theme name via the given request.
* Should return a default theme as fallback in any case.
* @param request request to be used for resolution
* @return the current theme name
*/
String resolveThemeName(HttpServletRequest request);
/**
* Set the current theme name to the given one.
* @param request request to be used for theme name modification
* @param response response to be used for theme name modification
* @param themeName the new theme name
* @throws UnsupportedOperationException if the ThemeResolver implementation
* does not support dynamic changing of the theme
*/
void
setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName);
}
|
9.MultipartResolver
默认实现为CommonsMultipartResolver,处理文件上传请求。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
interface
MultipartResolver {
/**
* Determine if the given request contains multipart content.
*/
boolean
isMultipart(HttpServletRequest request);
/**
* Parse the given HTTP request into multipart files and parameters,
* and wrap the request inside a
*/
MultipartHttpServletRequest resolveMultipart(HttpServletRequest request)
throws
MultipartException;
/**
* Cleanup any resources used for the multipart handling,
*/
void
cleanupMultipart(MultipartHttpServletRequest request);
}
|
10 总结。
简单介绍了下各个组件,每个组件都各司其职,互相配合。才有了强大的SpringMVC功能。在以后的文章里,针对每个组件,会结合具体例子,由浅入深式的进行剖析。
另外一个重要的核心部分是HandlerMethodInvoker,涉及的东西比较多,非常有看头。