Expert Spring MVC and Web Flow

(Dana P.) #1

ThemeResolvers


The ThemeResolverdoes a very similar job to that of its colleagues, LocaleResolverand
ViewResolver. It returns the correct Themeto the dispatcher based upon the application’s
configuration.
Spring offers three concrete ThemeResolverimplementations, shown in Table 7-1.

Table 7-1.Spring ThemeResolver Implementations
Implementation Description
FixedThemeResolver Always resolves the theme keyed by the name of its defaultThemeName
property. All users of the application will see the same theme.
SessionThemeResolver The name of the theme is stored in the HttpSessionfor each user. It
needs to be set for each user and is not persisted between sessions.
CookieThemeResolver Similar to the SessionThemeResolver, but the value is stored in a cookie
and is persisted between sessions.

The ThemeResolveris listed in the DispatcherServlet’s configuration file using the
reserved name themeResolver. The dispatcher will automatically use this bean to resolve
the correct theme or each application request.

ThemeChangeInterceptor
A final note on theme support: If you need to alter the theme on every request, you can config-
ure a ThemeChangeInterceptor, which is based on Spring’s interceptor support that you learned
about earlier in the book. A configurable parameter name can alter the theme on a per-request
basis.

Internationalization in the View Layer


Internationalization, or i18n, is an essential aspect of the view layer. After all, this is the layer
that renders your data to the end user and in the global marketplace; your users might be any-
where. The majority of them will expect to see local formatting used for numbers and dates at
minimum. Many will expect to be able to read much of your site in their own language.
This section contains a brief tour of the important parts of the view layer that you can
leverage in your applications to offer i18n of your site. We independently cover most of the
subsections you’ll see here elsewhere in this chapter and the next.

Locale Resolution


Recall from earlier in the book that the job of LocaleResolvers was to determine the preferred
or default locale of the user making the current request. Much of the support for i18n in other
view layer components relies on the job that the LocaleResolverdoes. We’ll recap on the avail-
able types of LocaleResolverand how they work in Table 7-2.

218 CHAPTER 7 ■THE VIEW LAYER

Free download pdf