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
- <Bean id="localeresolver" class= "Org.springframewrok.web.servlet
- . i18n. Sessionlocaleresolver ">
- <property name= "defaultlocale" value="en"/>
- </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
- <Bean id="localeresolver" class= "Org.springframework.web.servlet
- . 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
- <Bean id="localeresolver" class= "Org.springframework.web.servlet
- . i18n. Cookielocaleresolver ">
- <property name= "cookiename" value="language"/>
- <property name= "cookiemaxage" value="3600"/>
- <property name= "defaultlocale" value="en"/>
- </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.
4.FixedLocaleResolver
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
- <beans ... >
- ...
- <Bean id="Localechangeinterceptor"
- class="Org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
- <property name= "paramname" value="language"/>
- </Bean>
- <Bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="interceptors">
- <list>
- ...
- <ref bean="Localechangeinterceptor"/>
- </list>
- </Property>
- </Bean>
- <Bean class= "Org.springframework.web.servlet.mvc.support
- . Controllerclassnamehandlermapping ">
- <property name="interceptors">
- <list>
- ...
- <ref bean="Localechangeinterceptor"/>
- </list>
- </Property>
- </Bean>
- <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
- http://localhost:8080/court/welcome.htm?language=en_us
- 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
- Localecontextholder.setlocalecontext (Buildlocalecontext (Request), this . threadcontextinheritable);
The Buildlocalcontext code in Dispatchservlet is as follows:
[Java]View Plaincopy
- Protected Localecontext Buildlocalecontext ( final httpservletrequest request) {
- return New Localecontext () {
- Public Locale GetLocale () {
- return localeresolver. Resolvelocale (Request);
- }
- @Override
- Public String toString () {
- return GetLocale (). toString ();
- }
- };
- }
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
- 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
- Public static Locale GetLocale (HttpServletRequest request) {
- Localeresolver localeresolver = getlocaleresolver (request);
- if (localeresolver! = null) {
- return Localeresolver.resolvelocale (Request);
- }
- else {
- return Request.getlocale ();
- }
- }
Localresolver the code in the Dispatcherservlet Doservice method, save the Localresolver to the request property as follows:
[Java]View Plaincopy
- Request.setattribute (Locale_resolver_attribute, this.localeresolver)
Spring MVC localeresolver (parsing the user area)