spring.xml配置视图解析器如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.atchina"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean>
</beans>

方法执行后的返回值会作为页面地址参考,转发或者重定向到页面。

视图解析器可能会进行页面地址的拼串。

forward前缀指定一个转发操作

/***  forward:转发到一个页面*  /hello.jsp 转发当前项目下的hello.jsp*  *  forward:/hello.jsp, 视图解析器不会在这个返回值加前后缀*  forward:/hello.jsp 一定要加上/,如果不加/就是相对路径,容易出问题*/@RequestMapping("/hello3")public String hello3(){return "forward:/hello.jsp";}/***  forward:转发到一个请求*/@RequestMapping("/hello4")public String hello4(){return "forward:/hello3";}

重定向redirect

/***  重定向到hello.jsp页面 , 视图解析器不会在这个返回值加前后缀*  转发:   forward: 转发的路径*  重定向: redirect:重定向的路径*         /hello.jsp 代表就是从当前项目下开始,SpringMvc会为路径自动的拼接上项目名  */@RequestMapping("/hello5")public String hello5(){return "redirect:/hello.jsp";}/***  重定向一个请求*/@RequestMapping("/hello6")public String hello6(){return "redirect:/hello5";}

视图解析流程

1. 任何方法的返回值,最终都会被包装成ModelAndView对象。

2. 该方法处理视图渲染流程,将域中的数据在页面展示:页面就是用来渲染模型数据的。

processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);  // 来到页面的方法

3. 调用render(mv, request, response);渲染页面

4. View 和 ViewResolver

ViewResolver的作用是根据视图名(方法的返回值)得到View对象。

public interface ViewResolver {/*** Resolve the given view by name.* <p>Note: To allow for ViewResolver chaining, a ViewResolver should* return {@code null} if a view with the given name is not defined in it.* However, this is not required: Some ViewResolvers will always attempt* to build View objects with the given name, unable to return {@code null}* (rather throwing an exception when View creation failed).* @param viewName name of the view to resolve* @param locale Locale in which to resolve the view.* ViewResolvers that support internationalization should respect this.* @return the View object, or {@code null} if not found* (optional, to allow for ViewResolver chaining)* @throws Exception if the view cannot be resolved* (typically in case of problems creating an actual View object)*/View resolveViewName(String viewName, Locale locale) throws Exception;}

5. 怎么根据方法的返回值(视图名)得到View对象?

所有配置的视图解析器都会尝试根据视图名(方法的返回值)得到View(视图)对象;如果能得到View对象就返回,得不到就换下一个视图解析器。

protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale,HttpServletRequest request) throws Exception {for (ViewResolver viewResolver : this.viewResolvers) {// ViewResolver视图解析器根据方法的返回值,得到一个View对象View view = viewResolver.resolveViewName(viewName, locale);if (view != null) {return view;}}return null;}

调用View对象的render方法

@Overridepublic 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, getRequestToExpose(request), response);}

InternalResourceView类

@Overrideprotected 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 resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");}rd.include(request, 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(request, response);}}

总结: 视图解析器只是为了得到视图对象;视图对象才能真正的转发(将模型数据全部放在请求域中)或者重定向到页面;视图对象才能真正的渲染视图。

springmvc十六:视图解析相关推荐

  1. springmvc多个视图解析器管理跳转资源

    springmvc多个视图解析器管理跳转资源 项目结构 第一步:将所有资源交由springmvc管理 <servlet><servlet-name>springmvc</ ...

  2. springmvc如何使用视图解析器_SpringMVC的工作原理

    SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherServlet收到请求调用HandlerMapp ...

  3. springmvc如何使用视图解析器_SpringMVC工作原理

    SpringMVC工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherServlet收到请求调用HandlerMappi ...

  4. springmvc十六:九大组件

    DispatcherServlet中有九个引用类型的属性,这就是springmvc的九大组件. springmvc在工作的时候,关键位置都是由这些组件完成的. /** MultipartResolve ...

  5. springmvc如何使用视图解析器_SpringMVC相关面试题

    点击蓝字 关注我们 最近面试时,每家公司的必问题,今日作此分享,如有更好的解决方案或者存在错误,还请指正. Q1 什么是 SpringMvc? SpringMvc 是 spring 的一个模块,基于 ...

  6. springmvc配置thymeleaf视图解析器

    引入thymeleaf的依赖 <dependency><groupId>org.thymeleaf</groupId><artifactId>thyme ...

  7. 【SpringMVC】SpringMVC模型数据+视图解析器

    目录 一.模型数据-如何将数据存入request域 二.模型数据-如何将数据存入session域 三.@ModelAttribute 四.视图解析器 相关文章 [SpringMVC]入门篇:带你了解S ...

  8. 【SpringMVC笔记05】SpringMVC中的视图解析器

    这篇文章,主要介绍SpringMVC框架中的几种视图解析器. 目录 一.视图解析器 1.1.什么是视图解析器 1.2.InternalResourceViewResolver 1.3.UrlBased ...

  9. springmvc中的视图解析器详细配置

    视图解析器 springmvc中View Resolver负责将处理结果生成View视图,处理器执行完业务逻辑后将带有逻辑视图的ModelAndView返回给前端控制器,前端控制器再将ModelAnd ...

最新文章

  1. windows调用ubuntu下的sublimeText2环境搭建
  2. [转] Boost智能指针——scoped_ptr
  3. tfrecord文件生成与读取
  4. 04_面向初学者的快速入门、建立图像分类的一个神经网络、训练这个神经网络、评估模型的精确度
  5. C#中采用自定义方式读取自定义配置文件
  6. EM算法应用:k均值聚类(k-means)和高斯混合模型(GMM)
  7. jQuery常用的元素查找方法总结 .
  8. 【Python3网络爬虫开发实战】1.4.1-MySQL的安装
  9. 红魔3电竞手机配置进一步曝光 电池容量不低于5000mAh
  10. HeroKu PaaS模式
  11. Windows2008r2如何关闭防火墙
  12. SNAP 4. 使用snap进行地物光谱分析
  13. 摆脱五彩斑斓的黑,成为七彩程序员!
  14. 【英语流利说】让你发音更标准的十个窍门
  15. Antv踩坑—导入画布数据监听画布渲染完成事件
  16. 平均风向计算中对于风向角的判断
  17. c语言统计英文字母频率,C语言实现英文文本词频统计
  18. c语言单元测试(cunit)打桩,如何写打桩文件
  19. vuePress侧边栏sidebar分组自动生成的实现
  20. 统计字符串中出现频率最高的字母 C语言

热门文章

  1. 运用BT在centos下搭建一个博客论坛
  2. Docker常用操作命令
  3. Day9:yield的表达式形式、面向过程编程(grep -rl 'root' /etc)
  4. 大型网站技术架构:核心原理与案例分析阅读笔记二
  5. asp.net MVC 路由机制 Route
  6. C#获取邮件客户端保存的邮箱密码
  7. xslt中的Javascript取得xml中的参数
  8. 汇编语言中,DS与BX有何区别?怎么搭配使用?(BX是通用寄存器)
  9. 关于Two pointers的个人理解
  10. c# 第9节 数据类型之引用类型