参考来源:http://blog.csdn.net/qq924862077/article/details/54286976?utm_source=gold_browser_extension

RequestToViewNameTranslator可以在处理器返回的View为空时使用它根据Request获取viewName。RequestToViewNameTranslator提供的实现类只有一个DefaultRequestToViewNameTranslator。

接口RequestToViewNameTranslator中定义的如下:提供了getViewName抽象方法,其实就是根据request请求获取来组装视图名称。

  1. public interface RequestToViewNameTranslator {
  2. /**
  3. * Translate the given {@link HttpServletRequest} into a view name.
  4. * @param request the incoming {@link HttpServletRequest} providing
  5. * the context from which a view name is to be resolved
  6. * @return the view name (or {@code null} if no default found)
  7. * @throws Exception if view name translation fails
  8. */
  9. String getViewName(HttpServletRequest request) throws Exception;
  10. }

其实现类DefaultRequestToViewNameTranslator中的实现如下:其实其简单实现就是将请求名称作为视图名称返回,逻辑还是比较简单的。

  1. @Override
  2. public String getViewName(HttpServletRequest request) {
  3. String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
  4. return (this.prefix + transformPath(lookupPath) + this.suffix);
  5. }

接下来我们看看RequestToViewNameTranslator在springMVC中的具体运行流程:

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

  1. protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
  2. ......
  3. //设置默认的视图名称
  4. applyDefaultViewName(processedRequest, mv);
  5. ......
  6. }

在applyDefaultViewName中会判断ModelAndView的hasView为空时,就设置viewName

  1. private void applyDefaultViewName(HttpServletRequest request, ModelAndView mv) throws Exception {
  2. if (mv != null && !mv.hasView()) {
  3. mv.setViewName(getDefaultViewName(request));
  4. }
  5. }

getDefaultViewName的实现逻辑还是在ViewNameTranslator中。

  1. protected String getDefaultViewName(HttpServletRequest request) throws Exception {
  2. return this.viewNameTranslator.getViewName(request);
  3. }

在DefaultViewNameTranslator中实现的getViewName的逻辑如下,其实就是将请求路径作为ViewName

  1. @Override
  2. public String getViewName(HttpServletRequest request) {
  3. String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
  4. return (this.prefix + transformPath(lookupPath) + this.suffix);
  5. }

实现类DefaultViewNameTranslator的完整源码如下:

    1. public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator {
    2. private static final String SLASH = "/";
    3. private String prefix = "";
    4. private String suffix = "";
    5. private String separator = SLASH;
    6. private boolean stripLeadingSlash = true;
    7. private boolean stripTrailingSlash = true;
    8. private boolean stripExtension = true;
    9. private UrlPathHelper urlPathHelper = new UrlPathHelper();
    10. public void setPrefix(String prefix) {
    11. this.prefix = (prefix != null ? prefix : "");
    12. }
    13. public void setSuffix(String suffix) {
    14. this.suffix = (suffix != null ? suffix : "");
    15. }
    16. public void setSeparator(String separator) {
    17. this.separator = separator;
    18. }
    19. public void setStripLeadingSlash(boolean stripLeadingSlash) {
    20. this.stripLeadingSlash = stripLeadingSlash;
    21. }
    22. public void setStripTrailingSlash(boolean stripTrailingSlash) {
    23. this.stripTrailingSlash = stripTrailingSlash;
    24. }
    25. public void setStripExtension(boolean stripExtension) {
    26. this.stripExtension = stripExtension;
    27. }
    28. public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
    29. this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
    30. }
    31. public void setUrlDecode(boolean urlDecode) {
    32. this.urlPathHelper.setUrlDecode(urlDecode);
    33. }
    34. public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
    35. this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
    36. }
    37. public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
    38. Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
    39. this.urlPathHelper = urlPathHelper;
    40. }
    41. //根据请求获取视图名称
    42. @Override
    43. public String getViewName(HttpServletRequest request) {
    44. String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
    45. return (this.prefix + transformPath(lookupPath) + this.suffix);
    46. }
    47. protected String transformPath(String lookupPath) {
    48. String path = lookupPath;
    49. if (this.stripLeadingSlash && path.startsWith(SLASH)) {
    50. path = path.substring(1);
    51. }
    52. if (this.stripTrailingSlash && path.endsWith(SLASH)) {
    53. path = path.substring(0, path.length() - 1);
    54. }
    55. if (this.stripExtension) {
    56. path = StringUtils.stripFilenameExtension(path);
    57. }
    58. if (!SLASH.equals(this.separator)) {
    59. path = StringUtils.replace(path, SLASH, this.separator);
    60. }
    61. return path;
    62. }
    63. }

数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换的更多相关文章

  1. springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换

    RequestToViewNameTranslator可以在处理器返回的View为空时使用它根据Request获取viewName.RequestToViewNameTranslator提供的实现类只 ...

  2. springMVC源码分析--DispatcherServlet请求获取及处理

    在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...

  3. springMVC源码分析--访问请求执行ServletInvocableHandlerMethod和InvocableHandlerMethod

    在之前一篇博客中springMVC源码分析--RequestMappingHandlerAdapter(五)我们已经简单的介绍到具体请求访问的执行某个Controller中的方法是在RequestMa ...

  4. springMVC源码分析--FlashMap和FlashMapManager重定向数据保存

    在上一篇博客springMVC源码分析--页面跳转RedirectView(三)中我们看到了在RedirectView跳转时会将跳转之前的请求中的参数保存到fFlashMap中,然后通过FlashMa ...

  5. springMVC源码分析--HandlerInterceptor拦截器调用过程(二)

    在上一篇博客springMVC源码分析--HandlerInterceptor拦截器(一)中我们介绍了HandlerInterceptor拦截器相关的内容,了解到了HandlerInterceptor ...

  6. springMVC源码分析--页面跳转RedirectView(三)

    之前两篇博客springMVC源码分析--视图View(一)和springMVC源码分析--视图AbstractView和InternalResourceView(二)中我们已经简单的介绍了View相 ...

  7. springMVC源码分析--视图AbstractView和InternalResourceView(二)

    上一篇博客springMVC源码分析--视图View(一)中我们介绍了简单介绍了View的结构实现及运行流程,接下来我们介绍一下View的实现类做的处理操作. AbstractView实现了rende ...

  8. springMVC源码分析--视图View(一)

    之前的博客springMVC源码分析--HttpMessageConverter数据转化(一)中我们已经介绍了数据返回值的处理,在博客springMVC源码分析--ViewResolver视图解析器( ...

  9. springMVC源码分析--HttpMessageConverter写write操作(三)

    上一篇博客springMVC源码分析--HttpMessageConverter参数read操作中我们已经简单介绍了参数值转换的read操作,接下来我们介绍一下返回值的处理操作.同样返回值的操作操作也 ...

随机推荐

  1. [bzoj4827][Hnoi2017]礼物_FFT

    礼物 bzoj-4827 Hnoi-2017 题目大意:给定两个长度为$n$的手环,第一个手环上的$n$个权值为$x_i$,第二个为$y_i$.现在我可以同时将所有的$x_i$同时加上自然数$c$.我 ...

  2. 技术杂记之:在阿里云centos7上部署JDK MYSQL TOMCAT

    今日小编闲来无事,乘着公司新项目即将上线之际,在阿里云上整了一台centos作为测试机.原本以为一个小时搞定,结果还是花了一点小小时间.不管怎么说,记录下来,给各位小白当成课后甜点吧. 价格 先上价格 ...

  3. CSS3的animation功能

    旋转动画 <img src="https://facebook.github.io/react/img/logo.svg" class="App-logo" ...

  4. dubbo的泛化调用研究

    结论: 泛化调用需要继承一个类,在配置文件里需要明确指出generic=true; 泛化调用在书写provider代码时,变化不大: 泛化调用和普通调用的区别主要在consumer,从‘调用’的表面意 ...

  5. c++11中的线程、锁和条件变量

    void func(int i, double d, const string& s) { cout << i << ", " << d ...

  6. 【python】一些好的学习网址

    http://www.cnblogs.com/BeginMan/p/3179302.html http://www.cnblogs.com/huxi/category/251137.html http ...

  7. 网页页面NULL值对浏览器兼容性的影响

    网页页面NULL值对浏览器兼容性的影响       近期做项目中一个页面中的input radio出现浏览器兼容性问题. 主要问题: 在谷歌浏览器,360急速模式和搜狗急速模式中给radio初始动态赋 ...

  8. Flink内存管理源代码解读之基础数据结构

    概述 在分布式实时计算领域,怎样让框架/引擎足够高效地在内存中存取.处理海量数据是一个非常棘手的问题.在应对这一问题上Flink无疑是做得非常杰出的,Flink的自主内存管理设计或许比它自身的知名度更 ...

  9. $.extent()的理解

    $.extend()主要是用来扩展插件的,所谓的插件就是封装好的函数或者方法,可以直接调用. $.extend()与$.fn.extend()(或者写成$.prototype.extend()或者jq ...

  10. hdu1863

    #include<cstdio> #include<algorithm> using namespace std; int N,M; struct edge { int u,v ...