Theming Support
Spring MVC provides comprehensive support for theming, and enabling it in web applications is easy.
For example, in the sample contact application in this chapter, we want to create a theme and name it
standard. First, in the folder /src/main/webapp/WEB-INF/classes, create a file named
standard.properties with the content in Listing 17-15.
Listing 17-15. The standard.properties File
styleSheet=resources/styles/standard.css
As shown in Listing 17-15, the properties file contains a property named styleSheet, which points to
the style sheet to use for the standard theme. This properties file is the ResourceBundle for the theme,
and you can add as many components for your theme as you want (for example, the logo image location,
background image location, and so on).
The next step is to configure the DispatcherServlet's WebApplicationContext for theming support
by modifying the configuration file (servlet-context.xml). First, with in the <interceptors> definition,
we need to add one more interceptor bean, as shown in Listing 17-16.
Listing 17-16. Configure the Theme Interceptor
<interceptors>
<beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="lang"/>
</interceptors>
As shown in Listing 17-16, the new interceptor is highlighted in bold (the order is not important).
The ThemeChangeInterceptor class intercepts every request for changing the theme.
Second, add the bean definitions in Listing 17-17 into the configuration file (servlet-context.xml).
Listing 17-17. Configure Theme Support
<beans:bean class="org.springframework.ui.context.support.ResourceBundleThemeSource"
id="themeSource"/>
<beans:bean class="org.springframework.web.servlet.theme.CookieThemeResolver"
id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard"/>
As shown in Listing 17-17, two beans are defined. The first bean, implemented by the
ResourceBundleThemeSource class, is responsible for loading the ResourceBundle of the active theme. For
example, if the active theme is called standard, the bean will look for the file standard.properties as the
ResourceBundle of the theme. The second bean, implemented by the CookieThemeResolver class, is used
to resolve the active theme for users. The property defaultThemeName defines the default theme to use,
which is the standard theme. Note that as its name implies, the CookieThemeResolver class uses cookies
to store the theme for the user. There also exists the SessionThemeResolver class that stores the theme
attribute in a user's session.
Now the standard theme is configured and ready for use in our views. Listing 17-18 shows the
revised contact list view (/WEB-INF/views/contacts/list.jspx) with theme support.
Listing 17-18. Contact List View with Theme Support
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Other code omitted -->
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home