概述
不论控制器返回一个String,ModelAndView,View都会转换为ModelAndView对象,由视图解析器解析视图,然后,进行页面的跳转。
视图和视图解析器
请求处理方法执行完成后,最终返回一个 ModelAndView 对象。
对于那些返回 String,View 或 ModeMap 等类型的处理方法,
Spring MVC 也会在内部将它们装配成一个 ModelAndView 对象,它包含了逻辑名和模型对象的视图
Spring MVC 借助视图解析器(ViewResolver)得到最终的视图对象(View),
最终的视图可以是 JSP ,也可能是 Excel、JFreeChart等各种表现形式的视图
对于最终究竟采取何种视图对象对模型数据进行渲染,处理器并不关心,
处理器工作重点聚焦在生产模型数据的工作上,从而实现 MVC 的充分解耦
执行流程
1、任何方法的返回值,最终都会被包装成ModelAndView对象
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
-------------------------------
如果方法的返回有@ResponseBody注解,会在处理过程中交给
handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);处理
也不会有返回值且不会报错
如果故意写成 return null;
那么返回值默认是方法名的首字母小写作为视图名
然后再viewNames数组中找,能找到就处理,不能就换下一个解析器,都没有就抛异常
if (!canHandle(viewName, locale)) {
return null;
}
2、来到页面的方法
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
视图渲染流程:将域中的数据在页面展示;页面就是用来渲染模型数据的;
3、调用render(mv, request, response);渲染页面
4、View与ViewResolver;
ViewResolver的作用是根据视图名(方法的返回值)得到View对象;
view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
5、怎么能根据方法的返回值(视图名)得到View对象?
@Nullable
protected View resolveViewName(String viewName, @Nullable Map<String, Object> model,
Locale locale, HttpServletRequest request) throws Exception {
//遍历所有的ViewResolver;
if (this.viewResolvers != null) {
for (ViewResolver viewResolver : this.viewResolvers) {
//viewResolver视图解析器根据方法的返回值,得到一个View对象;
View view = viewResolver.resolveViewName(viewName, locale);
if (view != null) {
return view;
}
}
}
return null;
}
resolveViewName实现
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对象;
--
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);
}
}
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace(formatKey(cacheKey) + "served from cache");
}
}
return (view != UNRESOLVED_VIEW ? view : null);
}
}
createView
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());
String[] hosts = getRedirectHosts();
if (hosts != null) {
view.setHosts(hosts);
}
return applyLifecycleMethods(REDIRECT_URL_PREFIX, view);
}
// Check for special "forward:" prefix.
--
//转发为前缀
--
if (viewName.startsWith(FORWARD_URL_PREFIX)) {
String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
InternalResourceView view = new InternalResourceView(forwardUrl);
return applyLifecycleMethods(FORWARD_URL_PREFIX, view);
}
// Else fall back to superclass implementation: calling loadView.
--
//如果没有前缀就使用父类默认创建一个View;
--
return super.createView(viewName, locale);
}
创建好的view
补充:若项目中使用了JSTL,则SpringMVC 会自动把视图由InternalResourceView转为 JstlView (断点调试,将JSTL的jar包增加到项目中,视图解析器会自动修改为JstlView)
我使用的是maven工程,依赖都是默认导入的,所以哪怕我在配置文件配置了InternalResourceViewResolver,也会自动修改
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
返回View对象;
1、视图解析器得到View对象的流程就是,所有配置的视图解析器都来尝试根据视图名(返回值)得到View(视图)对象;
如果能得到就返回,得不到就换下一个视图解析器;
2、调用View对象的render方法;
public void render(@Nullable Map<String, ?> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("View " + formatViewName() +
", model " + (model != null ? model : Collections.emptyMap()) +
(this.staticAttributes.isEmpty() ? "" : ", static attributes " + this.staticAttributes));
}
Map<String, Object> mergedModel = createMergedOutputModel(model, request, response);
prepareResponse(request, response);
--
//渲染要给页面输出的所有数据
--
renderMergedOutputModel(mergedModel, getRequestToExpose(request), response);
}
InternalResourceView的这个方法renderMergedOutputModel;
protected void renderMergedOutputModel(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Expose the model object as request attributes.
--
//将模型中的数据放在请求域中
--
exposeModelAsRequestAttributes(model, request);
// Expose helpers as request attributes, if any.
exposeHelpers(request);
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(request, response);
// Obtain a RequestDispatcher for the target resource (typically a JSP).
RequestDispatcher rd = getRequestDispatcher(request, 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(request, response)) {
response.setContentType(getContentType());
if (logger.isDebugEnabled()) {
logger.debug("Including [" + getUrl() + "]");
}
rd.include(request, response);
}
else {
// Note: The forwarded resource is supposed to determine the content type itself.
if (logger.isDebugEnabled()) {
logger.debug("Forwarding to [" + getUrl() + "]");
}
---
//底层的转发到页面
---
rd.forward(request, response);
}
}
将模型中的所有数据取出来全放在request域中
protected void exposeModelAsRequestAttributes(Map<String, Object> model,
HttpServletRequest request) throws Exception {
model.forEach((name, value) -> {
if (value != null) {
-------------------------
request.setAttribute(name, value);
----------------------------
}
else {
request.removeAttribute(name);
}
});
}
流程总结
视图解析器只是为了得到视图对象;视图对象才能真正的转发(将模型数据全部放在请求域中)或者重定向到页面
视图对象才能真正的渲染视图;
国际化
1、导包导入了jstl的时候会自动创建为一个jstlView,可以支持快速国际化;
1)、javaWeb国际化步骤;
1)、得得到一个Locale对象;
2)、使用ResourceBundle绑定国际化资源文件;
3)、使用ResourceBundle.getString("key");获取到国际化配置文件中的值;
4)、web页面的国际化,fmt标签库来做;
<fmt:setLocale>
<fmt:setBundle >
<fmt:message>
2)、有了JstlView以后;
1)、让Spring管理国际化资源就行
<!--让SpringMVC管理国际化资源文件;配置一个资源文件管理器 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- basename指定基础名-->
<property name="basename" value="message"></property>
<property name="defaultEncoding" value="utf-8"></property>
</bean>
直接去页面使用fmt:message;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success成功</h1>
page content:${pageScope.msg}<br>
request:${requestScope.msg},${requestScope.book}<br>
session:${sessionScope.msg},${requestScope.book},${requestScope.haha}<br>
application:${applicationScope.msg}<br>
<fmt:message key="welcomeinfo"/>
</h1>
<form action="" method="post">
<fmt:message key="username"/>:<input /><br/>
<fmt:message key="password"/>:<input /><br/>
<input type="submit" value='<fmt:message key="loginBtn"/>'/>
</form>
</body>
</html>
存在的问题
国际化问题:我也按一样的配置写国际化和配置文件
但浏览器或者是postman换语言环境都没办法实现语言的转换,总是是中文的显示,除非是没有中文的配置,debug也不明所以然
所以国际化知识上有残缺
以后再补
mvc:view-controller标签
在以前做项目的时候,例如有时候就会写一些空的方法实现重定向等操作,但如此就会在控制层多出很多空方法
若希望直接响应通过 SpringMVC 渲染的页面,可以使用 mvc:view-controller 标签实现
<!-- 直接配置响应的页面:无需经过控制器来执行结果 -->
<mvc:view-controller path="/success" view-name="success"/>
但如此配置会导致其他的请求失败
解决:
<!-- 在实际开发过程中都需要配置mvc:annotation-driven标签,后面讲,这里先配置上 -->
<mvc:annotation-driven/>
自定义视图解析器
MyViewReslover
public class MyViewReslover implements ViewResolver, Ordered {
private int order;
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
--
//能处理就返回你的视图,不能则返回null,进入下一个循环
--
if (viewName.startsWith("myView")){
return new myView();
}else {
return null;
}
}
@Override
public int getOrder() {
return order;
}
--
//实现Ordered以使我们的解析器在其他的解析器前,
//因为InternalResourceViewResolver只是进行拼串,能处理所有返回
--
public void setOrder(int order) {
this.order = order;
}
}
myView
public class myView implements View {
--
//返回的类型
--
@Override
public String getContentType() {
return "text/html";
}
--
//渲染的逻辑,就是你想做些啥
--
@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().println("myView - time = " + new Date());
--
//设置响应内容类型,防止返回乱码。字符过滤器只设置了响应编码
--
response.setContentType("text/html");
response.getWriter().println("model = " + model.get("msg"));
}
}
配置文件web.xml
<bean class="com.lly.View.MyViewReslover">
<property name="order" value="1"/>
</bean>
重定向与转发请求
一般情况下,控制器方法返回字符串类型的值会被当成逻辑视图名处理
如果返回的字符串中带 forward: 或 redirect: 前缀时,SpringMVC 会对他们进行特殊处理:
将 forward: 和 redirect: 当成指示符,其后的字符串作为 URL 来处理
redirect:success.jsp:会完成一个到 success.jsp 的重定向的操作
forward:success.jsp:会完成一个到 success.jsp 的转发操作