Java Records-SPRINGMVC internationalization configuration

Source: Internet
Author: User
Tags i18n locale

1. Build SPRINGMVC framework, but more elaborate

2.spring-mvc.xml Add the following configuration:

 <!--International Resource configuration, resource file Binder -    <BeanID= "Messagesource"class= "Org.springframework.context.support.ReloadableResourceBundleMessageSource">        <!--internationalized Resource file configuration, specifying properties file storage location -        < Propertyname= "BaseName"value= "Classpath:messages/messages" />        <!--if the corresponding code is not found in the Internationalized resource file, use this code as the name -                       < Propertyname= "Usecodeasdefaultmessage"value= "true" />    </Bean>    <!--Dynamic switching internationalization, internationalization in the session -    <mvc:interceptors>      <!--internationalized operation interceptors must be configured if they are based on (request/session/cookie) -     <Beanclass= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />      </mvc:interceptors>       <BeanID= "Localeresolver"class= "Org.springframework.web.servlet.i18n.SessionLocaleResolver" />    

3. In resources, create a new directory messages, and then create the following two files:

messages_en_us.properties:money=moneymessages_zh_cn.properties:money= Money

4. Background controller Writing

 PackageCom.net.xinfang.controller;ImportJava.util.Locale;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.context.i18n.LocaleContextHolder;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RequestParam;Importorg.springframework.web.servlet.i18n.SessionLocaleResolver; @Controller @requestmapping (Value= "/cte") Public classCntoencontroller {@RequestMapping (value= "/getcte", method ={requestmethod.get}) PublicString Test (HttpServletRequest Request,model Model, @RequestParam (value= "Langtype", defaultvalue= "zh") String langtype) {if(Langtype.equals ("zh") {locale locale=NewLocale ("zh", "CN");             Request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name,locale); }            Else if(Langtype.equals ("en")) {locale locale=NewLocale ("en", "US");            Request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name,locale); }            Else{request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name,localecontexth            Older.getlocale ()); }             return"CTE"; }  }

5.jsp Page Authoring

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD "><% @taglib Prefix= "Spring" uri= "http://www.springframework.org/tags"%>

6. Testing

Java Records-SPRINGMVC internationalization configuration

Related Article

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.