diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/CookieThemeResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/CookieThemeResolver.java index 5d73b8e452..e8ccfcc983 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/CookieThemeResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/CookieThemeResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -20,12 +20,13 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.util.StringUtils; import org.springframework.web.servlet.ThemeResolver; import org.springframework.web.util.CookieGenerator; import org.springframework.web.util.WebUtils; /** - * Implementation of ThemeResolver that uses a cookie sent back to the user + * {@link ThemeResolver} implementation that uses a cookie sent back to the user * in case of a custom setting, with a fallback to the default theme. * This is particularly useful for stateless applications without user sessions. * @@ -79,29 +80,35 @@ public class CookieThemeResolver extends CookieGenerator implements ThemeResolve @Override public String resolveThemeName(HttpServletRequest request) { // Check request for preparsed or preset theme. - String theme = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME); - if (theme != null) { - return theme; + String themeName = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME); + if (themeName != null) { + return themeName; } // Retrieve cookie value from request. Cookie cookie = WebUtils.getCookie(request, getCookieName()); if (cookie != null) { - return cookie.getValue(); + String value = cookie.getValue(); + if (StringUtils.hasText(value)) { + themeName = value; + } } // Fall back to default theme. - return getDefaultThemeName(); + if (themeName == null) { + themeName = getDefaultThemeName(); + } + request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName); + return themeName; } @Override public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { - if (themeName != null) { + if (StringUtils.hasText(themeName)) { // Set request attribute and add cookie. request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName); addCookie(response, themeName); } - else { // Set request attribute to fallback theme and remove cookie. request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName()); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/SessionThemeResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/SessionThemeResolver.java index 7c9f0089f8..9b0a0f3778 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/SessionThemeResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/theme/SessionThemeResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -19,12 +19,14 @@ package org.springframework.web.servlet.theme; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.util.StringUtils; import org.springframework.web.util.WebUtils; /** - * Implementation of ThemeResolver that uses a theme attribute in the user's - * session in case of a custom setting, with a fallback to the default theme. - * This is most appropriate if the application needs user sessions anyway. + * {@link org.springframework.web.servlet.ThemeResolver} implementation that + * uses a theme attribute in the user's session in case of a custom setting, + * with a fallback to the default theme. This is most appropriate if the + * application needs user sessions anyway. * *

Custom controllers can override the user's theme by calling * {@code setThemeName}, e.g. responding to a theme change request. @@ -46,16 +48,18 @@ public class SessionThemeResolver extends AbstractThemeResolver { */ public static final String THEME_SESSION_ATTRIBUTE_NAME = SessionThemeResolver.class.getName() + ".THEME"; + @Override public String resolveThemeName(HttpServletRequest request) { - String theme = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME); - // specific theme, or fallback to default? - return (theme != null ? theme : getDefaultThemeName()); + String themeName = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME); + // A specific theme indicated, or do we need to fallback to the default? + return (themeName != null ? themeName : getDefaultThemeName()); } @Override public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { - WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME, themeName); + WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME, + (StringUtils.hasText(themeName) ? themeName : null)); } }