KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.springframework.beans.BeanUtils;
25 import org.springframework.core.Ordered;
26 import org.springframework.util.PatternMatchUtils;
27 import org.springframework.web.servlet.View;
28
29 /**
30  * Simple implementation of the {@link org.springframework.web.servlet.ViewResolver}
31  * interface, allowing for direct resolution of symbolic view names to URLs,
32  * without explicit mapping definition. This is useful if your symbolic names
33  * match the names of your view resources in a straightforward manner
34  * (i.e. the symbolic name is the unique part of the resource's filename),
35  * without the need for a dedicated mapping to be defined for each view.
36  *
37  * <p>Supports {@link AbstractUrlBasedView} subclasses like {@link InternalResourceView},
38  * {@link org.springframework.web.servlet.view.velocity.VelocityView} and
39  * {@link org.springframework.web.servlet.view.freemarker.FreeMarkerView}.
40  * The view class for all views generated by this resolver can be specified
41  * via the "viewClass" property.
42  *
43  * <p>View names can either be resource URLs themselves, or get augmented by a
44  * specified prefix and/or suffix. Exporting an attribute that holds the
45  * RequestContext to all views is explicitly supported.
46  *
47  * <p>Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" ->
48  * "/WEB-INF/jsp/test.jsp"
49  *
50  * <p>As a special feature, redirect URLs can be specified via the "redirect:"
51  * prefix. E.g.: "redirect:myAction.do" will trigger a redirect to the given
52  * URL, rather than resolution as standard view name. This is typically used
53  * for redirecting to a controller URL after finishing a form workflow.
54  *
55  * <p>Furthermore, forward URLs can be specified via the "forward:" prefix. E.g.:
56  * "forward:myAction.do" will trigger a forward to the given URL, rather than
57  * resolution as standard view name. This is typically used for controller URLs;
58  * it is not supposed to be used for JSP URLs - use logical view names there.
59  *
60  * <p>Note: This class does not support localized resolution, i.e. resolving
61  * a symbolic view name to different resources depending on the current locale.
62  *
63  * <p>Note: When chaining ViewResolvers, a UrlBasedViewResolver always needs
64  * to be last, as it will attempt to resolve any view name, no matter whether
65  * the underlying resource actually exists.
66  *
67  * @author Juergen Hoeller
68  * @author Rob Harrop
69  * @since 13.12.2003
70  * @see #setViewClass
71  * @see #setPrefix
72  * @see #setSuffix
73  * @see #setRequestContextAttribute
74  * @see #REDIRECT_URL_PREFIX
75  * @see AbstractUrlBasedView
76  * @see InternalResourceView
77  * @see org.springframework.web.servlet.view.velocity.VelocityView
78  * @see org.springframework.web.servlet.view.freemarker.FreeMarkerView
79  */

80 public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered {
81
82     /**
83      * Prefix for special view names that specify a redirect URL (usually
84      * to a controller after a form has been submitted and processed).
85      * Such view names will not be resolved in the configured default
86      * way but rather be treated as special shortcut.
87      */

88     public static final String JavaDoc REDIRECT_URL_PREFIX = "redirect:";
89
90     /**
91      * Prefix for special view names that specify a forward URL (usually
92      * to a controller after a form has been submitted and processed).
93      * Such view names will not be resolved in the configured default
94      * way but rather be treated as special shortcut.
95      */

96     public static final String JavaDoc FORWARD_URL_PREFIX = "forward:";
97
98
99     private Class JavaDoc viewClass;
100
101     private String JavaDoc prefix = "";
102
103     private String JavaDoc suffix = "";
104
105     private String JavaDoc[] viewNames = null;
106
107     private String JavaDoc contentType;
108
109     private boolean redirectContextRelative = true;
110
111     private boolean redirectHttp10Compatible = true;
112
113     private String JavaDoc requestContextAttribute;
114
115     private int order = Integer.MAX_VALUE;
116
117     /** Map of static attributes, keyed by attribute name (String) */
118     private final Map JavaDoc staticAttributes = new HashMap JavaDoc();
119
120
121     /**
122      * Set the view class that should be used to create views.
123      * @param viewClass class that is assignable to the required view class
124      * (by default, AbstractUrlBasedView)
125      * @see AbstractUrlBasedView
126      */

127     public void setViewClass(Class JavaDoc viewClass) {
128         if (viewClass == null || !requiredViewClass().isAssignableFrom(viewClass)) {
129             throw new IllegalArgumentException JavaDoc(
130                     "Given view class [" + (viewClass != null ? viewClass.getName() : null) +
131                     "] is not of type [" + requiredViewClass().getName() + "]");
132         }
133         this.viewClass = viewClass;
134     }
135
136     /**
137      * Return the view class to be used to create views.
138      */

139     protected Class JavaDoc getViewClass() {
140         return this.viewClass;
141     }
142
143     /**
144      * Return the required type of view for this resolver.
145      * This implementation returns AbstractUrlBasedView.
146      * @see AbstractUrlBasedView
147      */

148     protected Class JavaDoc requiredViewClass() {
149         return AbstractUrlBasedView.class;
150     }
151
152     /**
153      * Set the prefix that gets prepended to view names when building a URL.
154      */

155     public void setPrefix(String JavaDoc prefix) {
156         this.prefix = (prefix != null ? prefix : "");
157     }
158
159     /**
160      * Return the prefix that gets prepended to view names when building a URL.
161      */

162     protected String JavaDoc getPrefix() {
163         return prefix;
164     }
165
166     /**
167      * Set the suffix that gets appended to view names when building a URL.
168      */

169     public void setSuffix(String JavaDoc suffix) {
170         this.suffix = (suffix != null ? suffix : "");
171     }
172
173     /**
174      * Return the suffix that gets appended to view names when building a URL.
175      */

176     protected String JavaDoc getSuffix() {
177         return suffix;
178     }
179
180     /**
181      * Set the content type for all views.
182      * <p>May be ignored by view classes if the view itself is assumed
183      * to set the content type, e.g. in case of JSPs.
184      */

185     public void setContentType(String JavaDoc contentType) {
186         this.contentType = contentType;
187     }
188
189     /**
190      * Return the content type for all views, if any.
191      */

192     protected String JavaDoc getContentType() {
193         return contentType;
194     }
195
196     /**
197      * Set whether to interpret a given redirect URL that starts with a
198      * slash ("/") as relative to the current ServletContext, i.e. as
199      * relative to the web application root.
200      * <p>Default is "true": A redirect URL that starts with a slash will be
201      * interpreted as relative to the web application root, i.e. the context
202      * path will be prepended to the URL.
203      * <p><b>Redirect URLs can be specified via the "redirect:" prefix.</b>
204      * E.g.: "redirect:myAction.do"
205      * @see RedirectView#setContextRelative
206      * @see #REDIRECT_URL_PREFIX
207      */

208     public void setRedirectContextRelative(boolean redirectContextRelative) {
209         this.redirectContextRelative = redirectContextRelative;
210     }
211
212     /**
213      * Return whether to interpret a given redirect URL that starts with a
214      * slash ("/") as relative to the current ServletContext, i.e. as
215      * relative to the web application root.
216      */

217     protected boolean isRedirectContextRelative() {
218         return redirectContextRelative;
219     }
220
221     /**
222      * Set whether redirects should stay compatible with HTTP 1.0 clients.
223      * <p>In the default implementation, this will enforce HTTP status code 302
224      * in any case, i.e. delegate to <code>HttpServletResponse.sendRedirect</code>.
225      * Turning this off will send HTTP status code 303, which is the correct
226      * code for HTTP 1.1 clients, but not understood by HTTP 1.0 clients.
227      * <p>Many HTTP 1.1 clients treat 302 just like 303, not making any
228      * difference. However, some clients depend on 303 when redirecting
229      * after a POST request; turn this flag off in such a scenario.
230      * <p><b>Redirect URLs can be specified via the "redirect:" prefix.</b>
231      * E.g.: "redirect:myAction.do"
232      * @see RedirectView#setHttp10Compatible
233      * @see #REDIRECT_URL_PREFIX
234      */

235     public void setRedirectHttp10Compatible(boolean redirectHttp10Compatible) {
236         this.redirectHttp10Compatible = redirectHttp10Compatible;
237     }
238
239     /**
240      * Return whether redirects should stay compatible with HTTP 1.0 clients.
241      */

242     protected boolean isRedirectHttp10Compatible() {
243         return redirectHttp10Compatible;
244     }
245
246     /**
247      * Set the name of the RequestContext attribute for all views.
248      * @param requestContextAttribute name of the RequestContext attribute
249      * @see AbstractView#setRequestContextAttribute
250      */

251     public void setRequestContextAttribute(String JavaDoc requestContextAttribute) {
252         this.requestContextAttribute = requestContextAttribute;
253     }
254
255     /**
256      * Return the name of the RequestContext attribute for all views, if any.
257      */

258     protected String JavaDoc getRequestContextAttribute() {
259         return requestContextAttribute;
260     }
261
262     /**
263      * Set static attributes from a <code>java.util.Properties</code> object,
264      * for all views returned by this resolver.
265      * <p>This is the most convenient way to set static attributes. Note that
266      * static attributes can be overridden by dynamic attributes, if a value
267      * with the same name is included in the model.
268      * <p>Can be populated with a String "value" (parsed via PropertiesEditor)
269      * or a "props" element in XML bean definitions.
270      * @see org.springframework.beans.propertyeditors.PropertiesEditor
271      * @see AbstractView#setAttributes
272      */

273     public void setAttributes(Properties JavaDoc props) {
274         setAttributesMap(props);
275     }
276
277     /**
278      * Set static attributes from a Map, for all views returned by this resolver.
279      * This allows to set any kind of attribute values, for example bean references.
280      * <p>Can be populated with a "map" or "props" element in XML bean definitions.
281      * @param attributes Map with name Strings as keys and attribute objects as values
282      * @see AbstractView#setAttributesMap
283      */

284     public void setAttributesMap(Map JavaDoc attributes) {
285         if (attributes != null) {
286             this.staticAttributes.putAll(attributes);
287         }
288     }
289
290     /**
291      * Allow Map access to the static attributes for views returned by
292      * this resolver, with the option to add or override specific entries.
293      * <p>Useful for specifying entries directly, for example via
294      * "attributesMap[myKey]". This is particularly useful for
295      * adding or overriding entries in child view definitions.
296      */

297     public Map JavaDoc getAttributesMap() {
298         return this.staticAttributes;
299     }
300
301     /**
302      * Set the view names (or name patterns) that can be handled by this
303      * {@link org.springframework.web.servlet.ViewResolver}. View names can contain
304      * simple wildcards such that 'my*', '*Report' and '*Repo*' will all match the
305      * view name 'myReport'.
306      * @see #canHandle
307      */

308     public void setViewNames(String JavaDoc[] viewNames) {
309         this.viewNames = viewNames;
310     }
311
312     /**
313      * Return the view names (or name patterns) that can be handled by this
314      * {@link org.springframework.web.servlet.ViewResolver}.
315      */

316     protected String JavaDoc[] getViewNames() {
317         return viewNames;
318     }
319
320     /**
321      * Set the order in which this {@link org.springframework.web.servlet.ViewResolver}
322      * is evaluated.
323      */

324     public void setOrder(int order) {
325         this.order = order;
326     }
327
328     /**
329      * Return the order in which this {@link org.springframework.web.servlet.ViewResolver}
330      * is evaluated.
331      */

332     public int getOrder() {
333         return order;
334     }
335
336     protected void initApplicationContext() {
337         super.initApplicationContext();
338         if (getViewClass() == null) {
339             throw new IllegalArgumentException JavaDoc("viewClass is required");
340         }
341     }
342
343
344     /**
345      * This implementation returns just the view name,
346      * as this ViewResolver doesn't support localized resolution.
347      */

348     protected Object JavaDoc getCacheKey(String JavaDoc viewName, Locale JavaDoc locale) {
349         return viewName;
350     }
351
352     /**
353      * Overridden to implement check for "redirect:" prefix.
354      * <p>Not possible in <code>loadView</code>, since overridden
355      * <code>loadView</code> versions in subclasses might rely on the
356      * superclass always creating instances of the required view class.
357      * @see #loadView
358      * @see #requiredViewClass
359      */

360     protected View createView(String JavaDoc viewName, Locale JavaDoc locale) throws Exception JavaDoc {
361         // If this resolver is not supposed to handle the given view,
362
// return null to pass on to the next resolver in the chain.
363
if (!canHandle(viewName, locale)) {
364             return null;
365         }
366         // Check for special "redirect:" prefix.
367
if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
368             String JavaDoc redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
369             return new RedirectView(
370                             redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
371         }
372         // Check for special "forward:" prefix.
373
if (viewName.startsWith(FORWARD_URL_PREFIX)) {
374             String JavaDoc forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
375             return new InternalResourceView(forwardUrl);
376         }
377         // Else fall back to superclass implementation: calling loadView.
378
return super.createView(viewName, locale);
379     }
380
381     /**
382      * Indicates whether or not this {@link org.springframework.web.servlet.ViewResolver} can
383      * handle the supplied view name. If not, {@link #createView(String, java.util.Locale)} will
384      * return <code>null</code>. The default implementation checks against the configured
385      * {@link #setViewNames view names}.
386      * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
387      */

388     protected boolean canHandle(String JavaDoc viewName, Locale JavaDoc locale) {
389         String JavaDoc[] viewNames = getViewNames();
390         return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName));
391     }
392
393     /**
394      * Delegates to <code>buildView</code> for creating a new instance of the
395      * specified view class, and applies the following Spring lifecycle methods
396      * (as supported by the generic Spring bean factory):
397      * <ul>
398      * <li>ApplicationContextAware's <code>setApplicationContext</code>
399      * <li>InitializingBean's <code>afterPropertiesSet</code>
400      * </ul>
401      * @see #buildView(String)
402      * @see org.springframework.context.ApplicationContextAware#setApplicationContext
403      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
404      */

405     protected View loadView(String JavaDoc viewName, Locale JavaDoc locale) throws Exception JavaDoc {
406         AbstractUrlBasedView view = buildView(viewName);
407         return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);
408     }
409
410     /**
411      * Creates a new View instance of the specified view class and configures it.
412      * Does <i>not</i> perform any lookup for pre-defined View instances.
413      * <p>Spring lifecycle methods as defined by the bean container do not have to
414      * be called here; those will be applied by the <code>loadView</code> method
415      * after this method returns.
416      * <p>Subclasses will typically call <code>super.buildView(viewName)</code>
417      * first, before setting further properties themselves. <code>loadView</code>
418      * will then apply Spring lifecycle methods at the end of this process.
419      * @see #loadView(String, java.util.Locale)
420      */

421     protected AbstractUrlBasedView buildView(String JavaDoc viewName) throws Exception JavaDoc {
422         AbstractUrlBasedView view = (AbstractUrlBasedView) BeanUtils.instantiateClass(getViewClass());
423         view.setUrl(getPrefix() + viewName + getSuffix());
424         String JavaDoc contentType = getContentType();
425         if (contentType != null) {
426             view.setContentType(contentType);
427         }
428         view.setRequestContextAttribute(getRequestContextAttribute());
429         view.setAttributesMap(getAttributesMap());
430         return view;
431     }
432
433 }
434
Popular Tags