SpringMVC 4.0 Internationalization
First, the configuration file of spring:
<!--Change the locale using the parameter passed the day before...?locale=zh_CN --> <mvc:interceptors > <bean class = "org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors > <!--Configure the language conversion level, there is AcceptHeaderLocaleResolver: judged by the accept-language field of the http header; SessionLocaleResolver: Set the language during this session, and use this setting uniformly during the login cycle; CookieLocaleResolver: The previous language setting parameters are kept in the cookie, and are set according to the cookie each time; FixedLocaleResolver: Always use a fixed locale --> <bean id = "localeResolver" class = "org.springframework.web.servlet.i18n.CookieLocaleResolver" ></bean> <!-- When using ResourceBundleMessageSource for internationalization, the bean id must use messageSource--> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames" value="i18n.language"></property> <property name="cacheSeconds" value="-1"/> </bean>
Second, the JSP page
<h1 align="center">${message}</h1> <spring:message code="hello.prompt"></spring:message> ${language}<br/> <a href="/demo/language/test/chinese?locale=zh_CN"> 中文简体</a>| <a href="/demo/language/test/english?locale=en_US">English</a>| <a href="/demo/language/test/chineseTranditional?locale=zh_TW"> 中文繁體</a> <p style="font-size: medium;align-content: center"> <spring:message code="hello.title"></spring:message> </p>
3. Background controller
@Controller @RequestMapping("/language") public class LanguageController { @RequestMapping("/test/{lan}") public String change (athPathVariable String lan, ModelMap modelMap) modelMap.put("language",lan); return "hello"; } }
4. Common mistakes
javax.servlet.jsp.JspTagException: No message found under code 'hello.prompt' for locale 'zh_TW' 试着将<bean id="messageResource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">改为<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">