5.2 Spring MVC的国际化处理 5.2.2 基于Cookie的国际化实现
CookieLocaleResolver
不是默认的语言区域解析器,需要显式对其进行配置。使用它,Spring MVC
会从Cookie
中获取用户所设置的语言区域,来确定使用哪个语言区域.
实例 基于Cookie的国际化实现
springmvc-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="org.fkit"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/content/" p:suffix=".jsp"/> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basenames="message" p:defaultEncoding="utf-8"> </bean> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/> </beans>
|
加载国际化资源文件
指定国际化资源文件的路径,以及文件的编码。
1 2 3 4 5 6 7
| <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basenames="message" p:defaultEncoding="utf-8"> </bean>
|
国际化拦截器
使用Cookie来实现国际化,必须设置国际化操作拦截器:
1 2 3 4
| <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>
|
创建CookieLocaleResolver
注意,此处将默认配置的localeResolver
换成了CookieLocaleResolver
1 2 3
| <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
|
UserController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| @Controller public class UserController { @RequestMapping(value = "/loginForm") public String loginForm(String request_locale, Model model, HttpServletRequest request, HttpServletResponse response) { System.out.println("request_locale = " + request_locale); if (request_locale != null) { if (request_locale.equals("zh_CN")) { Locale locale = new Locale("zh", "CN"); (new CookieLocaleResolver()).setLocale(request, response, locale); } else if (request_locale.equals("en_US")) { Locale locale = new Locale("en", "US"); (new CookieLocaleResolver()).setLocale(request, response, locale); } else { (new CookieLocaleResolver()).setLocale(request, response, LocaleContextHolder.getLocale()); } } User user = new User(); model.addAttribute("user", user); return "loginForm"; } @PostMapping(value = "/login") public String login(@ModelAttribute User user, Model model, HttpServletRequest request) { if (user.getLoginname() != null && user.getLoginname().equals("xiaoming") && user.getPassword() != null && user.getPassword().equals("123456")) { RequestContext requestContext = new RequestContext(request); String username = requestContext.getMessage("username"); user.setUsername(username); model.addAttribute("user", user); return "success"; } return "error"; } }
|
UserController
的loginForm
方法根据提交的request_locale
参数值,创建CookieLocaleResolver
对象并调用CookieLocaleResolver
对象的setLocale
方法将语言环境设置在Cookie
中,从而进行语言环境切换.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试基于ConversionService的国际化</title> </head> <body> <a href="loginForm?request_locale=zh_CN">中文</a> | <a href="loginForm?request_locale=en_US">英文</a> <br /> <h3> <!-- 显示国际化title文本 --> <spring:message code="title" /> </h3> <form:form modelAttribute="user" method="post" action="login"> <table> <tr> <!-- 显示国际化loginname文本 --> <td><spring:message code="loginname" /></td> <td><form:input path="loginname" /></td> </tr> <tr> <!-- 显示国际化password文本 --> <td><spring:message code="password" /></td> <td><form:input path="password" /></td> </tr> <tr> <td><input type="submit" value="<spring:message code="submit"/>" /></td> </tr> </table> </form:form> </body> </html>
|
loginForm.jsp
中通过<spring:message code="国际化文本" />
的形式来填写相应的国际化文本.
success.jsp
1 2 3 4 5 6 7 8 9 10 11 12
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试基于ConversionService的国际化</title> </head> <body> <spring:message code="welcome" arguments="${requestScope.user.username}" /> </body> </html>
|
success.jsp
页面中通过spring:message
表单来显示格式化文本,该文本的名称为welcome
,并且该welcome
文本带有参数.使用参数可以创建动态的格式化文本。
测试
默认
中文环境