Expert Spring MVC and Web Flow

(Dana P.) #1

FixedLocaleResolver


The simplest implementation is the FixedLocaleResolver. This class allows you to define the
Localein the ApplicationContext, and because the Localechoice is fixed, it’s one size fits all
with this Strategy. This is a useful and easy way to override and ignore any client’s language
choice, for example. Simply define an instance of this class in your ApplicationContextwith
the bean name localeResolver, and the DispatcherServletwill recognize it automatically.
Listing 5-35 contains an example configuration of a FixedLocaleResolver.


Listing 5-35.Sample FixedLocaleResolver


<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">



<bean id="fixedLocaleResolver"
class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>


Using the configuration from Listing 5-35, no matter what the client advertises via its
headers, the framework will force the locale to be en.
You might notice that the defaultLocaleproperty is of type java.util.Locale, yet all we
specified in the above configuration is the string en. Spring will use its LocaleEditorproperty
editor to convert from the string to a full Localeinstance. LocaleEditoris one of the many
property editors that Spring will create and register by default.


■Tip Spring’s use of PropertyEditorsfrom the Java Bean specification is quite extensive. Take the time
to learn what editors Spring provides out of the box (a subset is covered in Chapter 6).


If your applications require providing each user their own unique experience, the
FixedLocaleResolverfalls quite short. With the default class AcceptHeaderLocaleResolver
(while it does provide a personalized experience) it’s impossible for the application to change
its value. There are two other implementations of LocaleResolverthat allow the application to
change the Localefor the user, the CookieLocaleResolverand the SessionLocaleResolver.
Both of these implementations support changing and storing the Localeacross requests.


CHAPTER 5 ■THE PROCESSING PIPELINE 105
Free download pdf