Expert Spring MVC and Web Flow

(Dana P.) #1

Listing 5-29.LocaleResolver Interface


package org.springframework.web.servlet;


public interface LocaleResolver {
Locale resolveLocale(HttpServletRequest request);


void setLocale(HttpServletRequest request, HttpServletResponse response,
Locale locale);
}


■Tip To clear out the locale using this strategy interface, simply set the localeproperty of the
LocaleResolverimplementation to null.


Figure 5-2.LocaleResolver class hierarchy


Once you have chosen an appropriate LocaleResolver, you need to register it with
the DispatcherServletby creating a bean definition with the namelocaleResolverin your
ApplicationContext. The DispatcherServletwill look for that name before falling back to its
default implementation, the AcceptHeaderLocaleResolver.
The LocaleResolverinterface defines how to query and set a Locale, but it does not define
when or how you should use it. Spring MVC’s i18n infrastructure (discussed in Chapters 7 and
9) will read out the Localewhen performing translations and culture-specific formatting. This
work is typically done under the hood, so your job as a developer is to set the locale via the
LocaleResolver.


<<interface>>
LocaleResolver

AcceptHeaderLocaleResolver

SessionLocaleResolver

CookieLocaleResolver

FixedLocaleResolver

CHAPTER 5 ■THE PROCESSING PIPELINE 101
Free download pdf