Theme is a collection of static resources, normally style sheets and images, that affect the look and feel of an application. Themes are helpful in enriching user experience. In this post we will see example of Using Themes in Spring MVC application.
Themes can be configured in Spring Using the following
1 2 3 |
org.springframework.ui.context.support.ResourceBundleThemeSource org.springframework.web.servlet.theme.ThemeChangeInterceptor org.springframework.web.servlet.theme.CookieThemeResolver |
1. ThemeSource
To use themes in application, we must set up an implementation of the
org.springframework.ui.context.ThemeSource interface.
By default the
org.springframework.ui.context.support.ResourceBundleThemeSource is used to load the properties file, but you can also have a custom theme source.
You can also configure base name prefix of the theme source using the property “basenamePrefix”
1 2 3 4 |
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource"> <property name="basenamePrefix" value="appTheme-" /> </bean> |
2. ThemeChangeInterceptor
ThemeChangeInterceptor allows theme changes on every request with a simple request parameter that can be configured.
1 2 3 4 |
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"> <property name="paramName" value="theme" /> </bean> |
So using the ThemeSource and ThemeChangeInterceptor the app knows which property file to load to get the theme.
example :
Themesource basenamePrefix = appTheme-
ThemeChangeInterceptor param theme = black
Then the framework search for file appTheme-black.properties in classpath to load it.
3. ThemeResolver
ThemeResolver decide which theme to use for every request. There are 3 ThemeResolver
FixedThemeResolver – uses fixed theme using defaultThemeName property
SessionThemeResolver – uses the theme maintained in session
CookieThemeResolver – uses the theme maintained in Cookies
We can give a default theme name in the ThemeResolvers.
We will use the CookieThemeResolver in our example.
4. Resource
For themes we mostly need css and images which we will store in the resources folder next to webapp. We need to map the file location to a resource mapping. We will use a mvc annotation.
1 |
<mvc:resources location="/resources/" mapping="/resources/**" /> |
Example : Using Themes in Spring MVC
Dispatcher Servlet
Dispatcher Servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource"> <property name="basenamePrefix" value="appTheme-" /> </bean> <bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"> <property name="paramName" value="theme" /> </bean> <bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver"> <property name="defaultThemeName" value="black" /> <property name="cookieName" value="kscodestheme" /> <property name="cookieMaxAge" value="3600" /> </bean> <mvc:interceptors> <ref bean="themeChangeInterceptor" /> </mvc:interceptors> <mvc:resources location="/resources/" mapping="/resources/**" /> <mvc:annotation-driven /> </beans> |
Properties files
We will need properties files starting with the prefix that we have configured in the themeSource bean.
1 2 3 4 5 6 7 8 9 10 11 |
appTheme-black.properties stylesheet.name=resources/css/black.css background.color=black appTheme-blue.properties stylesheet.name=resources/css/blue.css background.color=blue appTheme-pink.properties stylesheet.name=resources/css/pink.css background.color=pink |
CSS files
As mentioned in the properties files, we will need css files in the location mentioned
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
black.css body, input { font-size: 14px; color: white; } h1, h2 { font-size: 30px; } blue.css body, input { font-size: 12px; color: yellow; } h1, h2 { font-size: 26px; } pink.css body, input { font-size: 14px; color: red; } h1, h2 { font-size: 24px; } |
Changes in JSP file
As you may have noticed we had 2 properties in the properties file – stylesheet.name and background.color
So we will use both properties in the jsp, just demonstrate the use of these props. We will use one to define the style-sheet ref link and the other to define a direct css property of body tag.
The keys of the properties are the names that we refer to the themed elements from the view code. For JSP, use the spring:theme custom tag, which is very similar to the spring:message tag that we used in the Spring MVC Internationalization and Localization
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <html> <head> <link rel="stylesheet" href="<spring:theme code='stylesheet.name'/>" type="text/css" /> </head> <body bgcolor="<spring:theme code='background.color'/>"> <h2>Spring MVC Themes</h2> <form:form method="post" action="addEmployee" commandName="employee"> <table> <tr> <td>First Name</td> <td><input type="text" name="firstName" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastName" /></td> </tr> <tr> <td>Department Name</td> <td><input type="text" name="department" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> </body> </html> |
Output
Use URL to load black theme http://localhost:8080/sampleproject/employeeForm?theme=black
Use URL to load blue theme http://localhost:8080/sampleproject/employeeForm?theme=blue
Use URL to load pink theme http://localhost:8080/sampleproject/employeeForm?theme=pink