</interceptors>
<beans:bean
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application"
p:fallbackToSystemLocale="false"/>
<beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
id="localeResolver" p:cookieName="locale"/>
</beans:beans>
In Listing 17-13, the differences from the previous version are highlighted in bold. First, the p
namespace is added, and the resource definition is revised to reflect the new folder structure as
presented in Table 17-3. The <resources> tag belongs to the mvc namespace and was introduced in
Spring 3. This tag defines the locations of the static resource files, which enable Spring MVC to handle
the files within those folders efficiently. Within the tag, the location attribute defines the folders for the
static resources. The first path, /, indicates the root folder for the web application, which is
/src/main/webapp, while the second path, classpath:/META-INF/web-resources/, indicates the resource
files for the included library. It will be useful if you include the Spring JavaScript module, which includes
the supporting resource files within the /META-INF/web-resources folder. The mapping attribute defines
the URL for mapping to static resources; as an example, for the URL
http://localhost:8080/ch17/resources/styles/standard.css, Spring MVC will retrieve the file
standard.css from the folder /src/main/webapp/styles. The <default-servlet-handler/> tag enables
the mapping of the DispatcherServlet to the web application's root context URL, while still allowing
static resource requests to be handled by the container's default servlet.
Second, a Spring MVC interceptor with class LocaleChangeInterceptor is defined, which intercepts
all the requests to the DispatcherServlet. The interceptor supports locale switching with a configurable
request parameter. From the interceptor configuration, the URL param with the name lang is defined for
changing the locale for the application.
Then, a bean with class ReloadableResourceBundleMessageSource is defined. As explained in Chapter
5, the ReloadableResourceBundleMessageSource class implements the MessageSource interface, which
loads the messages from the defined files (in this case, it's the messages*.properties and
application*.properties in the /WEB-INF/i18n folder) in supporting i18n. Note the property
fallbackToSystemLocale. This property instructs Spring MVC whether to fall back to the locale of the
system that the application is running on when a special resource bundle for the client locale isn't
found.
Finally, a bean with the CookieLocaleResolver class is defined. This class supports the storage and
retrieval of locale settings from the user browser's cookie.
Modify the Contact List View for i18n Support
Now we can change the JSPX page to display i18n messages. Listing 17-14 shows the revised contact list
view (list.jspx).
Listing 17-14. Revised Contact List View for i18n
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:joda="http://www.joda.org/joda/time/tags"
xmlns:spring="http://www.springframework.org/tags"
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home