第15章 RequestToViewNameTranslator
RequestToViewNameTranslator可以在处理器返回的view为空时使用它根据request获取viewName。Spring MVC提供的实现类只有一个DefaultRequestToViewNameTranslator,这个类也非常简单,只是因为有一些getter/setter方法,所以看起来代码比较多,实际执行解析的只有两个,代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator {
private static final String SLASH = "/";
private String prefix = "";
private String suffix = "";
private String separator = SLASH;
private boolean stripLeadingSlash = true;
private boolean stripTrailingSlash = true;
private boolean stripExtension = true;
private UrlPathHelper urlPathHelper = new UrlPathHelper();
/**
* Set the prefix to prepend to generated view names.
* @param prefix the prefix to prepend to generated view names
*/
public void setPrefix(String prefix) {
this.prefix = (prefix != null ? prefix : "");
}
/**
* Set the suffix to append to generated view names.
* @param suffix the suffix to append to generated view names
*/
public void setSuffix(String suffix) {
this.suffix = (suffix != null ? suffix : "");
}
/**
* Set the value that will replace '{@code /}' as the separator
* in the view name. The default behavior simply leaves '{@code /}'
* as the separator.
*/
public void setSeparator(String separator) {
this.separator = separator;
}
/**
* Set whether or not leading slashes should be stripped from the URI when
* generating the view name. Default is "true".
*/
public void setStripLeadingSlash(boolean stripLeadingSlash) {
this.stripLeadingSlash = stripLeadingSlash;
}
/**
* Set whether or not trailing slashes should be stripped from the URI when
* generating the view name. Default is "true".
*/
public void setStripTrailingSlash(boolean stripTrailingSlash) {
this.stripTrailingSlash = stripTrailingSlash;
}
/**
* Set whether or not file extensions should be stripped from the URI when
* generating the view name. Default is "true".
*/
public void setStripExtension(boolean stripExtension) {
this.stripExtension = stripExtension;
}
/**
* Set if URL lookup should always use the full path within the current servlet
* context. Else, the path within the current servlet mapping is used
* if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
* Default is "false".
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
*/
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
}
/**
* Set if the context path and request URI should be URL-decoded.
* Both are returned <i>undecoded</i> by the Servlet API,
* in contrast to the servlet path.
* <p>Uses either the request encoding or the default encoding according
* to the Servlet spec (ISO-8859-1).
* @see org.springframework.web.util.UrlPathHelper#setUrlDecode
*/
public void setUrlDecode(boolean urlDecode) {
this.urlPathHelper.setUrlDecode(urlDecode);
}
/**
* Set if ";" (semicolon) content should be stripped from the request URI.
* @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
*/
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
}
/**
* Set the {@link org.springframework.web.util.UrlPathHelper} to use for
* the resolution of lookup paths.
* <p>Use this to override the default UrlPathHelper with a custom subclass,
* or to share common UrlPathHelper settings across multiple web components.
*/
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
this.urlPathHelper = urlPathHelper;
}
/**
* Translates the request URI of the incoming {@link HttpServletRequest}
* into the view name based on the configured parameters.
* @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest
* @see #transformPath
*/
public String getViewName(HttpServletRequest request) {
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
return (this.prefix + transformPath(lookupPath) + this.suffix);
}
/**
* Transform the request URI (in the context of the webapp) stripping
* slashes and extensions, and replacing the separator as required.
* @param lookupPath the lookup path for the current request,
* as determined by the UrlPathHelper
* @return the transformed path, with slashes and extensions stripped
* if desired
*/
protected String transformPath(String lookupPath) {
String path = lookupPath;
if (this.stripLeadingSlash && path.startsWith(SLASH)) {
path = path.substring(1);
}
if (this.stripTrailingSlash && path.endsWith(SLASH)) {
path = path.substring(0, path.length() - 1);
}
if (this.stripExtension) {
path = StringUtils.stripFilenameExtension(path);
}
if (!SLASH.equals(this.separator)) {
path = StringUtils.replace(path, SLASH, this.separator);
}
return path;
}
}
getViewName
足接口定义的方法,实际解析时就调用它。在getViewName
中首先从request
获得lookupPath
,然后使用transformPath
方法对其进行处理后加土前缀后缀返回。transformPath
方法的作用简单来说就是根据配置对lookupPath
“掐头去尾换分隔符”,它是根据其中的四个属性的设置来处理的,下面分别解释一下这四个属性,其中用到的Slash是一个静态常量,表示”/“。
stripLeadingSlash
:如果最前面的字符为Slash
是否将其去掉。stripTrailingSlash
:如果最后一个字符为Slash
是否将其去掉。stripExtension
:是否需要去掉扩展名。separator
:如果其值与Slash
不同则用于替换原来的分隔符Slash
。
getViewName
中还使用了可以给返回值添加前缀和后缀的prefix
和suffix
,这些参数都可以配置。可以配置的参数除了这6个外还有4个:urlDecode
、removeSemicolonContent
、alwaysUseFullPath
和urlPathHelper
,前三个参数都是用在urlPathHelper
中的,urlDecode
用于设置url
是否需要编解码,一般默认就行;removeSemicolonContent
在前面已经说过了.用于设置是否删除url
中与分号相关的内容;alwaysUseFullPath
用于设置是否总使用完整路径;urlpathHelper
是用于处理url
的工具,一般使用spring
默认提供的就可以了。RequestToViewNameTranslator
组件非常简单,本章就介绍到这里。