The internationalization of SPRINGMVC

Source: Internet
Author: User

Regarding the internationalization of SPRINGMVC, http://www.cnblogs.com/liukemng/p/3750117.html This article has already spoken very well. It says there are several ways of internationalization

1: Internationalization of header information based on HTTP (Request.getlocale ())

2: Session-based internationalization (set session Sessionlocaleresolver.locale_session_attribute_name

3: cookie-based internationalization

4: URL-based internationalization

Session-based implementation requires an entry URL or no URL is required to add a lang parameter, is not very convenient, there is no way I want to add, not add is the use of the previous local. Through the practice of URL-based internationalization to solve my problem.

Configuration applicaton.xml;

 <  mvc:interceptors  >  <  bean  class  = "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor"  />  </ mvc:interceptors  >  <  Span style= "color: #800000;" >bean  id  = "Localeresolver"   class  = "Com.sequoiadb.util.DBaasLocaleResolver"  />  

Ways to implement Dbaaslocaleresolver

 Public classDbaaslocaleresolverextendsAcceptheaderlocaleresolver {PrivateLocale mylocale=NULL;  PublicDbaaslocaleresolver () {} PublicLocale Resolvelocale (httpservletrequest request) {if(mylocale==NULL){            returnRequest.getlocale (); }Else{            returnMylocale; }    }    /*** I debug this class does not seem to be used *@paramRequest *@paramResponse *@paramlocale*/     Public voidSetLocale (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Java.util.Locale Locale) {Mylocale=locale; }}

Add internationalization file

Add properties file in src directory, Chinese transcoding to use Native2ascii.exe tool, this is the JDK comes with.

In this case, the above mentioned article's URL-based internationalization code is a bit problematic, because if the null pointer exception is reported without the locale, so I changed the logic, the first time according to the browser to internationalize so use Request.getlocale (), If there is a locale parameter in the URL to return to Mylocale, the structure of this Mylocale object is not very clear to me.

The only bug is that your locale parameter filling errors will also default to using the browser to internationalize, rather than using the last locale.

The internationalization of SPRINGMVC

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.