- JSP 和 Servlet
- 00-00、序
- 00-01、相关软件的安装
- 00-02、理解 HTTP
- 00-03、从 JSP 开始
- 00-04、理解 Servlet
- 00-05、过滤器与监听器
- 00-06、使用 velocity 模板引擎
- 00-07、使用数据库连接池
- 00-08、Tomcat 的运行机制
- Spring MVC
- 01-00、Spring 与依赖注入
- 01-01、Spring 与面向切面编程
- 01-02、使用 Spring MVC 构建 Hello World
- 01-03、JdbcTemplate
- 01-04、基于注解的 URL 映射
- 01-05、JSON
- 01-06、校验器
- 01-07、国际化
- 01-08、拦截器
- 01-09、文件上传
- 01-10、转换器与格式化
- Book
- Online Tutorial
- Q 和 A
- Learn More
- Supplement
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
01-07、国际化
本节的项目以为 01-06、校验器 创建的项目 Project_0106
为基础。
所谓国际化,是指根据浏览器 HTTP 请求头中 Accept-Language
中指定的语言、或者用户指定的语言(Cookie、session 中指定), 将 web 页面中的一些文本使用该语言展示出来。
根据浏览器 HTTP 请求头中 Accept-Language
指定的语言进行国际化
源码
这里只展示改动或者新增的文件。
dispatcher-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="me.letiantian.controller" />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<!-- 国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/resource/message00</value>
<value>/WEB-INF/resource/message01</value>
</list>
</property>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
</bean>
</beans>
message00.properties
welcome=hello
person.firstName.notempty=firstName can not be empty
person.secondName.notempty=secondName can not be empty
person.firstName.tooshort=firstName is too short
message00_en_US.properties
welcome=hello
person.firstName.notempty=firstName can not be empty
person.secondName.notempty=secondName can not be empty
person.firstName.tooshort=firstName is too short
message00_zh_CN.properties
welcome=你好
person.firstName.notempty=firstName 不能为空
person.secondName.notempty=secondName 不能为空
person.firstName.tooshort=firstName 太短
*message01.properties**
这三个文件为空。
PersonValidator.java
package me.letiantian.validator;
import me.letiantian.form.Person;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.ValidationUtils;
public class PersonValidator implements Validator{
@Override
public boolean supports(Class<?> type) {
return Person.class.isAssignableFrom(type);
}
@Override
public void validate(Object o, Errors errors) {
Person person = (Person) o;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "person.firstName.notempty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "secondName", "person.secondName.notempty");
if (person.getFirstName().length() < 2) {
errors.rejectValue("firstName", "person.firstName.tooshort");
}
}
}
hello/input.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@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> JSP Page</title>
<style type="text/css">
.error {color: red;}
</style>
</head>
<body>
<h1><spring:message code="welcome" /></h1>
<form:form modelAttribute="person" method="POST" action="${pageContext.request.contextPath}/hello/output">
firstName: <form:input path="firstName" /> <form:errors path="firstName" cssClass="error"/> <br/>
secondName: <form:input path="secondName" /> <form:errors path="secondName" cssClass="error"/> <br/>
<input type="submit" value="提交" />
</form:form>
</body>
</html>
效果
可以参考 Change Mozilla Firefox language settings 修改火狐浏览器的 Accept-Language。 如果没有效果,可以使用 netbeans 重启项目,再查看效果。
其他方式的国际化
上面的程序中 localeResolver
使用的 AcceptHeaderLocaleResolver
(见配置文件 dispatcher-servlet.xml
)。 另外,Spring 还给出 SessionLocaleResolver
、 CookieLocaleResolver
来实现国际化。也可以根据 URL 中的指定的 Locale 进行国际化。
可以参考:
SpringMVC 学习系列(8) 之 国际化
Spring MVC internationalization example
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论