springMVC源码分析--动态样式ThemeResolver(一)
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(一)的更多相关文章
- springMVC源码分析--动态样式ThemeResolver(二)
在上一篇博客springMVC源码分析--动态样式ThemeResolver(一)中我们介绍了多样式ThemeResolver的使用方法,接下来我们对源码进行简单的分析一下. ThemeResolve ...
- SpringMVC源码分析--容器初始化(五)DispatcherServlet
上一篇博客SpringMVC源码分析--容器初始化(四)FrameworkServlet我们已经了解到了SpringMVC容器的初始化,SpringMVC对容器初始化后会进行一系列的其他属性的初始化操 ...
- springMVC源码分析--DispatcherServlet请求获取及处理
在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...
- 8、SpringMVC源码分析(3):分析ModelAndView的形成过程
首先,我们还是从DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throw ...
- SpringMVC源码分析(3)DispatcherServlet的请求处理流程
<springmvc源码分析(2)dispatcherservlet的初始化>初始化DispatcherServlet的多个组件. 本文继续分析DispatcherServlet解析请求的 ...
- 7、SpringMVC源码分析(2):分析HandlerAdapter.handle方法,了解handler方法的调用细节以及@ModelAttribute注解
从上一篇 SpringMVC源码分析(1) 中我们了解到在DispatcherServlet.doDispatch方法中会通过 mv = ha.handle(processedRequest, res ...
- springMVC源码分析--ViewNameMethodReturnValueHandler返回值处理器(三)
之前两篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)和springMVC源码分析--HandlerMethodReturnValu ...
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...
- springMVC源码分析--RequestParamMethodArgumentResolver参数解析器(三)
之前两篇博客springMVC源码分析--HandlerMethodArgumentResolver参数解析器(一)和springMVC源码解析--HandlerMethodArgumentResol ...
随机推荐
- mysql列约束
列属性(约束)1: 是否允许为空(not null) --not null不允许为空create table t_1( a tinyint(3) zerofill not null, b ...
- 初级Django学习
创建Django项目django-admin startproject project_test1 新建应用:python manage.py startapp booktest admin: 权限管 ...
- [HNOI 2006]鬼谷子的钱袋
Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会,其中有一 ...
- [HNOI2010]STONE取石头游戏
题目描述 A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参加比赛. 与经典的取石子游戏相比,A公司举办 ...
- Set 集合
[题目描述]现在给你一些连续的整数,它们是从 A 到 B 的整数.一开始每个整数都属于各自的集合,然后你需要进行如下操作:每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于 P 的公共质因数, ...
- hdu 5135(2014广州—状态dp)
t题意:给你n条边,构造任意个三角形,一个三角形恰好只用3条边,每条边只能一次,求面积最大值 思路: 最开始想的是先排序从大到小取,但感觉并不怎么靠谱. 最多12条边,所以可以求出所有可能的三角形面积 ...
- hdu3183 RMQ
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- bzoj3702二叉树 线段树合并
3702: 二叉树 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 600 Solved: 272[Submit][Status][Discuss] ...
- Linux下安装3.0以上的python
Linux下自带的python2.7是不建议删除的,很多系统软件依赖python2.7,但是现在我们学习python一般需要python3.0,下面介绍安装python3.0. 1.进入python官 ...
- glusterfs 4.0.1 api 分析笔记1
一般来说,我们写个客户端程序大概的样子是这样的: /* glfs_example.c */ // gcc -o glfs_example glfs_example.c -L /usr/lib64/ - ...