http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-themeresolver

做了一个小demo,放到了码云上,点我

1、概览

你可以应用Spring Web MVC框架themes来为你的应用设置全局的外观。

一个theme就是静态资源的一个集合 -- 通常是css和图片,会影响应用的视觉样式。

2、定义themes

想在你的web应用中使用这些themes,你必须设置 org.springframework.ui.context.ThemeSource 的一个实现。WebApplicationContext接口扩展了ThemeSource,但委托它的责任到指定的实现。 默认,该委托会是一个 org.springframework.ui.context.support.ResourceBundleThemeSource 实现,该实现从classpath的根路径加载properties文件。 想要使用一个自定义的ThemeSource实现或者想要配置ResourceBundleThemeSource的base name prefix,你可以住一个bean -- 使用保留名themeSource。web应用context会自动探测并使用它。

当使用ResourceBundleThemeSource时,theme是被定义在一个简单的properties文件中。该properties文件列出了组成该theme的资源。例如:

styleSheet=/themes/cool/style.css
background=/themes/cool/img/coolBg.jpg

properties的keys是view code 中指向主题元素的名字。对一个JSP来说,你可以使用 “spring:theme”自定义标签来完成 -- 非常类似于”spring:message”标签。 下面的JSP片段使用了前面定义的theme:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>
</head>
<body style="background=<spring:theme code='background'/>">
...
</body>
</html>

默认情况下,ResourceBundleThemeSource使用一个空的base name前缀。 结果就是,会从classpath的根路径来加载properties文件。所以,你可以将”cool.properties”主题定义文件放在classpath的根路径下,例如,在”/WEB-INF/classes”中。 ResourceBundleThemeSource会使用标准的Java resource bundle加载机制,允许主题的国际化。 例如,”/WEB-INF/classes/cool_nl.properties”,会引用Dutch文字。

3、theme resolvers

当你定义了themes,你需要决定使用哪个theme。DispatcherServlet 会查找bean themeResolver以找出使用哪个ThemeResolver实现。 theme resolver的工作方式非常类似于LocaleResolver。 它会探测用于某个request的theme,也可以更改该request的theme。 下面是Spring提供的theme resolvers :

Table 22.5. ThemeResolver implementations

Class Description

FixedThemeResolver

Selects a fixed theme, set using the defaultThemeName property.

SessionThemeResolver

The theme is maintained in the user’s HTTP session. It only needs to be set once for each session, but is not persisted between sessions.

CookieThemeResolver

The selected theme is stored in a cookie on the client.

Spring还提供了一个 ThemeChangeInterceptor,允许request使用一个简单的parameter来改变使用的theme。 -- 看起来类似于LocaleChangeInterceptor。

Spring 4 官方文档学习(十一)Web MVC 框架之themes的更多相关文章

  1. Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC

    内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...

  2. Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图

    接前面的Spring 4 官方文档学习(十一)Web MVC 框架,那篇太长,故另起一篇. 针对web应用的所有的MVC框架,都会提供一种呈现views的方式.Spring提供了view resolv ...

  3. Spring 4 官方文档学习(十一)Web MVC 框架

    介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...

  4. Spring 4 官方文档学习(十二)View技术

    关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...

  5. Spring Boot 官方文档学习(一)入门及使用

    个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...

  6. Spring boot官方文档学习(一)

    个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...

  7. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)

    接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...

  8. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

    本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...

  9. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)

    题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...

  10. Spring 4 官方文档学习(十一)Web MVC 框架之编码式Servlet容器初始化

    在Servlet 3.0+ 环境中,你可以编码式配置Servlet容器,用来代替或者结合 web.xml文件.下面是注册DispatcherServlet : import org.springfra ...

随机推荐

  1. Python isnumeric() 方法

    描述 Python isnumeric() 方法检测字符串是否只由数字组成.这种方法是只针对unicode对象. 注:定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可,具体可以查 ...

  2. [转]玩转UltraEdit,UE常见快捷键操作

    编辑器Ultraedit快捷键说到编辑器的快捷键,VIM是无与伦比的.要反对,也得是带脚踏板的EmaCS.UE还是有差距的,很大差距.注意:VIM是开源.免费的,而UE则需要注册.UE是Windows ...

  3. HDU 3435 A new Graph Game(最小费用最大流)&amp;HDU 3488

    A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【Android】3.22 示例22--LBS云检索功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 简介:介绍如何使用LBS.云检索用户自有数据. 详述: (1)LBS.云是百度地图针对LBS开发者推出的平台级 ...

  5. Java并发:volatile内存可见性和指令重排

    volatile两大作用 1.保证内存可见性 2.防止指令重排 此外需注意volatile并不保证操作的原子性. (一)内存可见性 1 概念 JVM内存模型:主内存和线程独立的工作内存 Java内存模 ...

  6. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: “object”未包括“get_Range”的定义

    asp.net操作Excel合并单元格时,抛出了异常: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: "object" ...

  7. nfs missing codepage or helper program, or other error

    [root@xxxxx ~]# /bin/mount -t nfs -o nosuid,noexec,nodev,noatime,intr,rsize=,wsize= xxx.xxx.xxx.xxx: ...

  8. C# 简单Tcp通信demo

    Client 代码 private void btnSend_Click(object sender, EventArgs e) { TcpClient tcpClient = new TcpClie ...

  9. LeetCode: Linked List Cycle 解题报告

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  10. SharePoint自动化系列——Solution auto-redeploy using Selenium(C#)

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 本来的想法是做一个可以自动卸载并且部署新solution到SharePoint farm的tool ...