Expert Spring MVC and Web Flow

(Dana P.) #1
href="<spring:theme code="style"/>"
type="text/css"/>
</head>
<body background="<spring:theme code="background"/>">
<h1><spring:theme code="welcome.message"/></h1>



The theme tags highlighted in bold will be replaced with the corresponding value from
the winter.properties. We might also define a new theme, Summer, that we encapsulate in
summer.properties, shown in Listing 7-16.


Listing 7-16.The “Summer” Theme Definition Properties File


style=/styles/summer.css
background=/images/sun.png
welcome.message=Phew! It's a scorcher.


Now, our view can look very different when we switch between winter and summer
themes. You can use Spring’s theme support to dictate a fixed theme per application that all
users see. It can also allow individual users of the application to use a different theme. The
missing piece of the puzzle, then, is knowing how the choice of theme is made for any given
application or user. Let’s see how Spring does that with ThemeSources and ThemeResolvers.


ThemeSources


The properties files that contain the theme’s keys are located in the classpath and accessed as
resource bundles. By default a ResourceBundleThemeSourceis used that will look in the root of
the classpath for the properties files.
If you want to place your files elsewhere in the classpath, or if you need to use a custom
ThemeSourceimplementation, you can specify a bean in your context with the reserved name
themeSourceto do this. A bean with this name will automatically be detected and used by the
web ApplicationContext. Listing 7-17 has an example that alters the basename prefix.


Listing 7-17.ThemeSource Defined in the Web Application Context


<bean id="themeSource"
class="org.springframework.ui.context.support.ResourceBundleThemeSource">




Using the bean definition in the preceding listing, we can place our theme definition files in
the specified location of the classpath. Thus, they can be found at com.apress.expertspring-
mvc.themes.winter.propertiesand com.apress.expertspringmvc.themes.summer.properties,
respectively.


CHAPTER 7 ■THE VIEW LAYER 217
Free download pdf