KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > support > RequestContext


1 /*
2  * Copyright 2002-2007 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.support;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.springframework.context.MessageSourceResolvable;
29 import org.springframework.context.NoSuchMessageException;
30 import org.springframework.ui.context.Theme;
31 import org.springframework.ui.context.ThemeSource;
32 import org.springframework.ui.context.support.ResourceBundleThemeSource;
33 import org.springframework.validation.BindException;
34 import org.springframework.validation.BindingResult;
35 import org.springframework.validation.Errors;
36 import org.springframework.web.bind.EscapedErrors;
37 import org.springframework.web.context.WebApplicationContext;
38 import org.springframework.web.servlet.LocaleResolver;
39 import org.springframework.web.util.HtmlUtils;
40 import org.springframework.web.util.UrlPathHelper;
41 import org.springframework.web.util.WebUtils;
42
43 /**
44  * Context holder for request-specific state, like current web application
45  * context, current locale, current theme, and potential binding errors.
46  * Provides easy access to localized messages and Errors instances.
47  *
48  * <p>Suitable for exposition to views, and usage within JSP's "useBean" tag,
49  * JSP scriptlets, JSTL EL, Velocity templates, etc. Necessary for views
50  * that do not have access to the servlet request, like Velocity templates.
51  *
52  * <p>Can be instantiated manually, or automatically exposed to views as
53  * model attribute via AbstractView's "requestContextAttribute" property.
54  *
55  * <p>Will also work outside DispatcherServlet requests, accessing the root
56  * WebApplicationContext and using an appropriate fallback for the locale
57  * (the JSTL locale if available, or the HttpServletRequest locale else).
58  *
59  * @author Juergen Hoeller
60  * @since 03.03.2003
61  * @see org.springframework.web.servlet.DispatcherServlet
62  * @see org.springframework.web.servlet.view.AbstractView#setRequestContextAttribute
63  * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setRequestContextAttribute
64  * @see #getFallbackLocale
65  */

66 public class RequestContext {
67
68     /**
69      * Default theme name used if the RequestContext cannot find a ThemeResolver.
70      * Only applies to non-DispatcherServlet requests.
71      * <p>Same as AbstractThemeResolver's default, but not linked in here to
72      * avoid package interdependencies.
73      * @see org.springframework.web.servlet.theme.AbstractThemeResolver#ORIGINAL_DEFAULT_THEME_NAME
74      */

75     public final static String JavaDoc DEFAULT_THEME_NAME = "theme";
76
77     /**
78      * JSTL locale attribute, as used by JSTL implementations to expose their
79      * current locale. Used as fallback in non-DispatcherServlet requests; if not
80      * available, the accept-header locale is used (<code>request.getLocale</code).
81      * <p>Same as the FMT_LOCALE constant in JSTL's Config class, but not linked
82      * in here to avoid a hard-coded dependency on JSTL. RequestContext does not
83      * depend on JSTL except for this fallback check of JSTL's locale attribute.
84      * @see javax.servlet.jsp.jstl.core.Config#FMT_LOCALE
85      * @see javax.servlet.http.HttpServletRequest#getLocale
86      */

87     public final static String JavaDoc JSTL_LOCALE_ATTRIBUTE = "javax.servlet.jsp.jstl.fmt.locale";
88
89
90     /** JSTL suffix for request-scoped attributes */
91     protected static final String JavaDoc REQUEST_SCOPE_SUFFIX = ".request";
92
93     /** JSTL suffix for session-scoped attributes */
94     protected static final String JavaDoc SESSION_SCOPE_SUFFIX = ".session";
95
96     /** JSTL suffix for application-scoped attributes */
97     protected static final String JavaDoc APPLICATION_SCOPE_SUFFIX = ".application";
98
99
100     private HttpServletRequest JavaDoc request;
101
102     private Map JavaDoc model;
103
104     private WebApplicationContext webApplicationContext;
105
106     private Locale JavaDoc locale;
107
108     private Theme theme;
109
110     private boolean defaultHtmlEscape;
111
112     private UrlPathHelper urlPathHelper;
113
114     private Map JavaDoc errorsMap;
115
116
117     /**
118      * Create a new RequestContext for the given request,
119      * using the request attributes for Errors retrieval.
120      * <p>This only works with InternalResourceViews, as Errors instances
121      * are part of the model and not normally exposed as request attributes.
122      * It will typically be used within JSPs or custom tags.
123      * <p><b>Will only work within a DispatcherServlet request.</b> Pass in a
124      * ServletContext to be able to fallback to the root WebApplicationContext.
125      * @param request current HTTP request
126      * @see org.springframework.web.servlet.DispatcherServlet
127      * @see #RequestContext(javax.servlet.http.HttpServletRequest, javax.servlet.ServletContext)
128      */

129     public RequestContext(HttpServletRequest JavaDoc request) {
130         initContext(request, null, null);
131     }
132
133     /**
134      * Create a new RequestContext for the given request,
135      * using the request attributes for Errors retrieval.
136      * <p>This only works with InternalResourceViews, as Errors instances
137      * are part of the model and not normally exposed as request attributes.
138      * It will typically be used within JSPs or custom tags.
139      * <p>If a ServletContext is specified, the RequestContext will also
140      * work with the root WebApplicationContext (outside a DispatcherServlet).
141      * @param request current HTTP request
142      * @param servletContext the servlet context of the web application
143      * (can be <code>null</code>; necessary for fallback to root WebApplicationContext)
144      * @see org.springframework.web.context.WebApplicationContext
145      * @see org.springframework.web.servlet.DispatcherServlet
146      */

147     public RequestContext(HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext) {
148         initContext(request, servletContext, null);
149     }
150
151     /**
152      * Create a new RequestContext for the given request,
153      * using the given model attributes for Errors retrieval.
154      * <p>This works with all View implementations.
155      * It will typically be used by View implementations.
156      * <p><b>Will only work within a DispatcherServlet request.</b> Pass in a
157      * ServletContext to be able to fallback to the root WebApplicationContext.
158      * @param request current HTTP request
159      * @param model the model attributes for the current view
160      * (can be <code>null</code>, using the request attributes for Errors retrieval)
161      * @see org.springframework.web.servlet.DispatcherServlet
162      * @see #RequestContext(javax.servlet.http.HttpServletRequest, javax.servlet.ServletContext, Map)
163      */

164     public RequestContext(HttpServletRequest JavaDoc request, Map JavaDoc model) {
165         initContext(request, null, model);
166     }
167
168     /**
169      * Create a new RequestContext for the given request,
170      * using the given model attributes for Errors retrieval.
171      * <p>This works with all View implementations.
172      * It will typically be used by View implementations.
173      * <p>If a ServletContext is specified, the RequestContext will also
174      * work with a root WebApplicationContext (outside a DispatcherServlet).
175      * @param request current HTTP request
176      * @param servletContext the servlet context of the web application
177      * (can be <code>null</code>; necessary for fallback to root WebApplicationContext)
178      * @param model the model attributes for the current view
179      * (can be <code>null</code>, using the request attributes for Errors retrieval)
180      * @see org.springframework.web.context.WebApplicationContext
181      * @see org.springframework.web.servlet.DispatcherServlet
182      */

183     public RequestContext(HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext, Map JavaDoc model) {
184         initContext(request, servletContext, model);
185     }
186
187     /**
188      * Default constructor for subclasses.
189      */

190     protected RequestContext() {
191     }
192
193
194     /**
195      * Initialize this context with the given request,
196      * using the given model attributes for Errors retrieval.
197      * <p>Delegates to <code>getFallbackLocale</code> and <code>getFallbackTheme</code>
198      * for determining the fallback locale and theme, respectively, if no LocaleResolver
199      * and/or ThemeResolver can be found in the request.
200      * @param request current HTTP request
201      * @param servletContext the servlet context of the web application
202      * (can be <code>null</code>; necessary for fallback to root WebApplicationContext)
203      * @param model the model attributes for the current view
204      * (can be <code>null</code>, using the request attributes for Errors retrieval)
205      * @see #getFallbackLocale
206      * @see #getFallbackTheme
207      * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
208      * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
209      */

210     protected void initContext(HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext, Map JavaDoc model) {
211         this.request = request;
212         this.model = model;
213
214         // Fetch WebApplicationContext, either from DispatcherServlet or the root context.
215
// ServletContext needs to be specified to be able to fall back to the root context!
216
this.webApplicationContext = RequestContextUtils.getWebApplicationContext(request, servletContext);
217
218         // Determine locale to use for this RequestContext.
219
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
220         if (localeResolver != null) {
221             // Try LocaleResolver (we're within a DispatcherServlet request).
222
this.locale = localeResolver.resolveLocale(request);
223         }
224         else {
225             // No LocaleResolver available -> try fallback.
226
this.locale = getFallbackLocale();
227         }
228
229         // Determine theme to use for this RequestContext.
230
this.theme = RequestContextUtils.getTheme(request);
231         if (this.theme == null) {
232             // No ThemeResolver and ThemeSource available -> try fallback.
233
this.theme = getFallbackTheme();
234         }
235
236         // Determine default HTML escape setting from the "defaultHtmlEscape"
237
// context-param in web.xml, if any.
238
this.defaultHtmlEscape = WebUtils.isDefaultHtmlEscape(this.webApplicationContext.getServletContext());
239
240         this.urlPathHelper = new UrlPathHelper();
241     }
242
243     /**
244      * Determine the fallback locale for this context.
245      * <p>Default implementation checks for a JSTL locale attribute
246      * in request, session or application scope; if not found,
247      * returns the <code>HttpServletRequest.getLocale()</code>.
248      * @return the fallback locale (never <code>null</code>)
249      * @see javax.servlet.http.HttpServletRequest#getLocale
250      */

251     protected Locale JavaDoc getFallbackLocale() {
252         Locale JavaDoc locale = (Locale JavaDoc) getRequest().getAttribute(JSTL_LOCALE_ATTRIBUTE);
253         if (locale == null) {
254             locale = (Locale JavaDoc) getRequest().getAttribute(JSTL_LOCALE_ATTRIBUTE + REQUEST_SCOPE_SUFFIX);
255             if (locale == null) {
256                 HttpSession JavaDoc session = getRequest().getSession(false);
257                 if (session != null) {
258                     locale = (Locale JavaDoc) session.getAttribute(JSTL_LOCALE_ATTRIBUTE);
259                     if (locale == null) {
260                         locale = (Locale JavaDoc) session.getAttribute(JSTL_LOCALE_ATTRIBUTE + SESSION_SCOPE_SUFFIX);
261                     }
262                 }
263                 if (locale == null) {
264                     locale = (Locale JavaDoc) getServletContext().getAttribute(JSTL_LOCALE_ATTRIBUTE);
265                     if (locale == null) {
266                         locale = (Locale JavaDoc) getServletContext().getAttribute(JSTL_LOCALE_ATTRIBUTE + APPLICATION_SCOPE_SUFFIX);
267                         if (locale == null) {
268                             // No JSTL locale available -> fall back to accept-header locale.
269
locale = getRequest().getLocale();
270                         }
271                     }
272                 }
273             }
274         }
275         return locale;
276     }
277
278     /**
279      * Determine the fallback theme for this context.
280      * <p>Default implementation returns the default theme (with name "theme").
281      * @return the fallback theme, or <code>null
282      */

283     protected Theme getFallbackTheme() {
284         ThemeSource themeSource = RequestContextUtils.getThemeSource(getRequest());
285         if (themeSource == null) {
286             themeSource = new ResourceBundleThemeSource();
287         }
288         return themeSource.getTheme(DEFAULT_THEME_NAME);
289     }
290
291
292     /**
293      * Return the underlying HttpServletRequest.
294      * Only intended for cooperating classes in this package.
295      */

296     protected final HttpServletRequest JavaDoc getRequest() {
297         return this.request;
298     }
299
300     /**
301      * Return the current WebApplicationContext.
302      */

303     public final WebApplicationContext getWebApplicationContext() {
304         return this.webApplicationContext;
305     }
306
307     /**
308      * Return the underlying ServletContext.
309      * Only intended for cooperating classes in this package.
310      */

311     protected final ServletContext JavaDoc getServletContext() {
312         return this.webApplicationContext.getServletContext();
313     }
314
315     /**
316      * Return the current locale.
317      */

318     public final Locale JavaDoc getLocale() {
319         return this.locale;
320     }
321
322     /**
323      * Return the current theme.
324      */

325     public final Theme getTheme() {
326         return this.theme;
327     }
328
329
330     /**
331      * (De)activate default HTML escaping for messages and errors, for the scope
332      * of this RequestContext. The default is the application-wide setting
333      * (the "defaultHtmlEscape" context-param in web.xml).
334      * @see org.springframework.web.util.WebUtils#isDefaultHtmlEscape
335      */

336     public void setDefaultHtmlEscape(boolean defaultHtmlEscape) {
337         this.defaultHtmlEscape = defaultHtmlEscape;
338     }
339
340     /**
341      * Is default HTML escaping active?
342      */

343     public boolean isDefaultHtmlEscape() {
344         return this.defaultHtmlEscape;
345     }
346
347     /**
348      * Set the UrlPathHelper to use for context path and request URI decoding.
349      * Can be used to pass a shared UrlPathHelper instance in.
350      * <p>A default UrlPathHelper is always available.
351      */

352     public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
353         this.urlPathHelper = (urlPathHelper != null ? urlPathHelper : new UrlPathHelper());
354     }
355
356     /**
357      * Return the UrlPathHelper used for context path and request URI decoding.
358      * Can be used to configure the current UrlPathHelper.
359      * <p>A default UrlPathHelper is always available.
360      */

361     public UrlPathHelper getUrlPathHelper() {
362         return this.urlPathHelper;
363     }
364
365
366     /**
367      * Return the context path of the original request,
368      * that is, the path that indicates the current web application.
369      * This is useful for building links to other resources within the application.
370      * <p>Delegates to the UrlPathHelper for decoding.
371      * @see javax.servlet.http.HttpServletRequest#getContextPath
372      * @see #getUrlPathHelper
373      */

374     public String JavaDoc getContextPath() {
375         return this.urlPathHelper.getOriginatingContextPath(this.request);
376     }
377
378     /**
379      * Return the request URI of the original request, that is, the invoked URL
380      * without parameters. This is particularly useful as HTML form action target,
381      * possibly in combination with the original query string.
382      * <p><b>Note this implementation will correctly resolve to the URI of any
383      * originating root request in the presence of a forwarded request. However, this
384      * can only work when the Servlet 2.4 'forward' request attributes are present.
385      * For use in a Servlet 2.3- environment, you can rely on
386      * {@link org.springframework.web.servlet.view.InternalResourceView}
387      * to add these prior to dispatching the request.</b>
388      * <p>Delegates to the UrlPathHelper for decoding.
389      * @see #getQueryString
390      * @see org.springframework.web.util.UrlPathHelper#getOriginatingRequestUri
391      * @see #getUrlPathHelper
392      */

393     public String JavaDoc getRequestUri() {
394         return this.urlPathHelper.getOriginatingRequestUri(this.request);
395     }
396
397     /**
398      * Return the query string of the current request, that is, the part after
399      * the request path. This is particularly useful for building an HTML form
400      * action target in combination with the original request URI.
401      * <p><b>Note this implementation will correctly resolve to the query string of any
402      * originating root request in the presence of a forwarded request. However, this
403      * can only work when the Servlet 2.4 'forward' request attributes are present.
404      * For use in a Servlet 2.3- environment, you can rely on
405      * {@link org.springframework.web.servlet.view.InternalResourceView}
406      * to add these prior to dispatching the request.</b>
407      * <p>Delegates to the UrlPathHelper for decoding.
408      * @see #getRequestUri
409      * @see org.springframework.web.util.UrlPathHelper#getOriginatingQueryString
410      * @see #getUrlPathHelper
411      */

412     public String JavaDoc getQueryString() {
413         return this.urlPathHelper.getOriginatingQueryString(this.request);
414     }
415
416
417     /**
418      * Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
419      * @param code code of the message
420      * @param defaultMessage String to return if the lookup fails
421      * @return the message
422      */

423     public String JavaDoc getMessage(String JavaDoc code, String JavaDoc defaultMessage) {
424         return getMessage(code, null, defaultMessage, this.defaultHtmlEscape);
425     }
426
427     /**
428      * Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
429      * @param code code of the message
430      * @param args arguments for the message, or <code>null</code> if none
431      * @param defaultMessage String to return if the lookup fails
432      * @return the message
433      */

434     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage) {
435         return getMessage(code, args, defaultMessage, this.defaultHtmlEscape);
436     }
437
438     /**
439      * Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
440      * @param code code of the message
441      * @param args arguments for the message as a List, or <code>null</code> if none
442      * @param defaultMessage String to return if the lookup fails
443      * @return the message
444      */

445     public String JavaDoc getMessage(String JavaDoc code, List JavaDoc args, String JavaDoc defaultMessage) {
446         return getMessage(code, (args != null ? args.toArray() : null), defaultMessage, this.defaultHtmlEscape);
447     }
448
449     /**
450      * Retrieve the message for the given code.
451      * @param code code of the message
452      * @param args arguments for the message, or <code>null</code> if none
453      * @param defaultMessage String to return if the lookup fails
454      * @param htmlEscape HTML escape the message?
455      * @return the message
456      */

457     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage, boolean htmlEscape) {
458         String JavaDoc msg = this.webApplicationContext.getMessage(code, args, defaultMessage, this.locale);
459         return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg);
460     }
461
462     /**
463      * Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
464      * @param code code of the message
465      * @return the message
466      * @throws org.springframework.context.NoSuchMessageException if not found
467      */

468     public String JavaDoc getMessage(String JavaDoc code) throws NoSuchMessageException {
469         return getMessage(code, null, this.defaultHtmlEscape);
470     }
471
472     /**
473      * Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
474      * @param code code of the message
475      * @param args arguments for the message, or <code>null</code> if none
476      * @return the message
477      * @throws org.springframework.context.NoSuchMessageException if not found
478      */

479     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args) throws NoSuchMessageException {
480         return getMessage(code, args, this.defaultHtmlEscape);
481     }
482
483     /**
484      * Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
485      * @param code code of the message
486      * @param args arguments for the message as a List, or <code>null</code> if none
487      * @return the message
488      * @throws org.springframework.context.NoSuchMessageException if not found
489      */

490     public String JavaDoc getMessage(String JavaDoc code, List JavaDoc args) throws NoSuchMessageException {
491         return getMessage(code, (args != null ? args.toArray() : null), this.defaultHtmlEscape);
492     }
493
494     /**
495      * Retrieve the message for the given code.
496      * @param code code of the message
497      * @param args arguments for the message, or <code>null</code> if none
498      * @param htmlEscape HTML escape the message?
499      * @return the message
500      * @throws org.springframework.context.NoSuchMessageException if not found
501      */

502     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, boolean htmlEscape) throws NoSuchMessageException {
503         String JavaDoc msg = this.webApplicationContext.getMessage(code, args, this.locale);
504         return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg);
505     }
506
507     /**
508      * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance),
509      * using the "defaultHtmlEscape" setting.
510      * @param resolvable the MessageSourceResolvable
511      * @return the message
512      * @throws org.springframework.context.NoSuchMessageException if not found
513      */

514     public String JavaDoc getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
515         return getMessage(resolvable, this.defaultHtmlEscape);
516     }
517
518     /**
519      * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).
520      * @param resolvable the MessageSourceResolvable
521      * @param htmlEscape HTML escape the message?
522      * @return the message
523      * @throws org.springframework.context.NoSuchMessageException if not found
524      */

525     public String JavaDoc getMessage(MessageSourceResolvable resolvable, boolean htmlEscape) throws NoSuchMessageException {
526         String JavaDoc msg = this.webApplicationContext.getMessage(resolvable, this.locale);
527         return (htmlEscape ? HtmlUtils.htmlEscape(msg) : msg);
528     }
529
530
531     /**
532      * Retrieve the theme message for the given code.
533      * <p>Note that theme messages are never HTML-escaped, as they typically
534      * denote theme-specific resource paths and not client-visible messages.
535      * @param code code of the message
536      * @param defaultMessage String to return if the lookup fails
537      * @return the message
538      */

539     public String JavaDoc getThemeMessage(String JavaDoc code, String JavaDoc defaultMessage) {
540         return this.theme.getMessageSource().getMessage(code, null, defaultMessage, this.locale);
541     }
542
543     /**
544      * Retrieve the theme message for the given code.
545      * <p>Note that theme messages are never HTML-escaped, as they typically
546      * denote theme-specific resource paths and not client-visible messages.
547      * @param code code of the message
548      * @param args arguments for the message, or <code>null</code> if none
549      * @param defaultMessage String to return if the lookup fails
550      * @return the message
551      */

552     public String JavaDoc getThemeMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage) {
553         return this.theme.getMessageSource().getMessage(code, args, defaultMessage, this.locale);
554     }
555
556     /**
557      * Retrieve the theme message for the given code.
558      * <p>Note that theme messages are never HTML-escaped, as they typically
559      * denote theme-specific resource paths and not client-visible messages.
560      * @param code code of the message
561      * @param args arguments for the message as a List, or <code>null</code> if none
562      * @param defaultMessage String to return if the lookup fails
563      * @return the message
564      */

565     public String JavaDoc getThemeMessage(String JavaDoc code, List JavaDoc args, String JavaDoc defaultMessage) {
566         return this.theme.getMessageSource().getMessage(
567                 code, (args != null ? args.toArray() : null), defaultMessage, this.locale);
568     }
569
570     /**
571      * Retrieve the theme message for the given code.
572      * <p>Note that theme messages are never HTML-escaped, as they typically
573      * denote theme-specific resource paths and not client-visible messages.
574      * @param code code of the message
575      * @return the message
576      * @throws org.springframework.context.NoSuchMessageException if not found
577      */

578     public String JavaDoc getThemeMessage(String JavaDoc code) throws NoSuchMessageException {
579         return this.theme.getMessageSource().getMessage(code, null, this.locale);
580     }
581
582     /**
583      * Retrieve the theme message for the given code.
584      * <p>Note that theme messages are never HTML-escaped, as they typically
585      * denote theme-specific resource paths and not client-visible messages.
586      * @param code code of the message
587      * @param args arguments for the message, or <code>null</code> if none
588      * @return the message
589      * @throws org.springframework.context.NoSuchMessageException if not found
590      */

591     public String JavaDoc getThemeMessage(String JavaDoc code, Object JavaDoc[] args) throws NoSuchMessageException {
592         return this.theme.getMessageSource().getMessage(code, args, this.locale);
593     }
594
595     /**
596      * Retrieve the theme message for the given code.
597      * <p>Note that theme messages are never HTML-escaped, as they typically
598      * denote theme-specific resource paths and not client-visible messages.
599      * @param code code of the message
600      * @param args arguments for the message as a List, or <code>null</code> if none
601      * @return the message
602      * @throws org.springframework.context.NoSuchMessageException if not found
603      */

604     public String JavaDoc getThemeMessage(String JavaDoc code, List JavaDoc args) throws NoSuchMessageException {
605         return this.theme.getMessageSource().getMessage(
606                 code, (args != null ? args.toArray() : null), this.locale);
607     }
608
609     /**
610      * Retrieve the given MessageSourceResolvable in the current theme.
611      * <p>Note that theme messages are never HTML-escaped, as they typically
612      * denote theme-specific resource paths and not client-visible messages.
613      * @param resolvable the MessageSourceResolvable
614      * @return the message
615      * @throws org.springframework.context.NoSuchMessageException if not found
616      */

617     public String JavaDoc getThemeMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
618         return this.theme.getMessageSource().getMessage(resolvable, this.locale);
619     }
620
621
622     /**
623      * Retrieve the Errors instance for the given bind object,
624      * using the "defaultHtmlEscape" setting.
625      * @param name name of the bind object
626      * @return the Errors instance, or <code>null</code> if not found
627      */

628     public Errors getErrors(String JavaDoc name) {
629         return getErrors(name, this.defaultHtmlEscape);
630     }
631
632     /**
633      * Retrieve the Errors instance for the given bind object.
634      * @param name name of the bind object
635      * @param htmlEscape create an Errors instance with automatic HTML escaping?
636      * @return the Errors instance, or <code>null</code> if not found
637      */

638     public Errors getErrors(String JavaDoc name, boolean htmlEscape) {
639         if (this.errorsMap == null) {
640             this.errorsMap = new HashMap JavaDoc();
641         }
642         Errors errors = (Errors) this.errorsMap.get(name);
643         boolean put = false;
644         if (errors == null) {
645             errors = (Errors) getModelObject(BindingResult.MODEL_KEY_PREFIX + name);
646             // Check old BindException prefix for backwards compatibility.
647
if (errors == null) {
648                 errors = (Errors) getModelObject(BindException.ERROR_KEY_PREFIX + name);
649             }
650             if (errors == null) {
651                 return null;
652             }
653             put = true;
654         }
655         if (htmlEscape && !(errors instanceof EscapedErrors)) {
656             errors = new EscapedErrors(errors);
657             put = true;
658         }
659         else if (!htmlEscape && errors instanceof EscapedErrors) {
660             errors = ((EscapedErrors) errors).getSource();
661             put = true;
662         }
663         if (put) {
664             this.errorsMap.put(name, errors);
665         }
666         return errors;
667     }
668
669     /**
670      * Retrieve the model object for the given model name,
671      * either from the model or from the request attributes.
672      * @param modelName the name of the model object
673      * @return the model object
674      */

675     protected Object JavaDoc getModelObject(String JavaDoc modelName) {
676         if (this.model != null) {
677             return this.model.get(modelName);
678         }
679         else {
680             return this.request.getAttribute(modelName);
681         }
682     }
683
684     /**
685      * Create a BindStatus for the given bind object,
686      * using the "defaultHtmlEscape" setting.
687      * @param path the bean and property path for which values and errors
688      * will be resolved (e.g. "person.age")
689      * @return the new BindStatus instance
690      * @throws IllegalStateException if no corresponding Errors object found
691      */

692     public BindStatus getBindStatus(String JavaDoc path) throws IllegalStateException JavaDoc {
693         return new BindStatus(this, path, this.defaultHtmlEscape);
694     }
695
696     /**
697      * Create a BindStatus for the given bind object,
698      * using the "defaultHtmlEscape" setting.
699      * @param path the bean and property path for which values and errors
700      * will be resolved (e.g. "person.age")
701      * @param htmlEscape create a BindStatus with automatic HTML escaping?
702      * @return the new BindStatus instance
703      * @throws IllegalStateException if no corresponding Errors object found
704      */

705     public BindStatus getBindStatus(String JavaDoc path, boolean htmlEscape) throws IllegalStateException JavaDoc {
706         return new BindStatus(this, path, htmlEscape);
707     }
708
709 }
710
Popular Tags