Spring MVC localeresolver (parsing the user area)

Source: Internet
Author: User
Tags i18n locale

In order for a Web application to support internationalization, you must identify the preferred region for each user and display content based on that region.

In a spring MVC application, the user's region is identified by a zone parser, which must implement the Localeresolver interface. Spring MVC provides several localeresolver implementations that allow you to parse a region according to different criteria. In addition, you can implement this interface to create your own zone parser.

To define a zone resolver, you simply register a localeresolver type Bean in the context of the Web application. You must set the bean name of the zone parser to localeresolver so that the Dispatcherservlet can detect it automatically. Please note that only one zone resolver can be registered per Dispatcherservlet.

1. The header parsing area by HTTP request

The default zone parser used by spring is acceptheaderlocaleresolver. It parses the zone by verifying the accept-language header of the HTTP request. This header is set by the user's Web browser based on the locale of the underlying operating system. Note that the zone parser cannot change the user's zone because it cannot modify the locale of the user's operating system.

2. Resolving zones by Session properties

Another way to parse a region is through sessionlocaleresolver. It parses the zone by verifying the attributes that are preset in the user's session. If the Session property

Does not exist, it determines the default zone based on the Accept-language HTTP header.

XML code
    1. <Bean id="localeresolver" class= "Org.springframewrok.web.servlet
    2. . i18n. Sessionlocaleresolver ">
    3. <property name= "defaultlocale" value="en"/>
    4. </Bean>

If the session attribute does not exist, you can set the Defaultlocale property for the parser. Note that by modifying the session properties that save the zone, this area

The parser can change the user's locale settings.

3. Resolve the area by cookie

You can also verify the cookie in the user's browser and use Cookielocaleresolver to parse the area. If the cookie does not exist, it determines the default zone based on the Accept-language HTTP header.

XML code
    1. <Bean id="localeresolver" class= "Org.springframework.web.servlet
    2. . i18n. Cookielocaleresolver "/>

The cookie used by this zone parser can be customized using the CookieName and Cookiemaxage properties. The Cookiemaxage property indicates how many seconds the cookie should last, and 1 means that the cookie expires after the browser is closed.

XML code
  1. <Bean id="localeresolver" class= "Org.springframework.web.servlet
  2. . i18n. Cookielocaleresolver ">
  3. <property name= "cookiename" value="language"/>
  4. <property name= "cookiemaxage" value="3600"/>
  5. <property name= "defaultlocale" value="en"/>
  6. </Bean>

If the cookie does not exist in the user's browser, you can also set the Defaultlocale property for the parser. By modifying the cookie that holds the region, the region parser can change the user's area.


Always use a fixed local, changing local is not supported.

5. Modify the user's area

In addition to explicitly calling Localeresolver.setlocale () to modify the user's zone, you can also apply the Localechangeinterceptor interceptor to the handler map, which discovers the special parameters that appear in the current HTTP request. The parameter names can be customized by the ParamName property of the Interceptor. If this parameter appears in the current request, the interceptor will change the user's area based on the parameter value.

XML code
  1. <beans ... >
  2. ...
  3. <Bean id="Localechangeinterceptor"
  4. class="Org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  5. <property name= "paramname" value="language"/>
  6. </Bean>
  7. <Bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  8. <property name="interceptors">
  9. <list>
  10. ...
  11. <ref bean="Localechangeinterceptor"/>
  12. </list>
  13. </Property>
  14. </Bean>
  15. <Bean class= "Org.springframework.web.servlet.mvc.support
  16. . Controllerclassnamehandlermapping ">
  17. <property name="interceptors">
  18. <list>
  19. ...
  20. <ref bean="Localechangeinterceptor"/>
  21. </list>
  22. </Property>
  23. </Bean>
  24. <beans>

Localechangeinterceptor can only map reconnaissance parameters for those handlers that have it enabled. Therefore, if more than one handler mapping is configured in the context of the Web application, the interceptor must be registered in all handler mappings so that its locale can be changed in any URL.

Now, with the language parameter, you can modify the user's region with any URL. For example, the following two URLs change the user's regional language to American

English and German.

Java code
    1. http://localhost:8080/court/welcome.htm?language=en_us
    2. http://localhost:8080/court/welcome.htm?language=de
6. When using spring MVC, how does the controller get the requested Local

Dispatchservlet will set response in ProcessRequest (HttpServletRequest request, HttpServletResponse Localecontext) method, Associates the Localcontext with the current thread. The code is as follows:

[Java]View Plaincopy
    1. Localecontextholder.setlocalecontext (Buildlocalecontext (Request), this . threadcontextinheritable);

The Buildlocalcontext code in Dispatchservlet is as follows:

[Java]View Plaincopy
  1. Protected Localecontext Buildlocalecontext ( final httpservletrequest request) {
  2. return New Localecontext () {
  3. Public Locale GetLocale () {
  4. return localeresolver. Resolvelocale (Request);
  5. }
  6. @Override
  7. Public String toString () {
  8. return GetLocale (). toString ();
  9. }
  10. };
  11. }

Here The local is resolved by Localresolver, Localresolver is configured from the spring configuration file Localresolver, the default is "Acceptheaderlocaleresolver".

If you want to get the local of the current request in the controller, the code can be written as follows:

[Java]View Plaincopy
    1. Locale locale = Localecontextholder.getlocale ();

Or you can use the Requestcontextutils class method in spring getlocal to get the localresolver saved in the request and use Localresolver parsing to get the local. The code is as follows:

[Java]View Plaincopy
  1. Public static Locale GetLocale (HttpServletRequest request) {
  2. Localeresolver localeresolver = getlocaleresolver (request);
  3. if (localeresolver! = null) {
  4. return Localeresolver.resolvelocale (Request);
  5. }
  6. else {
  7. return Request.getlocale ();
  8. }
  9. }

Localresolver the code in the Dispatcherservlet Doservice method, save the Localresolver to the request property as follows:

[Java]View Plaincopy
    1. Request.setattribute (Locale_resolver_attribute, this.localeresolver)

Spring MVC localeresolver (parsing the user area)

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.