-
Notifications
You must be signed in to change notification settings - Fork 37.8k
/
DefaultRequestToViewNameTranslator.java
208 lines (181 loc) · 7.14 KB
/
DefaultRequestToViewNameTranslator.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.RequestToViewNameTranslator;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.UrlPathHelper;
/**
* {@link RequestToViewNameTranslator} that simply transforms the URI of
* the incoming request into a view name.
*
* <p>Can be explicitly defined as the {@code viewNameTranslator} bean in a
* {@link org.springframework.web.servlet.DispatcherServlet} context.
* Otherwise, a plain default instance will be used.
*
* <p>The default transformation simply strips leading and trailing slashes
* as well as the file extension of the URI, and returns the result as the
* view name with the configured {@link #setPrefix prefix} and a
* {@link #setSuffix suffix} added as appropriate.
*
* <p>The stripping of the leading slash and file extension can be disabled
* using the {@link #setStripLeadingSlash stripLeadingSlash} and
* {@link #setStripExtension stripExtension} properties, respectively.
*
* <p>Find below some examples of request to view name translation.
* <ul>
* <li>{@code http://localhost:8080/gamecast/display.html} » {@code display}</li>
* <li>{@code http://localhost:8080/gamecast/displayShoppingCart.html} » {@code displayShoppingCart}</li>
* <li>{@code http://localhost:8080/gamecast/admin/index.html} » {@code admin/index}</li>
* </ul>
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 2.0
* @see org.springframework.web.servlet.RequestToViewNameTranslator
* @see org.springframework.web.servlet.ViewResolver
*/
public 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;
/**
* Set the prefix to prepend to generated view names.
* @param prefix the prefix to prepend to generated view names
*/
public void setPrefix(@Nullable 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(@Nullable 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;
}
/**
* Shortcut to same property on underlying {@link #setUrlPathHelper UrlPathHelper}.
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
* @deprecated as of 5.3, the path is resolved externally and obtained with
* {@link ServletRequestPathUtils#getCachedPathValue(ServletRequest)}
*/
@Deprecated
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
}
/**
* Shortcut to same property on underlying {@link #setUrlPathHelper UrlPathHelper}.
* @see org.springframework.web.util.UrlPathHelper#setUrlDecode
* @deprecated as of 5.3, the path is resolved externally and obtained with
* {@link ServletRequestPathUtils#getCachedPathValue(ServletRequest)}
*/
@Deprecated
public void setUrlDecode(boolean urlDecode) {
}
/**
* Set if ";" (semicolon) content should be stripped from the request URI.
* @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
* @deprecated as of 5.3, the path is resolved externally and obtained with
* {@link ServletRequestPathUtils#getCachedPathValue(ServletRequest)}
*/
@Deprecated
public void setRemoveSemicolonContent(boolean 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.
* @deprecated as of 5.3, the path is resolved externally and obtained with
* {@link ServletRequestPathUtils#getCachedPathValue(ServletRequest)}
*/
@Deprecated
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
}
/**
* Translates the request URI of the incoming {@link HttpServletRequest}
* into the view name based on the configured parameters.
* @see ServletRequestPathUtils#getCachedPath(ServletRequest)
* @see #transformPath
* @throws IllegalArgumentException if neither a parsed RequestPath, nor a
* String lookupPath have been resolved and cached as a request attribute.
*/
@Override
public String getViewName(HttpServletRequest request) {
String path = ServletRequestPathUtils.getCachedPathValue(request);
return (this.prefix + transformPath(path) + 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
*/
@Nullable
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;
}
}