Table 7-2.LocaleResolver Implementations
Implementation Description
FixedLocaleResolver Always returns the same locale for each request that can be set
with setDefaultLocale().
AcceptHeaderLocaleResolver Resolves a locale from the HTTP headers of the client request.
Most browsers and other HTTP clients send the current locale in
the header accept-language.
SessionLocaleResolver Resolves the locale from an attribute held in the user’s
HttpSession. The attribute key can be configured on the
resolver. A locale resolved from the session is not persisted
between sessions; it must be set once per user per session.
CookieLocaleResolver Similar to a SessionLocaleResolver, but the choice of locale is
persisted between sessions.
■NoteIf you use a SessionLocaleResolveror a CookieLocaleResolver,it will fall back to the
accept-language locale in the request headers if a locale cannot be found in the session or cookie,
respectively.
Listing 7-18 details the configuration of a LocaleResolverin the ApplicationContext.
Listing 7-18.Configuring the AcceptHeaderLocaleResolver
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>
MessageSource Beans
A MessageSourceis an object that can resolve messages, given a key to that message. Spring
provides two concrete implementations of this interface for use in your applications.
- ResourceBundleMessageSourceis based on the familiar standard resource bundle han-
dling in the JVM. - ReloadableResourceBundleMessageSourceworks in similar fashion but has the added
advantage that changes to the properties files will be picked up without having to
restart the web application.
Whichever implementation you choose, the usage of a MessageSourcebean is the same.
Listing 7-19 shows a MessageSourcedefined in the ApplicationContextfile.
CHAPTER 7 ■THE VIEW LAYER 219