本節(jié)的項目以為“校驗器”創(chuàng)建的項目Project_0106
為基礎(chǔ)。
所謂國際化,是指根據(jù)瀏覽器HTTP請求頭中Accept-Language
中指定的語言、或者用戶指定的語言(Cookie、session中指定),將web頁面中的一些文本使用該語言展示出來。
Accept-Language
指定的語言進行國際化項目結(jié)構(gòu)如下:
這里只展示改動或者新增的文件。
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
來實現(xiàn)國際化。也可以根據(jù)URL中的指定的Locale進行國際化。
可以參考:
SpringMVC學(xué)習(xí)系列(8) 之 國際化
Spring MVC internationalization example
更多建議: