KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > DefaultRequestToViewNameTranslator


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20
21 import org.springframework.util.StringUtils;
22 import org.springframework.util.Assert;
23 import org.springframework.web.servlet.RequestToViewNameTranslator;
24 import org.springframework.web.util.UrlPathHelper;
25
26 /**
27  * Simply transforms the URI of the incoming request into the view name.
28  *
29  * <p>Can be explicitly defined as the "viewNameTranslator" bean in a
30  * {@link org.springframework.web.servlet.DispatcherServlet} context; else,
31  * a plain default instance will be used.
32  *
33  * <p>The default transformation simply strips the leading slash and file
34  * extension of the URI and returns the result as the view name with the
35  * configured {@link #setPrefix(String) "prefix"} and and
36  * {@link #setSuffix(String) "suffix"} added as appropriate.
37  *
38  * <p>The stripping of the leading slash and file extension can be disabled
39  * using the {@link #setStripLeadingSlash(boolean) "stripLeadingSlash"} and
40  * {@link #setStripExtension(boolean) "stripExtension"} properties,
41  * respectively.
42  *
43  * <p>Find below some examples of request to view name translation.
44  *
45  * <pre class="code"> http://localhost:8080/gamecast/display.html -> display
46  * http://localhost:8080/gamecast/displayShoppingCart.html -> displayShoppingCart
47  * http://localhost:8080/gamecast/admin/index.html -> admin/index
48  * </pre>
49  *
50  * @author Rob Harrop
51  * @since 2.0
52  * @see org.springframework.web.servlet.RequestToViewNameTranslator
53  * @see org.springframework.web.servlet.ViewResolver
54  */

55 public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator {
56
57     private static final String JavaDoc SLASH = "/";
58
59
60     private String JavaDoc prefix = "";
61
62     private String JavaDoc suffix = "";
63
64     private String JavaDoc separator = SLASH;
65
66     private boolean stripLeadingSlash = true;
67
68     private boolean stripExtension = true;
69
70     private UrlPathHelper urlPathHelper = new UrlPathHelper();
71
72
73     /**
74      * Set the prefix to prepend to generated view names.
75      * @param prefix the prefix to prepend to generated view names
76      */

77     public void setPrefix(String JavaDoc prefix) {
78         this.prefix = (prefix == null ? "" : prefix);
79     }
80
81     /**
82      * Set the suffix to append to generated view names.
83      * @param suffix the suffix to append to generated view names
84      */

85     public void setSuffix(String JavaDoc suffix) {
86         this.suffix = (suffix == null ? "" : suffix);
87     }
88
89     /**
90      * Set the value that will replace '<code>/</code>' as the separator
91      * in the view name. The default behavior simply leaves '<code>/</code>'
92      * as the separator.
93      * @param separator the desired separator value
94      */

95     public void setSeparator(String JavaDoc separator) {
96         this.separator = separator;
97     }
98
99     /**
100      * Set whether or not leading slashes should be stripped from the URI when
101      * generating the view name. Default is "true".
102      * @param stripLeadingSlash <code>true</code> if leading slashes are to be stripped
103      */

104     public void setStripLeadingSlash(boolean stripLeadingSlash) {
105         this.stripLeadingSlash = stripLeadingSlash;
106     }
107
108     /**
109      * Set whether or not file extensions should be stripped from the URI when
110      * generating the view name. Default is "true".
111      * @param stripExtension <code>true</code> if file extensions should be stripped
112      */

113     public void setStripExtension(boolean stripExtension) {
114         this.stripExtension = stripExtension;
115     }
116
117     /**
118      * Set if URL lookup should always use the full path within the current servlet
119      * context. Else, the path within the current servlet mapping is used
120      * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
121      * Default is "false".
122      * @param alwaysUseFullPath <code>true</code> if URL lookup should always use the full path
123      * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
124      */

125     public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
126         this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
127     }
128
129     /**
130      * Set if the context path and request URI should be URL-decoded.
131      * Both are returned <i>undecoded</i> by the Servlet API,
132      * in contrast to the servlet path.
133      * <p>Uses either the request encoding or the default encoding according
134      * to the Servlet spec (ISO-8859-1).
135      * <p>Note: Setting this to "true" requires JDK 1.4 if the encoding differs
136      * from the VM's platform default encoding, as JDK 1.3's URLDecoder class
137      * does not offer a way to specify the encoding.
138      * @param urlDecode <code>true</code> if the context path and request URI should be URL-decoded
139      * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
140      */

141     public void setUrlDecode(boolean urlDecode) {
142         this.urlPathHelper.setUrlDecode(urlDecode);
143     }
144
145     /**
146      * Set the {@link org.springframework.web.util.UrlPathHelper} to use for
147      * the resolution of lookup paths.
148      * <p>Use this to override the default UrlPathHelper with a custom subclass,
149      * or to share common UrlPathHelper settings across multiple web components.
150      * @param urlPathHelper the desired helper
151      * @throws IllegalArgumentException if the supplied UrlPathHelper is <code>null</code>
152      */

153     public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
154         Assert.notNull(urlPathHelper);
155         this.urlPathHelper = urlPathHelper;
156     }
157
158
159     /**
160      * Translates the request URI of the incoming {@link HttpServletRequest} to the
161      * view name based on the configured parameters.
162      * @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest
163      * @see #transformPath
164      */

165     public final String JavaDoc getViewName(HttpServletRequest JavaDoc request) {
166         String JavaDoc lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
167         return this.prefix + transformPath(lookupPath) + this.suffix;
168     }
169
170
171     /**
172      * Transform the request URI (in the context of the webapp) stripping
173      * slashes and extensions, and replacing the separator as required.
174      * @param lookupPath the lookup path for the current request,
175      * as determined by the UrlPathHelper
176      * @return the transformed path, with slashes and extensions stripped
177      * if desired
178      */

179     protected String JavaDoc transformPath(String JavaDoc lookupPath) {
180         String JavaDoc path = lookupPath;
181         if (this.stripLeadingSlash && path.startsWith(SLASH)) {
182             path = path.substring(1);
183         }
184         if (this.stripExtension) {
185             path = StringUtils.stripFilenameExtension(path);
186         }
187         if (!SLASH.equals(this.separator)) {
188             path = StringUtils.replace(path, SLASH, this.separator);
189         }
190         return path;
191     }
192
193 }
194
Popular Tags