Using Themes in Spring MVC

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. 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”

2. ThemeChangeInterceptor

ThemeChangeInterceptor allows theme changes on every request with a simple request parameter that can be configured.

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.

Example : Using Themes in Spring MVC

Dispatcher Servlet

Properties files

We will need properties files starting with the prefix that we have configured in the themeSource bean.

CSS files

As mentioned in the properties files, we will need css files in the location mentioned

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

Output

Use URL to load black theme http://localhost:8080/sampleproject/employeeForm?theme=black
Using Themes in Spring MVC

Use URL to load blue theme http://localhost:8080/sampleproject/employeeForm?theme=blue
Using Themes in Spring MVC

Use URL to load pink theme http://localhost:8080/sampleproject/employeeForm?theme=pink
Using Themes in Spring MVC

Download Full Code – SpringMVCThemes.zip

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn