Spring 4 Official Document Learning (11) locales of Web MVC framework

Source: Internet
Author: User

Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-localeresolver

Most parts of the spring architecture support internationalization, as spring Web MVC Does. Dispatcherservlet allows you to automate the process of messages-using the client Locale. This is done through the Localeresolver Object.

When a request comes in, Dispatcherservlet looks for a locale resolver, and if one is found, it tries to use it to set the Locale. Using the Requestcontext.getlocale () method, you can always obtain a locale that has been processed by the locale Resolver.

In addition to automatic locale processing, You can also add an interceptor to process the mapping to change the locale in a specific scenario, for example, based on a parameter in the Request.

Locale resolvers and interceptors are all defined in the package and org.springframework.web.servlet.i18n then configured in the context of your application in a regular manner. Here is a selection of the locale resolvers in Spring.

1. Get time zone information

In addition to obtaining the client locale, it is often necessary to know their time zone Information. The Localecontextresolver interface extends the localresolver, allowing resolvers to provide richer Localecontext--which may contain time zone Information.

When possible, the User's timezone can be obtained using Requestcontext.gettimezone (). Date/time Converter and formatter registered in Spring Conversionservice automatically use time zone Information.

2, Acceptheaderlocaleresolver

This locale resolver will check the Accept-language header in the Request. typically, The header field contains the locale of the client operating System.

Note that the locale resolver does not support time zone Information.

3, Cookielocaleresolver

The locale resolver checks for cookies that may exist on the client to determine whether locale or timezone is Specified. If there is, the specified information is Used. By using the properties of the locale resolver, you can specify the name and lifespan of the Cookie. The following example:

<BeanID= "localeresolver"class= "org.springframework.web.servlet.i18n.CookieLocaleResolver">    < propertyname= "cookiename"value= "clientlanguage"/>    <!--in SECONDS. If set to-1, The cookie isn't persisted (deleted when browser shuts Down) -    < propertyname= "cookiemaxage"value= "100000"/></Bean>

Table 22.4. Cookielocaleresolver Properties

property
Default Description

CookieName

ClassName + LOCALE

The name of the cookie

Cookiemaxage

Integer.max_int

The maximum time a cookie is stay persistent on the Client. If-1 is specified, the cookie won't be persisted; IT'LL is available until the client shuts down their browser.

Cookiepath

/

Limits the visibility of the cookie to a certain part of your Site. When Cookiepath is specified, the cookie would be visible to that path and the paths below it.

4, Sessionlocaleresolver

Sessionlocaleresolver allows you to get locale and timezone from the Session. This strategy stores the selected locale settings in the httpsession of the servlet container, relative to Cookielocaleresolver. accordingly, These settings are temporary relative to each session, and are lost when each session ENDS.

Note that this is not directly related to the external session management Mechanism-such as spring Session project. This sessionlocaleresolver will simply evaluate and modify the current httpservletrequest corresponding httpsession Attributes.

5, Localechangeinterceptor

You can enable locales changes by adding localechangeinterceptor to one of the handler mappings. It detects a parameter in the request and modifies the Locale. It will call Localeresolver's setlocale (). The following example shows that calling all "*.view" resources containing the parameter named Sitelanguage will change the locale immediately. For this example, the request "http://www.sf.net/home.view?siteLanguage=nl" will change the site language to Dutch.

<BeanID= "localechangeinterceptor"class= "org.springframework.web.servlet.i18n.LocaleChangeInterceptor">    < propertyname= "paramname"value= "sitelanguage"/></Bean><BeanID= "localeresolver"class= "org.springframework.web.servlet.i18n.CookieLocaleResolver"/><BeanID= "urlmapping"class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    < propertyname= "interceptors">        <List>            <refBean= "localechangeinterceptor"/>        </List>    </ property>    < propertyname= "mappings">        <value>/**/*.view=somecontroller</value>    </ property></Bean>

Spring 4 Official Document Learning (11) locales of Web MVC framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.