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