/** * 从请求中,解析出使用的主题。例如,从请求头 User-Agent ,判断使用 PC 端,还是移动端的主题 * * Resolve the current theme name via the given request. * Should return a default theme as fallback in any case. * @param request request to be used for resolution * @return the current theme name */ String resolveThemeName(HttpServletRequest request);
/** * 设置请求,所使用的主题。 * * Set the current theme name to the given one. * @param request request to be used for theme name modification * @param response response to be used for theme name modification * @param themeName the new theme name ({@code null} or empty to reset it) * @throws UnsupportedOperationException if the ThemeResolver implementation * does not support dynamic changing of the theme */ void setThemeName(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable String themeName);
}
AbstractThemeResolver
抽象类 AbstractThemeResolver ,提供两个方法:
setDefaultThemeName 设置默认的样式名
getDefaultThemeName 获取默认的样式名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public abstract class AbstractThemeResolver implements ThemeResolver {
/** * Out-of-the-box value for the default theme name: "theme". */ public static final String ORIGINAL_DEFAULT_THEME_NAME = "theme";
public class SessionThemeResolver extends AbstractThemeResolver {
/** * Name of the session attribute that holds the theme name. * Only used internally by this implementation. * Use {@code RequestContext(Utils).getTheme()} * to retrieve the current theme in controllers or views. * @see org.springframework.web.servlet.support.RequestContext#getTheme * @see org.springframework.web.servlet.support.RequestContextUtils#getTheme */ public static final String THEME_SESSION_ATTRIBUTE_NAME = SessionThemeResolver.class.getName() + ".THEME";
@Override public String resolveThemeName(HttpServletRequest request) { 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()); }
public class CookieThemeResolver extends CookieGenerator implements ThemeResolver {
/** * The default theme name used if no alternative is provided. */ public static final String ORIGINAL_DEFAULT_THEME_NAME = "theme";
/** * Name of the request attribute that holds the theme name. Only used * for overriding a cookie value if the theme has been changed in the * course of the current request! Use RequestContext.getTheme() to * retrieve the current theme in controllers or views. * @see org.springframework.web.servlet.support.RequestContext#getTheme */ public static final String THEME_REQUEST_ATTRIBUTE_NAME = CookieThemeResolver.class.getName() + ".THEME";
/** * The default name of the cookie that holds the theme name. */ public static final String DEFAULT_COOKIE_NAME = CookieThemeResolver.class.getName() + ".THEME";
public CookieThemeResolver() { setCookieName(DEFAULT_COOKIE_NAME); }
/** * Set the name of the default theme. */ public void setDefaultThemeName(String defaultThemeName) { this.defaultThemeName = defaultThemeName; }
/** * Return the name of the default theme. */ public String getDefaultThemeName() { return this.defaultThemeName; }
@Override public String resolveThemeName(HttpServletRequest request) { // Check request for preparsed or preset theme. String themeName = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME); if (themeName != null) { return themeName; }
// Retrieve cookie value from request. String cookieName = getCookieName(); if (cookieName != null) { Cookie cookie = WebUtils.getCookie(request, cookieName); if (cookie != null) { String value = cookie.getValue(); if (StringUtils.hasText(value)) { themeName = value; } } }
// Fall back to default theme. if (themeName == null) { themeName = getDefaultThemeName(); } request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName); return themeName; }