关于国际化:
1.在页面上能够根据浏览器语言设置的情况对文本(不是内容),时间,数值进行本地化处理
2.可以在bean中获取国际化资源文件Locale对应的消息
3.可以通过超链接切换Locale,而不再依赖于浏览器的语言设置情况
解决:
1.使用JSTL的 fmt 标签
2.在bean中注入ResourceBundleMessageSource的示例,使其对应的getMessage方法即可
3.配置LocalResolver 和 LocaleChangeInterceptor
第一步:编写国际化文件properties
i18n.properties 待国际化的资源文件
NotEmpty.employee.lastName=^^LastName姓名不能为空
Email.employee.email=Email地址不正确
Past.employee.birth=Birth不能输入一个将来的时间
i18n.user=Username
i18n.password=Password
i18n_en_US.properties 美国
NotEmpty.employee.lastName=^^LastName姓名不能为空
Email.employee.email=Email地址不正确
Past.employee.birth=Birth不能输入一个将来的时间
i18n.user=Username
i18n.password=Password
i18n_zh_CN.properties 中国
NotEmpty.employee.lastName=^^LastName姓名不能为空
Email.employee.email=Email地址不正确
Past.employee.birth=Birth不能输入一个将来的时间
i18n.user=用户名
i18n.password=密码
第二步:在index.jsp编写连接< a>标签
<a href="i18n">I18N PAGE</a>
第三步:编写响应handler方法
@Autowired
private ResourceBundleMessageSource messageSource;
@RequestMapping("/i18n")
public String testI18n(Locale locale){
String val = messageSource.getMessage("i18n.user",null,locale);
System.out.println(val);
return "i18n";
}
第四步:编写对应的视图(注意用到了fmt标签)
i18n.jsp
<%--
Created by IntelliJ IDEA.
User: 14741
Date: 2019/1/6
Time: 19:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<fmt:message key="i18n.user"></fmt:message>
<br><br>
<a href="i18n2">I18N2 PAGE</a>
<br><br>
<a href="i18n?locale=zh_CH">中文</a>
<br><br>
<a href="i18n?locale=en_US">英文</a>
</body>
</html>
i18n2.jsp
<%--
Created by IntelliJ IDEA.
User: 14741
Date: 2019/1/6
Time: 19:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<fmt:message key="i18n.password"></fmt:message>
<br><br>
<a href="i18n">I18N PAGE</a>
</body>
</html>
第五步:配置springmvc.xml
<!--配置LocaleChanceInterceptor-->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
<!-- <mvc:view-controller path="/i18n" view-name="i18n"/>-->
<mvc:view-controller path="/i18n2" view-name="i18n2"/>
第六步:演示结果