Page-level JSTL time zone support for JSP tags

Issue: SPR-15746
This commit is contained in:
Juergen Hoeller 2017-07-12 22:59:42 +02:00
parent 50f8b6b3ab
commit e138d7d29f
2 changed files with 80 additions and 59 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.springframework.web.servlet.support;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
@ -85,9 +86,9 @@ public class JspAwareRequestContext extends RequestContext {
}
/**
* This implementation checks for a JSTL locale attribute
* in page, request, session or application scope; if not found,
* returns the {@code HttpServletRequest.getLocale()}.
* This implementation checks for a JSTL locale attribute in page,
* request, session or application scope; if not found, returns the
* {@code HttpServletRequest.getLocale()}.
*/
@Override
protected Locale getFallbackLocale() {
@ -100,6 +101,21 @@ public class JspAwareRequestContext extends RequestContext {
return getRequest().getLocale();
}
/**
* This implementation checks for a JSTL time zone attribute in page,
* request, session or application scope; if not found, returns {@code null}.
*/
@Override
protected TimeZone getFallbackTimeZone() {
if (jstlPresent) {
TimeZone timeZone = JstlPageLocaleResolver.getJstlTimeZone(getPageContext());
if (timeZone != null) {
return timeZone;
}
}
return null;
}
/**
* Inner class that isolates the JSTL dependency.
@ -111,6 +127,11 @@ public class JspAwareRequestContext extends RequestContext {
Object localeObject = Config.find(pageContext, Config.FMT_LOCALE);
return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
public static TimeZone getJstlTimeZone(PageContext pageContext) {
Object timeZoneObject = Config.find(pageContext, Config.FMT_TIME_ZONE);
return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -72,7 +72,6 @@ import org.springframework.web.util.WebUtils;
* @see org.springframework.web.servlet.DispatcherServlet
* @see org.springframework.web.servlet.view.AbstractView#setRequestContextAttribute
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setRequestContextAttribute
* @see #getFallbackLocale()
*/
public class RequestContext {
@ -91,8 +90,8 @@ public class RequestContext {
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = RequestContext.class.getName() + ".CONTEXT";
protected static final boolean jstlPresent = ClassUtils.isPresent("javax.servlet.jsp.jstl.core.Config",
RequestContext.class.getClassLoader());
protected static final boolean jstlPresent = ClassUtils.isPresent(
"javax.servlet.jsp.jstl.core.Config", RequestContext.class.getClassLoader());
private HttpServletRequest request;
@ -276,56 +275,6 @@ public class RequestContext {
}
}
/**
* Determine the fallback locale for this context.
* <p>The default implementation checks for a JSTL locale attribute in request, session
* or application scope; if not found, returns the {@code HttpServletRequest.getLocale()}.
* @return the fallback locale (never {@code null})
* @see javax.servlet.http.HttpServletRequest#getLocale()
*/
protected Locale getFallbackLocale() {
if (jstlPresent) {
Locale locale = JstlLocaleResolver.getJstlLocale(getRequest(), getServletContext());
if (locale != null) {
return locale;
}
}
return getRequest().getLocale();
}
/**
* Determine the fallback time zone for this context.
* <p>The default implementation checks for a JSTL time zone attribute in request,
* session or application scope; returns {@code null} if not found.
* @return the fallback time zone (or {@code null} if none derivable from the request)
*/
protected TimeZone getFallbackTimeZone() {
if (jstlPresent) {
TimeZone timeZone = JstlLocaleResolver.getJstlTimeZone(getRequest(), getServletContext());
if (timeZone != null) {
return timeZone;
}
}
return null;
}
/**
* Determine the fallback theme for this context.
* <p>The default implementation returns the default theme (with name "theme").
* @return the fallback theme (never {@code null})
*/
protected Theme getFallbackTheme() {
ThemeSource themeSource = RequestContextUtils.getThemeSource(getRequest());
if (themeSource == null) {
themeSource = new ResourceBundleThemeSource();
}
Theme theme = themeSource.getTheme(DEFAULT_THEME_NAME);
if (theme == null) {
throw new IllegalStateException("No theme defined and no fallback theme found");
}
return theme;
}
/**
* Return the underlying HttpServletRequest. Only intended for cooperating classes in this package.
@ -383,6 +332,39 @@ public class RequestContext {
return this.timeZone;
}
/**
* Determine the fallback locale for this context.
* <p>The default implementation checks for a JSTL locale attribute in request, session
* or application scope; if not found, returns the {@code HttpServletRequest.getLocale()}.
* @return the fallback locale (never {@code null})
* @see javax.servlet.http.HttpServletRequest#getLocale()
*/
protected Locale getFallbackLocale() {
if (jstlPresent) {
Locale locale = JstlLocaleResolver.getJstlLocale(getRequest(), getServletContext());
if (locale != null) {
return locale;
}
}
return getRequest().getLocale();
}
/**
* Determine the fallback time zone for this context.
* <p>The default implementation checks for a JSTL time zone attribute in request,
* session or application scope; returns {@code null} if not found.
* @return the fallback time zone (or {@code null} if none derivable from the request)
*/
protected TimeZone getFallbackTimeZone() {
if (jstlPresent) {
TimeZone timeZone = JstlLocaleResolver.getJstlTimeZone(getRequest(), getServletContext());
if (timeZone != null) {
return timeZone;
}
}
return null;
}
/**
* Change the current locale to the specified one,
* storing the new locale through the configured {@link LocaleResolver}.
@ -434,6 +416,23 @@ public class RequestContext {
return this.theme;
}
/**
* Determine the fallback theme for this context.
* <p>The default implementation returns the default theme (with name "theme").
* @return the fallback theme (never {@code null})
*/
protected Theme getFallbackTheme() {
ThemeSource themeSource = RequestContextUtils.getThemeSource(getRequest());
if (themeSource == null) {
themeSource = new ResourceBundleThemeSource();
}
Theme theme = themeSource.getTheme(DEFAULT_THEME_NAME);
if (theme == null) {
throw new IllegalStateException("No theme defined and no fallback theme found");
}
return theme;
}
/**
* Change the current theme to the specified one,
* storing the new theme name through the configured {@link ThemeResolver}.
@ -870,7 +869,8 @@ public class RequestContext {
}
/**
* Retrieve the model object for the given model name, either from the model or from the request attributes.
* Retrieve the model object for the given model name, either from the model
* or from the request attributes.
* @param modelName the name of the model object
* @return the model object
*/