Skip to content

Commit e138d7d

Browse files
committed
Page-level JSTL time zone support for JSP tags
Issue: SPR-15746
1 parent 50f8b6b commit e138d7d

File tree

2 files changed

+80
-59
lines changed

2 files changed

+80
-59
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.Locale;
2020
import java.util.Map;
21+
import java.util.TimeZone;
2122
import javax.servlet.http.HttpServletRequest;
2223
import javax.servlet.http.HttpServletResponse;
2324
import javax.servlet.jsp.PageContext;
@@ -85,9 +86,9 @@ protected final PageContext getPageContext() {
8586
}
8687

8788
/**
88-
* This implementation checks for a JSTL locale attribute
89-
* in page, request, session or application scope; if not found,
90-
* returns the {@code HttpServletRequest.getLocale()}.
89+
* This implementation checks for a JSTL locale attribute in page,
90+
* request, session or application scope; if not found, returns the
91+
* {@code HttpServletRequest.getLocale()}.
9192
*/
9293
@Override
9394
protected Locale getFallbackLocale() {
@@ -100,6 +101,21 @@ protected Locale getFallbackLocale() {
100101
return getRequest().getLocale();
101102
}
102103

104+
/**
105+
* This implementation checks for a JSTL time zone attribute in page,
106+
* request, session or application scope; if not found, returns {@code null}.
107+
*/
108+
@Override
109+
protected TimeZone getFallbackTimeZone() {
110+
if (jstlPresent) {
111+
TimeZone timeZone = JstlPageLocaleResolver.getJstlTimeZone(getPageContext());
112+
if (timeZone != null) {
113+
return timeZone;
114+
}
115+
}
116+
return null;
117+
}
118+
103119

104120
/**
105121
* Inner class that isolates the JSTL dependency.
@@ -111,6 +127,11 @@ public static Locale getJstlLocale(PageContext pageContext) {
111127
Object localeObject = Config.find(pageContext, Config.FMT_LOCALE);
112128
return (localeObject instanceof Locale ? (Locale) localeObject : null);
113129
}
130+
131+
public static TimeZone getJstlTimeZone(PageContext pageContext) {
132+
Object timeZoneObject = Config.find(pageContext, Config.FMT_TIME_ZONE);
133+
return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
134+
}
114135
}
115136

116137
}

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

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,7 +72,6 @@
7272
* @see org.springframework.web.servlet.DispatcherServlet
7373
* @see org.springframework.web.servlet.view.AbstractView#setRequestContextAttribute
7474
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setRequestContextAttribute
75-
* @see #getFallbackLocale()
7675
*/
7776
public class RequestContext {
7877

@@ -91,8 +90,8 @@ public class RequestContext {
9190
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = RequestContext.class.getName() + ".CONTEXT";
9291

9392

94-
protected static final boolean jstlPresent = ClassUtils.isPresent("javax.servlet.jsp.jstl.core.Config",
95-
RequestContext.class.getClassLoader());
93+
protected static final boolean jstlPresent = ClassUtils.isPresent(
94+
"javax.servlet.jsp.jstl.core.Config", RequestContext.class.getClassLoader());
9695

9796
private HttpServletRequest request;
9897

@@ -276,56 +275,6 @@ else if (localeResolver != null) {
276275
}
277276
}
278277

279-
/**
280-
* Determine the fallback locale for this context.
281-
* <p>The default implementation checks for a JSTL locale attribute in request, session
282-
* or application scope; if not found, returns the {@code HttpServletRequest.getLocale()}.
283-
* @return the fallback locale (never {@code null})
284-
* @see javax.servlet.http.HttpServletRequest#getLocale()
285-
*/
286-
protected Locale getFallbackLocale() {
287-
if (jstlPresent) {
288-
Locale locale = JstlLocaleResolver.getJstlLocale(getRequest(), getServletContext());
289-
if (locale != null) {
290-
return locale;
291-
}
292-
}
293-
return getRequest().getLocale();
294-
}
295-
296-
/**
297-
* Determine the fallback time zone for this context.
298-
* <p>The default implementation checks for a JSTL time zone attribute in request,
299-
* session or application scope; returns {@code null} if not found.
300-
* @return the fallback time zone (or {@code null} if none derivable from the request)
301-
*/
302-
protected TimeZone getFallbackTimeZone() {
303-
if (jstlPresent) {
304-
TimeZone timeZone = JstlLocaleResolver.getJstlTimeZone(getRequest(), getServletContext());
305-
if (timeZone != null) {
306-
return timeZone;
307-
}
308-
}
309-
return null;
310-
}
311-
312-
/**
313-
* Determine the fallback theme for this context.
314-
* <p>The default implementation returns the default theme (with name "theme").
315-
* @return the fallback theme (never {@code null})
316-
*/
317-
protected Theme getFallbackTheme() {
318-
ThemeSource themeSource = RequestContextUtils.getThemeSource(getRequest());
319-
if (themeSource == null) {
320-
themeSource = new ResourceBundleThemeSource();
321-
}
322-
Theme theme = themeSource.getTheme(DEFAULT_THEME_NAME);
323-
if (theme == null) {
324-
throw new IllegalStateException("No theme defined and no fallback theme found");
325-
}
326-
return theme;
327-
}
328-
329278

330279
/**
331280
* Return the underlying HttpServletRequest. Only intended for cooperating classes in this package.
@@ -383,6 +332,39 @@ public TimeZone getTimeZone() {
383332
return this.timeZone;
384333
}
385334

335+
/**
336+
* Determine the fallback locale for this context.
337+
* <p>The default implementation checks for a JSTL locale attribute in request, session
338+
* or application scope; if not found, returns the {@code HttpServletRequest.getLocale()}.
339+
* @return the fallback locale (never {@code null})
340+
* @see javax.servlet.http.HttpServletRequest#getLocale()
341+
*/
342+
protected Locale getFallbackLocale() {
343+
if (jstlPresent) {
344+
Locale locale = JstlLocaleResolver.getJstlLocale(getRequest(), getServletContext());
345+
if (locale != null) {
346+
return locale;
347+
}
348+
}
349+
return getRequest().getLocale();
350+
}
351+
352+
/**
353+
* Determine the fallback time zone for this context.
354+
* <p>The default implementation checks for a JSTL time zone attribute in request,
355+
* session or application scope; returns {@code null} if not found.
356+
* @return the fallback time zone (or {@code null} if none derivable from the request)
357+
*/
358+
protected TimeZone getFallbackTimeZone() {
359+
if (jstlPresent) {
360+
TimeZone timeZone = JstlLocaleResolver.getJstlTimeZone(getRequest(), getServletContext());
361+
if (timeZone != null) {
362+
return timeZone;
363+
}
364+
}
365+
return null;
366+
}
367+
386368
/**
387369
* Change the current locale to the specified one,
388370
* storing the new locale through the configured {@link LocaleResolver}.
@@ -434,6 +416,23 @@ public Theme getTheme() {
434416
return this.theme;
435417
}
436418

419+
/**
420+
* Determine the fallback theme for this context.
421+
* <p>The default implementation returns the default theme (with name "theme").
422+
* @return the fallback theme (never {@code null})
423+
*/
424+
protected Theme getFallbackTheme() {
425+
ThemeSource themeSource = RequestContextUtils.getThemeSource(getRequest());
426+
if (themeSource == null) {
427+
themeSource = new ResourceBundleThemeSource();
428+
}
429+
Theme theme = themeSource.getTheme(DEFAULT_THEME_NAME);
430+
if (theme == null) {
431+
throw new IllegalStateException("No theme defined and no fallback theme found");
432+
}
433+
return theme;
434+
}
435+
437436
/**
438437
* Change the current theme to the specified one,
439438
* storing the new theme name through the configured {@link ThemeResolver}.
@@ -870,7 +869,8 @@ else if (!htmlEscape && errors instanceof EscapedErrors) {
870869
}
871870

872871
/**
873-
* Retrieve the model object for the given model name, either from the model or from the request attributes.
872+
* Retrieve the model object for the given model name, either from the model
873+
* or from the request attributes.
874874
* @param modelName the name of the model object
875875
* @return the model object
876876
*/

0 commit comments

Comments
 (0)