Spring 3 MVC: Themes In Spring-Tutorial With Example---reference

Welcome to Part 6 of Spring 3.0 MVC Series. In previous article we saw how to add Internationalization i18n and Localization L10n support to Spring 3.0 based web application. We usedLocaleChangeInterceptor to intercept the change in locale andReloadableResourceBundleMessageSource class to add message resources properties.

In this part we will see how to add Themes in Spring MVC. We will create three different themes and add functionality in our HelloWorldSpring project for user to select any of the available theme. Once user selects a theme, we can save it in cookies so that it can be persisted between different sessions.

Introduction to Themes in Spring MVC

A theme is a collection of static resources, typically style sheets and images, that affect the visual style of the application. We can apply Spring Web MVC framework themes to set the overall look-and-feel of application, thereby enhancing user experience.

To use themes in your web application, you must set up an implementation of the org.springframework.ui.context.ThemeSource interface. The WebApplicationContext interface extends ThemeSource but delegates its responsibilities to a dedicated implementation. By default the delegate will be an org.springframework.ui.context.support.ResourceBundleThemeSource implementation that loads properties files from the root of the classpath. To use a custom ThemeSource implementation or to configure the base name prefix of the ResourceBundleThemeSource, you can register a bean in the application context with the reserved name themeSource. The web application context automatically detects a bean with that name and uses it.

When using the ResourceBundleThemeSource, a theme is defined in a simple properties file. The properties file lists the resources that make up the theme. Here is an example:

Our Goal

Our goal is to change the Hello World Spring 3 MVC and add Theme support to it. User will have option to select theme from 3 predefined themes (default, black and blue).

Adding Theme support to Spring 3 MVC

Let us configure our Spring 3 MVC application to add Theme support. For this we will add following code into spring-servlet.xml file.

File: WebContent/WEB-INF/spring-servlet.xml

<bean id="themeSource"
        <property name="basenamePrefix" value="theme-" />

<!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor"
    <property name="paramName" value="theme" />
<bean id="themeResolver"
    <property name="defaultThemeName" value="default" />

<bean id="handlerMapping"
    <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
            <ref bean="themeChangeInterceptor" />

In the above configuration, first we have added themeSource bean. Notice that this bean is an instance of class ResourceBundleThemeSource and we also have specified a property basenamePrefix with value“theme-“. ResourceBundleThemeSource class will load the property files containing theme definition starting with prefix “theme-“. Thus, if we have defined 3 new themes in our project (default, black and blue) then we will create 3 property files while will have certain configuration properties. Also these files will be placed under the project classpath.

Next, we defined an interceptor bean themeChangeInterceptor which is an instance of classorg.springframework.web.servlet.theme.ThemeChangeInterceptor. Also note here that we have specified a property paramName with value theme. This interceptor is invoked whenever a request is made with parameter “theme” with different values.

Once the themeChangeInterceptor intercepts the change in the theme, the changes are then stored in the cookies using class org.springframework.web.servlet.theme.CookieThemeResolver. We have configured this class in our spring-servlet.xml configuration file. Also note that we have specified default theme name with this bean.

Now create following properties files in resources/ folder of the project.

File: resources/theme-default.properties


File: resources/theme-blue.properties


File: resources/theme-black.properties


CSS Stylesheet for different Themes

Let us create 3 CSS stylesheet which will act as the theme files for our project. Create following CSS files in WebContent/themes folder.

File: WebContent/themes/default.css

body {

    background-color: white;

    color: black;


File: WebContent/themes/blue.css

body {

    background-color: #DBF5FF;

    color: #007AAB;


File: WebContent/themes/black.css

body {

    background-color: #888;

    color: white;


JSP View changes

We are almost done with the changes and last bit that is remaining is to add a functionality for user to select the theme from UI. For this we will change the header.jsp file and add 3 links with different themes. User can click on any of this link and change the theme of webapplication.

File: WebContent/WEB-INF/jsp/header.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<h3><spring:message code="label.title"/></h3>

<span style="float: right">

    <a href="?lang=en">en</a>


    <a href="?lang=de">de</a>


<span style="float: left">

    <a href="?theme=default">def</a>


    <a href="?theme=black">blk</a>


    <a href="?theme=blue">blu</a>


Notice that in above JSP changes we created 3 links with argument “?theme=”. Thus whenever user will click these links, a new parameter will be passed in the request with the appropriate theme. The request interceptor of Spring will fetch this value and change the theme accordingly.

That’s All Folks

That’s pretty much it :) We just added Theme support to our demo Spring 3.0 MVC application. All you have to do is just execute the app in Eclipse. Press Alt + Shift + X, R.

Download Source Code

Click here to download Source code (21kb, zip)

reference from:


时间: 11-24

Spring 3 MVC: Themes In Spring-Tutorial With Example---reference的相关文章

Spring Web MVC基础

1.MVC模式简介 M-Model模型 模型(Model)的职责是负责业务逻辑.包含两层:业务数据和业务处理逻辑.比如实体类.DAO.Service都属于模型层. V-View视图 视图(View)的职责是负责显示界面和用户交互(手机用户信息).属于视图的组件是不包含业务逻辑和控制逻辑的JSP C-Controller 控制器 控制器是模型层M和视图层V之间的桥梁,用于控制流程比如: 在Servlet项目中的单一控制器ActionServlet. 2.什么是Spring Web MVC Spri

Spring Web MVC 基础

一.Spring Web MVC简介 1.1.MVC模式简介 *M-Model模型 模型(Model)的职责是负责业务逻辑.包含两层:业务数据和业务处理逻辑.比如实体类.DAO.Service都属于模型层. *V-View视图 视图(View)的职责是负责显示界面和用户交互(收集用户信息).属于视图的组件是不包含业务逻辑和控制逻辑的JSP. *C-Controller控制器 控制器是模型层M和视图层V之间的桥梁,用于控制流程比如:在Servlet项目中的单一控制器ActionServlet. 1

(Spring文档翻译)Part V, the Web 17.1 Spring Web MVC framework介绍

指南文档的这个部分涵盖了Spring框架对表现层(特别是基于Web的表现层)以及WebSocket消息风格的web应用的支持. Spring框架拥有自己的web框架,Spring Web MVC,包含在前面几个章节.之后的几章是关于Spring框架对其他web技术的集成支持,像JSF等. 再之后是Spring框架的MVC porlet 框架. Spring 的MVC框架围绕着DispatcherServlet设计,DispatcherServlet将请求转发给handler,用可配置的handl

Spring - Sring MVC入门

2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的.   另外还有一种基于组件的.事件驱动的Web框架在此就不介绍了,如Tapestry.JSF等.   Spring Web MVC也是服务到工作者模式的实现,但

Features of Spring Web MVC

21.1.1 Features of Spring Web MVC Spring Web Flow Spring Web Flow (SWF) aims to be the best solution for the management of web application page flow. SWF integrates with existing frameworks like Spring MVC and JSF, in both Servlet and Portlet environ

Spring 4 MVC+Apache Tiles 3 Example

In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuration. Apache Tiles is a template based, composite view framework: it allows to reuse page pieces across the application, keeping consistent look and feel

Spring Security3 - MVC 整合教程 (初识Spring Security3)

下面我们将实现关于Spring Security3的一系列教程.最终的目标是整合Spring Security + Spring3MVC完成类似于SpringSide3中mini-web的功能. Spring Security是什么? 引用 Spring Security,这是一种基于Spring AOP和Servlet过滤器的安全框架.它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理身份确认和授权.在Spring Framework基础上,Spring Security充分利用了

spring 3 mvc hello world + mavern +jetty

Spring 3 MVC hello world example By mkyong | August 2, 2011 | Updated : June 15, 2015 In this tutorial, we show you a Spring 3 MVC hello world example, using Maven build tool. Technologies used : Spring 3.2.13.RELEASE Maven 3 JDK 1.6 Eclipse 4.4 Boos

Spring 4 MVC example with Maven

In this tutorial, we show you a Spring 4 MVC example, using Maven build tool. Technologies used : Spring 4.3.0.RELEASE Maven 3 JDK 1.8 Eclipse Mars.2 Release (4.5.2) Boostrap 3 1. Project Structure Download the project source code and review the proj