Spring MVC中通过ThemeSource接口来提供对动态更换样式的支持,并提供了ResourceBundleThemeSource这个具体实现类来提供通
过properties配置文件对theme中的样式的配置 
例如配置文件中 内容为 helloworld=theme/default/css/helloworld.css 
而jsp文件中使用 <link rel="stylesheet" type="text/css" href="<spring:theme code='helloworld'/>" /> 
来引用对helloworld这个样式文件的引入。由此来实现样式文件的动态引用,从而使spring mvc中可以实现换肤功能。 
如果说ThemeSource接口是提供了如何去取当前的theme的code与实际内容的mapping关系,那么spring mvc提供的另外一个interfaceThemeResolver则是提供了如何去设置当前使用的theme的手段。 
  Spring MVC提供了三个ThemeReslover的实现类,分别是 :

(1)FixedThemeResolver:固定格式的theme,不能在系统运行时动态更改theme. 
(2)SessionThemeResolver:theme name存放在session中key值为 org.springframework.web.servlet.theme.SessionThemeResolver.THEME 的session attribute中。可在运行中通过更改session中的相应的key值来动态调整theme的值。

(3) CookieThemeResolver:theme name存放在cookie中key值为 org.springframework.web.servlet.theme.CookieThemeResolver.THEME 中。可在运行中通过更改cookie中的相应的key值来动态调整theme的值。 
以上Themesource和ThemeResolver在servlet context xml中的配置示例如下 :

<!-- 样式-->
	<bean class="org.springframework.ui.context.support.ResourceBundleThemeSource"  id="themeSource">
    	<property name="basenamePrefix" value="theme."></property>
	</bean>
  	<bean id="themeResolver"  class="org.springframework.web.servlet.theme.SessionThemeResolver">
  		<property name="defaultThemeName" value="red" />
	</bean>

然后在classpath路径下创建样式文件:

我们默认使用的是red.properties中的内容,内容为多样式文件路径:

helloworld=theme/red/css/helloworld.css

这样我们需要创建css文件了:

创建theme.jsp,里面内容如下:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<link rel="stylesheet" type="text/css"
	href="<spring:theme code='helloworld'/>" />
</head>
<body>
	<div id="divTheme"><h1><spring:message code='hello'/></h1></div>
	<a href="changeTheme.action?themeName=blue"> blue</a>
	<a href="changeTheme.action?themeName=gray"> gray</a>
	<a href="changeTheme.action?themeName=red"> red</a>
</body>
</html>

Controlelr中的操作就是如下了:

@Controller
public class ThemeChange {
	private final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private ThemeResolver themeResolver;

	@RequestMapping("/changeTheme")
	public String changeTheme(HttpServletRequest request,
			HttpServletResponse response, String themeName) {
		if(themeName == null){
			themeName = "red";
		}
		logger.info("current theme is " + themeResolver.resolveThemeName(request));
		themeResolver.setThemeName(request, response, themeName);
		logger.info("current theme change to " + themeResolver.resolveThemeName(request));
		return "theme";
	}
}

结果页面如下:

(1)红色样式

(2)蓝色样式

springMVC源码分析--动态样式ThemeResolver(一)的更多相关文章

  1. springMVC源码分析--动态样式ThemeResolver(二)

    在上一篇博客springMVC源码分析--动态样式ThemeResolver(一)中我们介绍了多样式ThemeResolver的使用方法,接下来我们对源码进行简单的分析一下. ThemeResolve ...

  2. SpringMVC源码分析--容器初始化(五)DispatcherServlet

    上一篇博客SpringMVC源码分析--容器初始化(四)FrameworkServlet我们已经了解到了SpringMVC容器的初始化,SpringMVC对容器初始化后会进行一系列的其他属性的初始化操 ...

  3. springMVC源码分析--DispatcherServlet请求获取及处理

    在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...

  4. 8、SpringMVC源码分析(3):分析ModelAndView的形成过程

    首先,我们还是从DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throw ...

  5. SpringMVC源码分析(3)DispatcherServlet的请求处理流程

    <springmvc源码分析(2)dispatcherservlet的初始化>初始化DispatcherServlet的多个组件. 本文继续分析DispatcherServlet解析请求的 ...

  6. 7、SpringMVC源码分析(2):分析HandlerAdapter.handle方法,了解handler方法的调用细节以及@ModelAttribute注解

    从上一篇 SpringMVC源码分析(1) 中我们了解到在DispatcherServlet.doDispatch方法中会通过 mv = ha.handle(processedRequest, res ...

  7. springMVC源码分析--ViewNameMethodReturnValueHandler返回值处理器(三)

    之前两篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)和springMVC源码分析--HandlerMethodReturnValu ...

  8. springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)

    在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...

  9. springMVC源码分析--RequestParamMethodArgumentResolver参数解析器(三)

    之前两篇博客springMVC源码分析--HandlerMethodArgumentResolver参数解析器(一)和springMVC源码解析--HandlerMethodArgumentResol ...

随机推荐

  1. Mac终端开启代理

    使用shadowsocks开启代理,只支持浏览器开启代理,其他软件是不能够开启代理的,因为这个问题,自己一直无法用本地终端 git clone 一些库,网上查找方法,总结如下: 步骤一:开启shado ...

  2. 《C++ Primer》学习笔记:迭代器介绍

    <C++Primer>(第五版)中,3.4.1的例题中使用一个名为text的字符串向量存放文本文件中的数据,输出text中的内容,刚开始我这样写: #include <iostrea ...

  3. [HNOI 2011]数学作业

    Description 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 N 和 M,要求计算 Concatenate (1 .. N) Mod M 的值,其中 Con ...

  4. UVALive - 3882:And Then There Was One

    约瑟夫环 f[i]表示有i个人先处理第k个人,最后被处理的人是谁 #include<cstdio> #include<cstdlib> #include<algorith ...

  5. hdu2669与hdu1576(扩展欧几里德)

    模板: int Extend_Euclid(int a, int b, int &x, int &y){         if(b == 0){             x = 1; ...

  6. Codeforces #Round 406(Div.2)

    来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------- 大家好,我是一个假人.在学习O ...

  7. ubuntu linux c学习笔记----共享内存(shmget,shmat,shmdt,shmctl)

    shmget int shmget(key_t key, size_t size, int flag); key: 标识符的规则 size:共享存储段的字节数 flag:读写的权限 返回值:成功返回共 ...

  8. ubuntu上的附件-终端和用快捷键ctrl+alt+f1 有啥区别

    ctrl +alt +Fn 打开的是模拟终端,简单说来,linux系统一开机会自动打开6个模拟终端,然后自动切换到其中一个(一般来说是切换到图形界面的那个也就是说窗口管理器是在这6个模拟终端中运行的) ...

  9. C语言第二次作业-----顺序结构

    一:改错题 (1)输出指定信息: 将给定源代码输入编译器: 执行编译命令,发现编译器报错,错误信息如下: 经检查,发现源程序将"stdio.h"误拼为"stido.h&q ...

  10. Win2003及2008R2重启自动登录设置方法

    在windows系统中,使用最多的可能就是远程操作了,关于远程操作的那些事很多用户还是有些迷茫的.如果win2003系统远程重启后,要重新登录系统才能启用远程功能,这就十分的麻烦,如何才能实现重启后的 ...