5.2 Spring MVC的国际化处理
AcceptHeaderLocaleResolver是默认的,也是最容易使用的语言区域解析器。使用它,Spring MVC会读取浏览器的accept-language标题,来确定使用哪个语言区域。AcceptHeaderLocaleResolver可以不用显式配置,当然也可以显式配置。 
示例:基于浏览器请求的国际化实现
首先,准备两个资源文件
message_enUs.properties
第一个资源文件是message_enUs.properties,该文件内容如
| 12
 3
 4
 5
 6
 
 | loginname= Login name:password = Password:
 submit = Submit
 welcome = Welcom {0}
 title = Login Page
 username = administrator
 
 | 
message_zh_CN.properties
第二个资源文件的内容如下:
| 12
 3
 4
 5
 6
 
 | loginname=登录名:password=密码:
 submit=提交
 welcome=欢迎 {0}
 title=登录页面
 username=管理员
 
 | 
将上面两个国际化资源文件保存为UTF-8编码.接下来在JSP页面中通过<spring:message/>标签来输出国际化消息。
| 12
 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
 
 | <%@ 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>测试基于浏览器请求的国际化</title>
 </head>
 <body>
 <!-- 使用message标签来输出国际化信息 -->
 <h3>
 <spring:message code="title" />
 </h3>
 <form:form modelAttribute="user" method="post" action="login">
 <table>
 <tr>
 <td><spring:message code="loginname" /></td>
 <td><form:input path="loginname" /></td>
 </tr>
 <tr>
 <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>
 
 | 
springmvc-config.xml
接下来在Spring MVC配置文件中加载国际化资源文件。
| 12
 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
 
 | <?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:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 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"/>
 <mvc:interceptors>
 
 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
 </mvc:interceptors>
 
 <bean
 id="localeResolver"
 class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/>
 </beans>
 
 | 
国际化配置
国际化配置如下:
| 12
 3
 4
 5
 6
 7
 
 | 
 <bean
 id="messageSource"
 class="org.springframework.context.support.ResourceBundleMessageSource"
 p:basenames="message"
 p:defaultEncoding="UTF-8"/>
 
 | 
指定国际化资源文件路径
其中p:basenames属性用于指定国际化文件的路径,这个路径是相对于src目录的路径,例如这里对应的路径为:src/message_en_US.properties和message_zh_CN.properties,指定国际化路径的时候只需要指定文件的前缀就行类,后面的语言,地区以及文件名后缀不需要指出.也就是对于:
| 12
 
 | message_zh_CN.propertiesmessage_en_US.properties
 
 | 
指定国际化资源文件编码
p:defaultEncoding="UTF-8"这个属性用于指定这些国际化文件使用的编码,默认编码是ISO-8859-1,这样中文将会变成unicode代码,不方便我们阅读修改.所以我这里使用UTF-8编码
这组国际化资源文件,只需要指定message即可
UserController.java
| 12
 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
 
 | @Controllerpublic class UserController {
 @RequestMapping(value = "/loginForm")
 public String loginForm(Model model)
 {
 
 User user = new User();
 model.addAttribute("user", user);
 
 return "loginForm";
 }
 @PostMapping(value = "/login")
 
 public String login(@ModelAttribute User user, Model model, HttpServletRequest request)
 {
 System.out.println(user);
 
 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";
 }
 }
 
 | 
login()方法接收请求,如果验证通过,则使用RequestContext对象的getMessage()方法来获取国际化消息,并跳转到success.jsp页面。
success.jsp
| 12
 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>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>测试基于浏览器请求的国际化</title>
 </head>
 <body>
 <spring:message code="welcome" arguments="${requestScope.user.username}" />
 </body>
 </html>
 
 | 
在success.Jjsp页面中,使用message标签读取资源文件中名为welcome的消息,并设置了一个参数,参数值为user对象的username属性
测试
正确填写登陆名xiaoming,密码123456,
![这里有一张图片]()
将会成功跳转到成功界面如下图所示:
![这里有一张图片]()
测试国际化
设置浏览器语言
点击火狐浏览器右边的菜单按钮,然后选中选项:
![这里有一张图片]()
找到语言,点击右边的选择按钮:
![这里有一张图片]()
把英语(美国)移动到最上面:
![这里有一张图片]()
显示效果
刷新登陆表单,即可看到提示信息变成英文的了:
![这里有一张图片]()
![这里有一张图片]()