7. initRequestToViewNameTranslator 请求视图名

它主要与视图解析有关,如果对ViewResolvers、ModelAndView、View等没有多大印象,可以先看第8节。

RequestToViewNameTranslator主要是获取请求中的viewName,然后可以根据这个viewName获取ModelAndView对象。

RequestToViewNameTranslator接口定义:

public interface RequestToViewNameTranslator {

	// Strategy interface for translating an incoming HttpServletRequest into a logical view name when no view name is explicitly supplied.
String getViewName(HttpServletRequest request) throws Exception; }

简单的来说就是根据request请求获取来组装视图名称,仅此而已。

在DispatcherServlet的doDispatch函数中会设置默认的视图名:

// 设置默认的视图名称
applyDefaultViewName(processedRequest, mv);
applyDefaultViewName中会判断ModelAndView的hasView为空时,就设置viewName: if (mv != null && !mv.hasView()) {
mv.setViewName(getDefaultViewName(request));
}

  

8. initViewResolvers 视图解析器

ViewResolvers是一个List<ViewResolver>类型数据,视图解析是链式的,如果一个视图解析器根据viewName和local参数没有找到对应的View,则开始使用第二的解析器来进行查询,直到找到为止。默认的ViewResolvers为InternalResourceViewResolver:

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

ViewResolver的作用就是通过解析viewName返回一个View对象:

public interface ViewResolver {
View resolveViewName(String viewName, Locale locale) throws Exception;
}

前文指出HandlerAdapter处理Handler请求,会返回一个视图对象ModelAndView。如果这个MdoelAndView Object类的View对象是String实例,则通过ViewResolver接口实现类的resolveViewName方法来转换为View类型,如果MdoelAndView Object类的View对象是Viwe实例,通过getter方法就可以获取View对象。

ModelAndView类的属性定义如下:

public class ModelAndView {
// 两种设置方式,1:String类型 2:View类型。通过isReference()来区分
// 如果view是String类型,则通过ViewResolver接口实现类的resolveViewName方法来转换为View类型
private Object view;
// LinkedHashMap<String, Object>类型
private ModelMap model;
private HttpStatus status;
private boolean cleared = false;
... }

获取到View对象之后,调用View的render方法进行页面渲染。

具体的过程如下:

当请求到达doDispatch后HandlerAdapte处理请求,返回一个MdoelAndView对象mv,然后调用:

this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);

该方法中主要调用render(mv, request, response),它会检查view是什么类型,如果是String类型,则通过ViewResolver接口实现类的resolveViewName方法来转换为View类型,总之最后要获取View实例对象, 调用View接口实现类的render方法,实现页面渲染:

public interface View {
String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus";
String PATH_VARIABLES = View.class.getName() + ".pathVariables";
String SELECTED_CONTENT_TYPE = View.class.getName() + ".selectedContentType"; String getContentType(); // @param model Map with name Strings as keys and corresponding model
void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception;
}

常用的View实现类有:

如果想看一个View渲染的具体流程可以参考《InternalResourceView源码分析(待写)》

ps:不同视图对应不同的视图解析器,如FreeMarkerView就需要FreeMarkerViewResolver来完成解析。

9. initFlashMapManager 重定向属性存储

官方文档中指出:FlashMapManager用于检索和保存FlashMap实例的策略界面

那FlashMap是什么呢?

看看官方文档描述:

/**
* A FlashMap provides a way for one request to store attributes intended for
* use in another. This is most commonly needed when redirecting from one URL
* to another -- e.g. the Post/Redirect/Get pattern. A FlashMap is saved before
* the redirect (typically in the session) and is made available after the
* redirect and removed immediately.
*
* <p>A FlashMap can be set up with a request path and request parameters to
* help identify the target request. Without this information, a FlashMap is
* made available to the next request, which may or may not be the intended
* recipient. On a redirect, the target URL is known and a FlashMap can be
* updated with that information. This is done automatically when the
* {@code org.springframework.web.servlet.view.RedirectView} is used.
*
* <p>Note: annotated controllers will usually not use FlashMap directly.
* See {@code org.springframework.web.servlet.mvc.support.RedirectAttributes}
* for an overview of using flash attributes in annotated controllers.
*
* @author Rossen Stoyanchev
* @since 3.1
* @see FlashMapManager
*/
@SuppressWarnings("serial")
public final class FlashMap extends HashMap<String, Object> implements Comparable<FlashMap> {
...
}

简单翻译:

FlashMap为一个请求提供了一种存储属性的功能,用于另一个请求中使用这些属性。这种方式通常在 一个URL重定向到另一个URL时最常需要 - 例如Post / Redirect / Get模式。FlashMap在重定向之前保存(通常在会话中),并在重定向后可用并立即删除。

可以使用请求路径和请求参数来设置FlashMap,以帮助识别目标请求。如果没有此信息,FlashMap将可用于下一个请求(该请求可能是也可能不是预期)。在一个重定向中,目标URL是已知的,可以使用该信息更新FlashMap,这个过程是自动完成的,它通过org.springframework.web.servlet.view.RedirectView完成此操作。

注意:带注释的控制器通常不会直接使用FlashMap。 有关在带注释的控制器中使用Flash属性的概述,请参阅org.springframework.web.servlet.mvc.support.RedirectAttributes。

通过上面的描述可知,FlashMap和FlashMapManager主要用于重定向数据保存。重定向就不得不提到RedirectView,RedirectView跳转时会将跳转之前的请求中的参数保存到FlashMap中,然后通过FlashManager保存起来:

//获取原请求所携带的数据
FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request); //将数据保存起来,作为跳转之后请求的数据使用
flashMapManager.saveOutputFlashMap(flashMap, request, response);

当重定向的请求在浏览器中重定向之后会再次会被DispathcerServlet进行拦截,在DispatcherServlet的doService方法中,有一步会从FlashManager中获取保存的FlashMap中的值:

FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);

这样重定向的请求就可以使用以前的一些属性值了。

总结:

FlashMap简单来说就是一个HashMap,用于数据保存,主要作用于重定向,springMVC中默认的FlashMapManager为SessionFlashMapManager

org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager

(全文完)

springmvc DispatchServlet初始化九大加载策略(三)的更多相关文章

  1. springmvc DispatchServlet初始化九大加载策略(一)

    由于篇幅较长,因此分三篇进行讲解: springmvc DispatchServlet初始化九大加载策略(一) springmvc DispatchServlet初始化九大加载策略(二) spring ...

  2. springmvc DispatchServlet初始化九大加载策略(二)

    4. initHandlerMappings 请求分发 HandlerMappings是一个List<HandlerMapping>类型数据,也就是说初始化可以存放多种Mapping,和其 ...

  3. 【腾讯Bugly干货分享】彻底弄懂 Http 缓存机制 - 基于缓存策略三要素分解法

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/qOMO0LIdA47j3RjhbCWUEQ 作者:李 ...

  4. Http协议:彻底弄懂 Http 缓存机制 - 基于缓存策略三要素分解法

    转载:http://mp.weixin.qq.com/s/uWPls0qrqJKHkHfNLmaenQ 导语 Http 缓存机制作为 web 性能优化的重要手段,对从事 Web 开发的小伙伴们来说是必 ...

  5. 【死磕 Spring】----- IOC 之 Spring 统一资源加载策略

    原文出自:http://cmsblogs.com 在学 Java SE 的时候我们学习了一个标准类 java.net.URL,该类在 Java SE 中的定位为统一资源定位器(Uniform Reso ...

  6. Solr4.8.0源码分析(22)之SolrCloud的Recovery策略(三)

    Solr4.8.0源码分析(22)之SolrCloud的Recovery策略(三) 本文是SolrCloud的Recovery策略系列的第三篇文章,前面两篇主要介绍了Recovery的总体流程,以及P ...

  7. Spring+SpringMVC+MyBatis+easyUI整合基础篇(三)搭建步骤

    框架介绍: 主角即Spring.SpringMVC.MyBatis.easyUI,大家应该也都有所了解,概念性的东西就不写了,有万能的百度.   工作环境:       jdk 1.7       m ...

  8. hibernate框架学习之数据抓取(加载)策略

    Hibernate获取数据方式 lHibernate提供了多种方式获取数据 •load方法获取数据 •get方法获取数据 •Query/ Criteria对象获取数据 lHibernate获取的数据分 ...

  9. hibernate框架学习第六天:QBC、分页查询、投影、数据加载策略、二级缓存

    QBC查询 1.简单查询 Criteria c = s.createCriteria(TeacherModel.class); 2.获取查询结果 多条:list 单挑:uniqueResult 3.分 ...

随机推荐

  1. Zabbix二次开发_03api列表_中文版

    基于ZABBIX 3.0 https://www.zabbix.com/documentation/3.0/manual/api/reference 参考方法 本节提供了的zabbix提供的功能的概述 ...

  2. setting.xml配置文件 --转载

    转载出处:http://www.cnblogs.com/yakov/archive/2011/11/26/maven2_settings.html 在此,简单的说下 setting.xml 和 pom ...

  3. [UE4]Acotr

    任何能被放在关卡中的对象都是Actor Tick是每帧都会调用的事件

  4. php 编程笔记分享 - 非常实用

    php opendir()列出目录下所有文件的两个实例 php opendir()函数讲解及遍历目录实例 php move_uploaded_file()上传文件实例及遇到问题的解决方法 php使用m ...

  5. ElasticSearch 索引模块——集成IK中文分词

    下载插件地址 https://github.com/medcl/elasticsearch-analysis-ik/tree/v1.10.0 对这个插件在window下进行解压 用maven工具对插件 ...

  6. faker之python构造虚拟数据

    python中可以使用faker来制造一些虚拟数据 首选安装faker pip install Faker 老版的叫法是faker-factory,但是已不适用 使用faker.Factory.cre ...

  7. PHP获取跳转后的URL,存到数据库,设置缓存时间

    <?php error_reporting(0); header("Content-Type: text/html; charset=utf-8"); $fid=$_GET[ ...

  8. sql 随机取数

    Sql server:      select top 10 * from 表 order by newid()Access:      SELECT top 10 * FROM 表 ORDER BY ...

  9. 红帽yum源安装报错initscripts-9.49.41-1.el7.x86_64 conflicts redhat-release &lt; 7.5-0.11" ?

    https://access.redhat.com/solutions/3425111 环境 Red Hat Enterprise Linux 7 问题 yum fails to apply upda ...

  10. leetcode35

    public class Solution { public int SearchInsert(int[] nums, int target) { ; i < nums.Length; i++) ...