数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换

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

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

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

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


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

[java] view plain copy
 
 print?
  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函数中会设置默认的视图名

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

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

[java] view plain copy
 
 print?
  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中。

[java] view plain copy
 
 print?
  1. protected String getDefaultViewName(HttpServletRequest request) throws Exception {  
  2.         return this.viewNameTranslator.getViewName(request);  
  3.     }  

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

[java] view plain copy
 
 print?
  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的完整源码如下:

[java] view plain copy
 
 print?
    1. public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator {  
    2.   
    3.     private static final String SLASH = "/";  
    4.   
    5.   
    6.     private String prefix = "";  
    7.   
    8.     private String suffix = "";  
    9.   
    10.     private String separator = SLASH;  
    11.   
    12.     private boolean stripLeadingSlash = true;  
    13.   
    14.     private boolean stripTrailingSlash = true;  
    15.   
    16.     private boolean stripExtension = true;  
    17.   
    18.     private UrlPathHelper urlPathHelper = new UrlPathHelper();  
    19.   
    20.   
    21.       
    22.     public void setPrefix(String prefix) {  
    23.         this.prefix = (prefix != null ? prefix : "");  
    24.     }  
    25.   
    26.     public void setSuffix(String suffix) {  
    27.         this.suffix = (suffix != null ? suffix : "");  
    28.     }  
    29.   
    30.     public void setSeparator(String separator) {  
    31.         this.separator = separator;  
    32.     }  
    33.   
    34.     public void setStripLeadingSlash(boolean stripLeadingSlash) {  
    35.         this.stripLeadingSlash = stripLeadingSlash;  
    36.     }  
    37.   
    38.     public void setStripTrailingSlash(boolean stripTrailingSlash) {  
    39.         this.stripTrailingSlash = stripTrailingSlash;  
    40.     }  
    41.   
    42.     public void setStripExtension(boolean stripExtension) {  
    43.         this.stripExtension = stripExtension;  
    44.     }  
    45.   
    46.       
    47.     public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {  
    48.         this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);  
    49.     }  
    50.   
    51.       
    52.     public void setUrlDecode(boolean urlDecode) {  
    53.         this.urlPathHelper.setUrlDecode(urlDecode);  
    54.     }  
    55.   
    56.     public void setRemoveSemicolonContent(boolean removeSemicolonContent) {  
    57.         this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);  
    58.     }  
    59.   
    60.     public void setUrlPathHelper(UrlPathHelper urlPathHelper) {  
    61.         Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");  
    62.         this.urlPathHelper = urlPathHelper;  
    63.     }  
    64.   
    65.     //根据请求获取视图名称  
    66.     @Override  
    67.     public String getViewName(HttpServletRequest request) {  
    68.         String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);  
    69.         return (this.prefix + transformPath(lookupPath) + this.suffix);  
    70.     }  
    71.   
    72.       
    73.     protected String transformPath(String lookupPath) {  
    74.         String path = lookupPath;  
    75.         if (this.stripLeadingSlash && path.startsWith(SLASH)) {  
    76.             path = path.substring(1);  
    77.         }  
    78.         if (this.stripTrailingSlash && path.endsWith(SLASH)) {  
    79.             path = path.substring(0, path.length() - 1);  
    80.         }  
    81.         if (this.stripExtension) {  
    82.             path = StringUtils.stripFilenameExtension(path);  
    83.         }  
    84.         if (!SLASH.equals(this.separator)) {  
    85.             path = StringUtils.replace(path, SLASH, this.separator);  
    86.         }  
    87.         return path;  
    88.     }  
    89.   
    90. }  
原文地址:https://www.cnblogs.com/1020182600HENG/p/6912011.html