1 16 17 package org.springframework.web.servlet.support; 18 19 import java.util.Locale ; 20 import java.util.ResourceBundle ; 21 22 import javax.servlet.ServletContext ; 23 import javax.servlet.http.HttpServletRequest ; 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 38 public abstract class JstlUtils { 39 40 public static final String REQUEST_SCOPE_SUFFIX = ".request"; 41 42 54 public static MessageSource getJstlAwareMessageSource( 55 ServletContext servletContext, MessageSource messageSource) { 56 57 String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT); 58 if (jstlInitParam != null) { 59 ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource(); 63 jstlBundleWrapper.setBasename(jstlInitParam); 64 jstlBundleWrapper.setParentMessageSource(messageSource); 65 return jstlBundleWrapper; 66 } 67 return messageSource; 68 } 69 70 78 public static void exposeLocalizationContext(HttpServletRequest request, MessageSource messageSource) { 79 80 Locale jstlLocale = RequestContextUtils.getLocale(request); 82 ResourceBundle bundle = new MessageSourceResourceBundle(messageSource, jstlLocale); 83 LocalizationContext jstlContext = new LocalizationContext(bundle, jstlLocale); 84 85 request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT, jstlContext); 87 request.setAttribute(Config.FMT_LOCALE, jstlLocale); 88 89 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 |