Browse Source

Page-level JSTL time zone support for JSP tags

Issue: SPR-15746
pull/1479/head
Juergen Hoeller 9 years ago
parent
commit
e138d7d29f
  1. 29
      spring-webmvc/src/main/java/org/springframework/web/servlet/support/JspAwareRequestContext.java
  2. 110
      spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java

29
spring-webmvc/src/main/java/org/springframework/web/servlet/support/JspAwareRequestContext.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Locale;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext; import javax.servlet.jsp.PageContext;
@ -85,9 +86,9 @@ public class JspAwareRequestContext extends RequestContext {
} }
/** /**
* This implementation checks for a JSTL locale attribute * This implementation checks for a JSTL locale attribute in page,
* in page, request, session or application scope; if not found, * request, session or application scope; if not found, returns the
* returns the {@code HttpServletRequest.getLocale()}. * {@code HttpServletRequest.getLocale()}.
*/ */
@Override @Override
protected Locale getFallbackLocale() { protected Locale getFallbackLocale() {
@ -100,6 +101,21 @@ public class JspAwareRequestContext extends RequestContext {
return getRequest().getLocale(); 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. * Inner class that isolates the JSTL dependency.
@ -111,6 +127,11 @@ public class JspAwareRequestContext extends RequestContext {
Object localeObject = Config.find(pageContext, Config.FMT_LOCALE); Object localeObject = Config.find(pageContext, Config.FMT_LOCALE);
return (localeObject instanceof Locale ? (Locale) localeObject : null); 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);
}
} }
} }

110
spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.DispatcherServlet
* @see org.springframework.web.servlet.view.AbstractView#setRequestContextAttribute * @see org.springframework.web.servlet.view.AbstractView#setRequestContextAttribute
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setRequestContextAttribute * @see org.springframework.web.servlet.view.UrlBasedViewResolver#setRequestContextAttribute
* @see #getFallbackLocale()
*/ */
public class RequestContext { public class RequestContext {
@ -91,8 +90,8 @@ public class RequestContext {
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = RequestContext.class.getName() + ".CONTEXT"; 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", protected static final boolean jstlPresent = ClassUtils.isPresent(
RequestContext.class.getClassLoader()); "javax.servlet.jsp.jstl.core.Config", RequestContext.class.getClassLoader());
private HttpServletRequest request; 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. * Return the underlying HttpServletRequest. Only intended for cooperating classes in this package.
@ -383,6 +332,39 @@ public class RequestContext {
return this.timeZone; 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, * Change the current locale to the specified one,
* storing the new locale through the configured {@link LocaleResolver}. * storing the new locale through the configured {@link LocaleResolver}.
@ -434,6 +416,23 @@ public class RequestContext {
return this.theme; 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, * Change the current theme to the specified one,
* storing the new theme name through the configured {@link ThemeResolver}. * 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 * @param modelName the name of the model object
* @return the model object * @return the model object
*/ */

Loading…
Cancel
Save