Spring 3 MVC: Themes in Spring-Tutorial with Example

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 used LocaleChangeInterceptor to intercept the change in locale and ReloadableResourceBundleMessageSource 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 class org.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)


  • DESH 13 November, 2013, 16:56

    Hey, I’m using this code directly. but getting this exception:-
    java.lang.NoClassDefFoundError: org/apache/tiles/startup/BasicTilesInitializer
    at java.lang.ClassLoader.defineClass1(Native Method)
    I have added tiles-core-3.0.1.jar but still this prob coming. can anybody help me.

  • Sergei 14 November, 2013, 19:30

    Viral, you forgot to mention the changes in layout.jsp

     <link rel="stylesheet" href="<spring:theme code="css"/>" type="text/css"/>
    • Mahendra Bagul 26 November, 2013, 17:24


  • sandeep 29 November, 2013, 17:13

    some programs are not running success fully like i18,themes
    in themes add a link like:<link rel="stylesheet" href="” type=”text/css” /> in layout.jsp if it not works remove jar files and add new jar files .
    or else refer this site ex:http://www.devmanuals.com/tutorials/java/spring/spring3/mvc/Spring3-MVC-Themes.html

    • sandeep 29 November, 2013, 17:14

      <link rel="stylesheet" href="” type=”text/css” />

  • kyochan19792002 7 December, 2013, 0:09

    The correction for layout.jsp should be

    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
    <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="<spring:theme code="css"/>">
    <title><tiles:insertAttribute name="title" ignore="true" /></title>
    • jrp 23 December, 2013, 19:32

      Thanks kyochan19792002

    • Boechi 6 June, 2014, 18:16

      Thanks, saved me lot of time to search this out.

    • eagleeye1984 9 July, 2014, 6:54

      Thanks. It really helped. You saved me 15 minutes of spring doc reading.

  • Nitin 2 September, 2014, 18:03

    Hello Viral,

    I am facing issue with loading themes. The scenario is described below.
    The controller in the application have request mapping at class level and method level so the URL for a request is generally in the following format. “http://server:port/context/req_map_class/req_map_method.jsp”. When I try to switch the theme, the css url is of the format “http://server:port/context/req_map_class/theme/blue.css”, which is non-existent. The css is actually available “http://server:port/context/theme/blue.css”.

    Please advise.


Leave a Reply

Your email address will not be published. Required fields are marked *


To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current ye@r *