springMVC源码分析--动态样式ThemeResolver(二)
在上一篇博客springMVC源码分析--动态样式ThemeResolver(一)中我们介绍了多样式ThemeResolver的使用方法,接下来我们对源码进行简单的分析一下。
ThemeResolver的体系结构如下:
1、接口ThemeResolver中定义的接口是比较简单的,提供两个接口:
(1)resolveThemeName获取样式名
(2)setThemeName设置样式名
public interface ThemeResolver { /** * 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 * @throws UnsupportedOperationException if the ThemeResolver implementation * does not support dynamic changing of the theme */ void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName); }
2、抽象类AbstractThemeResolver,提供两个方法:
(1)setDefaultThemeName 设置默认的样式名
(2)getDefaultThemeName 获取默认的样式名
public abstract class AbstractThemeResolver implements ThemeResolver { public final static String ORIGINAL_DEFAULT_THEME_NAME = "theme"; private String defaultThemeName = ORIGINAL_DEFAULT_THEME_NAME; public void setDefaultThemeName(String defaultThemeName) { this.defaultThemeName = defaultThemeName; } public String getDefaultThemeName() { return this.defaultThemeName; } }
3、实现类CookieThemeResolver,其实现原理其实是比较简单的,就是在Cookie中设置样式名
(1)在setThemeName函数中中将theme设置到cookie中
public class CookieThemeResolver extends CookieGenerator implements ThemeResolver { public final static 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"; public static final String DEFAULT_COOKIE_NAME = CookieThemeResolver.class.getName() + ".THEME"; private String defaultThemeName = ORIGINAL_DEFAULT_THEME_NAME; 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 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. Cookie cookie = WebUtils.getCookie(request, getCookieName()); 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; } @Override public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { 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()); removeCookie(response); } } }
3、实现类SessionThemeResolver的实现也是比较简单的,就是将themeName保存到session中就可以了。
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()); } @Override public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME, (StringUtils.hasText(themeName) ? themeName : null)); } }
4、FixedThemeResolver中没有具体的实现操作
public class FixedThemeResolver extends AbstractThemeResolver { @Override public String resolveThemeName(HttpServletRequest request) { return getDefaultThemeName(); } @Override public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) { throw new UnsupportedOperationException("Cannot change theme - use a different theme resolution strategy"); } }
springMVC源码分析--动态样式ThemeResolver(二)的更多相关文章
- springMVC源码分析--动态样式ThemeResolver(一)
Spring MVC中通过ThemeSource接口来提供对动态更换样式的支持,并提供了ResourceBundleThemeSource这个具体实现类来提供通过properties配置文件对them ...
- springMVC源码分析--容器初始化(二)DispatcherServlet
在上一篇博客springMVC源码分析--容器初始化(一)中我们介绍了spring web初始化IOC容器的过程,springMVC作为spring项目中的子项目,其可以和spring web容器很好 ...
- springMVC源码分析--DispatcherServlet请求获取及处理
在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...
- SpringMVC源码分析--容器初始化(三)HttpServletBean
在上一篇博客springMVC源码分析--容器初始化(二)DispatcherServlet中,我们队SpringMVC整体生命周期有一个简单的说明,并没有进行详细的源码分析,接下来我们会根据博客中提 ...
- springMVC源码分析--国际化实现Session和Cookie(二)
上一篇博客springMVC源码分析--国际化LocaleResolver(一)中我们介绍了springMVC提供的国际化的解决方案,接下来我们根据springMVC提供的解决方案来简单的实现一个多语 ...
- 框架-springmvc源码分析(二)
框架-springmvc源码分析(二) 参考: http://www.cnblogs.com/leftthen/p/5207787.html http://www.cnblogs.com/leftth ...
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...
- springMVC源码分析--SimpleServletHandlerAdapter(二)
上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...
- springMVC源码分析--AbstractHandlerMapping(二)
上一篇博客springMVC源码分析--HandlerMapping(一)中我们简单的介绍了HandlerMapping,接下来我们介绍一下它的抽象实现类AbstractHandlerMapping
随机推荐
- TCP三次握手详解及释放连接过程
TCP在传输之前会进行三次沟通,一般称为"三次握手",传完数据断开的时候要进行四次沟通,一般称为"四次挥手". 两个序号和三个标志位: (1)序号:seq序号, ...
- Javasript设计模式之链式调用
写过jquery的可能都知道,jquery里面可以很方便的使用以下代码: // 不使用链式调用 const element = $(ele); element.addClass('red'); ele ...
- [ Java学习基础 ] 浅析Java方法调用
先看一个示例,读者可先自行阅读判断输出结果: public class TestClass { private static void testMethod(){ System.out.printl ...
- bzoj 5251: [2018多省省队联测]劈配
Description 一年一度的综艺节目<中国新代码>又开始了. Zayid从小就梦想成为一名程序员,他觉得这是一个展示自己的舞台,于是他毫不犹豫地报名了. 题目描述 轻车熟路的Zayi ...
- 【HNOI2002】【矩阵快速幂】公交车路线
仍然是学弟出的题目的原题@lher 学弟将题目改成了多组数据,n在ll范围内,所以我就只讲提高版的做法. 链接:https://www.luogu.org/problem/show?pid=2233 ...
- SparkSQL——用之惜之
SparkSql作为Spark的结构化数据处理模块,提供了非常强大的API,让分析人员用一次,就会为之倾倒,为之着迷,为之至死不渝.在内部,SparkSQL使用额外结构信息来执行额外的优化.在外部,可 ...
- SpringBoot学习之自动依赖
在前面使用SSM集成时,我们可以使用注解实现无配置化注入,但是这种依赖被进行“人工干预了的”,换句话就是说我们手动进行装配,那么此时还没有达到SpringBoot这种自动装配的效果,那么究竟Sprin ...
- ASP.NET Core部署到Windows IIS
网上已经有许多ASP.NET Core关于Widows IIS部署的文章,在部署到服务器时遇到了一些问题,在这里我就不再对原理进行阐释(复制)了,只写下一些关键环节,想看原理的同学请参考官网,此文章作 ...
- numpy.squeeze()是干啥的
例子: a = 3 print np.squeeze(a) # 输出3 a = [3] print np.squeeze(a) # 输出3 a = [[3]] print np.squeeze(a) ...
- Why Helm? - 每天5分钟玩转 Docker 容器技术(160)
本章我们将学习 Helm,Kubernetes 的包管理器. 每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Hel ...