SpringMVC如何解析视图概述
- 不论控制器返回一个String,ModelAndView,View都会转换为ModelAndView对象,由视图解析器解析视图,然后,进行页面的跳转。
![]()
- 视图解析源码分析:重要的两个接口
- 断点调式
流程图
视图和视图解析器
- 请求处理方法执行完成后,最终返回一个 ModelAndView 对象。对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它们装配成一个 ModelAndView 对象 ,它包含了逻辑名和模型对象的视图
- Spring MVC 借助 视图解析器(ViewResolver) 得到最终的视图对象(View),最终的视图可以是 JSP ,也可能是 Excel、JFreeChart等各种表现形式的视图
- 对于最终究竟采取何种视图对象对模型数据进行渲染,处理器并不关心,处理器工作重点聚焦在生产模型数据的工作上,从而实现 MVC 的充分解耦
视图
视图的作用是渲染模型数据,将模型里的数据以某种形式呈现给客户。
为了实现视图模型和具体实现技术的解耦,Spring 在 org.springframework.web.servlet 包中定义了一个高度抽象的 View 接口:

视图对象由视图解析器负责实例化。由于视图是无状态的,所以他们不会有线程安全的问题
常用的视图实现类

视图解析器
- SpringMVC 为逻辑视图名的解析提供了不同的策略,可以在 Spring WEB 上下文中配置一种或多种解析策略,并指定他们之间的先后顺序。每一种映射策略对应一个具体的视图解析器实现类。
- 视图解析器的作用比较单一:将逻辑视图解析为一个具体的视图对象。
- 所有的视图解析器都必须实现 ViewResolver 接口:

常用的视图解析类实现类

- 程序员可以选择一种视图解析器或混用多种视图解析器
- 每个视图解析器都实现了 Ordered 接口并开放出一个 order 属性,可以通过 order 属性指定解析器的优先顺序,order 越小优先级越高。
- SpringMVC 会按视图解析器顺序的优先顺序对逻辑视图名进行解析,直到解析成功并返回视图对象,否则将抛出 ServletException 异常
- InternalResourceViewResolver
- JSP 是最常见的视图技术,可以使用 InternalResourceViewResolve作为视图解析器:

- JSP 是最常见的视图技术,可以使用 InternalResourceViewResolve作为视图解析器:
Spring MVC视图解析器之前缀
forward
1 | @RequestMapping("hello") |
重定向redirect
1 | /** |
JstlView
- 导包导入jstl的时候自动创建为一个jstlView,可以快速方便支持国际化
- JavaWeb国际化步骤
- 得到一个Locale对象
- 使用ResourceBundle绑定国际化资源文件
- 使用ResourceBundle.getString(key);获取到国际化配置文件中的值
- Web页面国际化,fmt标签来做
1
2
3< fmt:setLocale>
< fmt:setBundle >
<fmt:message>
- 使用jstlView
- 让Spring管理国际化资源
- 去页面取值
- 若项目中使用了JSTL,则SpringMVC 会自动把视图由InternalResourceView转为 JstlView (断点调试,将JSTL的jar包增加到项目中,视图解析器会自动修改为JstlView)
- 若使用 JSTL 的 fmt 标签则需要在 SpringMVC 的配置文件中配置国际化资源文件
- 若希望直接响应通过 SpringMVC 渲染的页面,可以使用 mvc:view-controller 标签实现
*注意:使用jstl国际化是,页面映射不能带前缀 *
实验代码
| i18n.properties | i18n_en_US.properties | i18n_zh_CN.properties | ||
|---|---|---|---|---|
| i18n.username=username | i18n.password=password | i18n.username=Username | ||
| i18n.password=Password | i18n.username=\u7528\u6237\u540D | i18n.password=\u5BC6\u7801 | ||
| 1. 增加jstl标签 jar包(断点调试,这时的View对象就是JstlView) | ||||
![]() |
||||
| 2. 设置国际化资源文件 | ||||
|
||||
| 3. 控制器代码 | ||||
|
||||
| 4. 成功页面(/success.jsp)使用fmt标签库 | ||||
|
||||
| ## mvc:view-controller标签 | ||||
| - 若希望直接响应通过 SpringMVC 渲染的页面,可以使用 mvc:view-controller 标签实现 | ||||
|
||||
| - 请求的路径: | ||||
|
||||
| - 配置mvc:view-controller会导致其他请求路径失效 | ||||
| - 解决办法: | ||||
|
||||
| ## 自定义视图 | ||||
| - 自定义视图(需要加入SpringMVC,那么,一定需要实现框架的接口) | ||||
| - 若希望使用 Excel 展示数据列表,仅需要扩展 SpringMVC 提供的 AbstractExcelView 或 AbstractJExcelView 即可。 | ||||
| - 实现 buildExcelDocument() 方法,在方法中使用模型数据对象构建 Excel 文档就可以了。 | ||||
| - AbstractExcelView 基于 POI API,而 AbstractJExcelView 是基于 JExcelAPI 的。 | ||||
| - 视图对象需要配置 IOC 容器中的一个 Bean,使用 BeanNameViewResolver 作为视图解析器即可 | ||||
| - 若希望直接在浏览器中直接下载 Excel 文档,则可以设置响应头 Content-Disposition 的值为 attachment;filename=xxx.xls | ||||
![]() |
||||
| ## 实验代码 | ||||
| 1. 测试链接 | ||||
|
||||
| 2. 控制方法 | ||||
|
||||
| 3. 自定义视图 | ||||
|
||||
| 4. 自定义视图解析器 | ||||
|
||||
| 5. 声明视图解析器 | ||||
|
||||
| —— | ||||
| 注意:InternalResourceViewResolver默认的优先级:private int order = Integer.MAX_VALUE; |
视图解析器源码解析
- 方法执行后的返回值会作为页面地址参考,转发或者重定向到页面
- 视图解析器可能会进行页面地址拼串
任何方法的返回值,最终都会被包装成ModelAndView对象
processDispatchResult(HttpServletRequest request, HttpServletResponse response,HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception)
视图渲染流程:将域中的数据页面显示,页面就是渲染模型数据
render(mv, request, response);
View与ViewResolver:
ViewResolver对象就是根据视图名返回View对象

怎么能根据方法的返回值得到View对象?
1
2
3
4
5
6
7
8
9
10
11
12
13protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale,
HttpServletRequest request) throws Exception {
//遍历所有resolver
for (ViewResolver viewResolver : this.viewResolvers) {
//ViewResolver视图解析器根据方法的返回值,得到一个View对象
View view = viewResolver.resolveViewName(viewName, locale);
if (view != null) {
return view;
}
}
return null;
}- viewResolver.resolveViewName细节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
if (!isCache()) {
return createView(viewName, locale);
}
else {
Object cacheKey = getCacheKey(viewName, locale);
View view = this.viewAccessCache.get(cacheKey);
if (view == null) {
synchronized (this.viewCreationCache) {
view = this.viewCreationCache.get(cacheKey);
if (view == null) {
// Ask the subclass to create the View object.
/*****创建******/
view = createView(viewName, locale);
if (view == null && this.cacheUnresolved) {
view = UNRESOLVED_VIEW;
}
if (view != null) {
this.viewAccessCache.put(cacheKey, view);
this.viewCreationCache.put(cacheKey, view);
if (logger.isTraceEnabled()) {
logger.trace("Cached view [" + cacheKey + "]");
}
}
}
}
}
return (view != UNRESOLVED_VIEW ? view : null);
}
} - 创建View的细节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22@Override
protected View createView(String viewName, Locale locale) throws Exception {
// If this resolver is not supposed to handle the given view,
// return null to pass on to the next resolver in the chain.
if (!canHandle(viewName, locale)) {
return null;
}
// Check for special "redirect:" prefix.
/********/
if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
RedirectView view = new RedirectView(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
return applyLifecycleMethods(viewName, view);
}
// Check for special "forward:" prefix.
if (viewName.startsWith(FORWARD_URL_PREFIX)) {
String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
return new InternalResourceView(forwardUrl);
}
// Else fall back to superclass implementation: calling loadView.
return super.createView(viewName, locale);
}**视图解析器得到View的流程** > 所有配置的视图解析器都来尝试根据视图名(返回值)得到View对象 > 如果能得到就返回,得不到就换下一个视图解析器 > 再调用View对象的Render,
- viewResolver.resolveViewName细节
View中的render得到
1
2
3
4
5
6
7
8
9
10
11
12
13@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (logger.isTraceEnabled()) {
logger.trace("Rendering view with name '" + this.beanName + "' with model " + model +
" and static attributes " + this.staticAttributes);
}
Map<String, Object> mergedModel = createMergedOutputModel(model, request, response);
prepareResponse(request, response);
//渲染页面输出的所有数据
renderMergedOutputModel(mergedModel, request, response);
}renderMergedOutputModel位于InternalResourceView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41@Override
protected void renderMergedOutputModel(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine which request handle to expose to the RequestDispatcher.
HttpServletRequest requestToExpose = getRequestToExpose(request);
// *** Expose the model object as request attributes.
//将模型的数据放到request域中
exposeModelAsRequestAttributes(model, requestToExpose);
// Expose helpers as request attributes, if any.
exposeHelpers(requestToExpose);
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(requestToExpose, response);
// Obtain a RequestDispatcher for the target resource (typically a JSP).
RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath);
if (rd == null) {
throw new ServletException("Could not get RequestDispatcher for [" + getUrl() +
"]: Check that the corresponding file exists within your web application archive!");
}
// If already included or response already committed, perform include, else forward.
if (useInclude(requestToExpose, response)) {
response.setContentType(getContentType());
if (logger.isDebugEnabled()) {
logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
rd.include(requestToExpose, response);
}
else {
// Note: The forwarded resource is supposed to determine the content type itself.
if (logger.isDebugEnabled()) {
logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
rd.forward(requestToExpose, response);
}
}为什么数据可以在请求域中获得
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20protected void exposeModelAsRequestAttributes(Map<String, Object> model, HttpServletRequest request) throws Exception {
for (Map.Entry<String, Object> entry : model.entrySet()) {
String modelName = entry.getKey();
Object modelValue = entry.getValue();
if (modelValue != null) {
request.setAttribute(modelName, modelValue);
if (logger.isDebugEnabled()) {
logger.debug("Added model object '" + modelName + "' of type [" + modelValue.getClass().getName() +
"] to request in view with name '" + getBeanName() + "'");
}
}
else {
request.removeAttribute(modelName);
if (logger.isDebugEnabled()) {
logger.debug("Removed model object '" + modelName +
"' from request in view with name '" + getBeanName() + "'");
}
}
}
}
视图解析器只是为了获得视图对象;视图对象才能真正的转发(将模型数据全部放在请求域中)或者重定向到页面
视图对象才能真正渲染视图
源码执行流程





