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. [Awson原创]洪水(flood)

    Description Awson是某国际学校信竞组的一只菜鸡.今年,该市发生了千年难遇的洪水.被监禁在学校的Awson不甘怠堕,想将自己投入到公益服务事业中去.这天,他偷了H老师的小电驴,偷偷地溜出 ...

  2. [AHOI2005]洗牌

    题目描述 为了表彰小联为Samuel星球的探险所做出的贡献,小联被邀请参加Samuel星球近距离载人探险活动. 由于Samuel星球相当遥远,科学家们要在飞船中度过相当长的一段时间,小联提议用扑克牌打 ...

  3. 【网络流】【BZOJ1070】【SCOI2007】修车

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1070 题意:问你如何分配老司机使得每部车的等待时间之和最短. 解题思路:本题不易正做,考虑 ...

  4. ●BZOJ 4289 PA2012 Tax

    ●赘述题目 算了,题目没有重复的必要. 注意理解:对答案造成贡献的是每个点,就是了. 举个栗子: 对于如下数据: 2 1 1 2 1 答案是 2: ●题解 方法:建图(难点)+最短路. 先来几个链接: ...

  5. NOIP2014-5-24模拟赛

    Problem 1 护花(flower.cpp/c/pas) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时 ...

  6. 51nod 平均数(马拉松14)

    平均数 alpq654321 (命题人)   基准时间限制:4 秒 空间限制:131072 KB 分值: 80 LYK有一个长度为n的序列a. 他最近在研究平均数. 他甚至想知道所有区间的平均数,但是 ...

  7. HDU 4526 拼车记

    话说威威猫有一次去参加比赛,虽然学校离比赛地点不太远,但威威猫还是想坐出租车去.大学城的出租车总是比较另类,有“拼车”一说,也就是说,你一个人坐车去,还是一堆人一起,总共需要支付的钱是一样的(每辆出租 ...

  8. bzoj1934

    1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 2406  Solved: 1498[Submit][ ...

  9. 有源汇上下界可行流(POJ2396)

    题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...

  10. Spring中<context:annotation-config/>

    最近在研究Spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Sp ...