KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 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.Locale JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.jsp.jstl.core.Config;
25 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
26
27 import org.springframework.context.MessageSource;
28 import org.springframework.context.support.MessageSourceResourceBundle;
29 import org.springframework.context.support.ResourceBundleMessageSource;
30
31 /**
32  * Helper class for preparing JSTL views,
33  * in particular for exposing a JSTL localization context.
34  *
35  * @author Juergen Hoeller
36  * @since 20.08.2003
37  */

38 public abstract class JstlUtils {
39
40     public static final String JavaDoc REQUEST_SCOPE_SUFFIX = ".request";
41
42     /**
43      * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
44      * context-param and creates a corresponding child message source,
45      * with the provided Spring-defined MessageSource as parent.
46      * @param servletContext the ServletContext we're running in
47      * (to check JSTL-related context-params in web.xml)
48      * @param messageSource the MessageSource to expose, typically
49      * the ApplicationContext of the current DispatcherServlet
50      * @return the MessageSource to expose to JSTL; first checking the
51      * JSTL-defined bundle, then the Spring-defined MessageSource
52      * @see org.springframework.context.ApplicationContext
53      */

54     public static MessageSource getJstlAwareMessageSource(
55             ServletContext JavaDoc servletContext, MessageSource messageSource) {
56
57         String JavaDoc jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
58         if (jstlInitParam != null) {
59             // Create a ResourceBundleMessageSource for the specified resource bundle
60
// basename in the JSTL context-param in web.xml, wiring it with the given
61
// Spring-defined MessageSource as parent.
62
ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
63             jstlBundleWrapper.setBasename(jstlInitParam);
64             jstlBundleWrapper.setParentMessageSource(messageSource);
65             return jstlBundleWrapper;
66         }
67         return messageSource;
68     }
69
70     /**
71      * Exposes JSTL-specific request attributes specifying locale
72      * and resource bundle for JSTL's formatting and message tags,
73      * using Spring's locale and message source.
74      * @param request current HTTP request
75      * @param messageSource the MessageSource to expose,
76      * typically the current application context
77      */

78     public static void exposeLocalizationContext(HttpServletRequest JavaDoc request, MessageSource messageSource) {
79
80         // Add JSTL locale and LocalizationContext request attributes.
81
Locale JavaDoc jstlLocale = RequestContextUtils.getLocale(request);
82         ResourceBundle JavaDoc bundle = new MessageSourceResourceBundle(messageSource, jstlLocale);
83         LocalizationContext jstlContext = new LocalizationContext(bundle, jstlLocale);
84
85         // for JSTL implementations that stick to the config names (e.g. Resin's)
86
request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
87         request.setAttribute(Config.FMT_LOCALE, jstlLocale);
88
89         // for JSTL implementations that append the scope to the config names (e.g. Jakarta's)
90
request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + REQUEST_SCOPE_SUFFIX, jstlContext);
91         request.setAttribute(Config.FMT_LOCALE + REQUEST_SCOPE_SUFFIX, jstlLocale);
92     }
93
94 }
95
Popular Tags