国际化

欢马劈雪     最近更新时间:2020-08-04 05:37:59

196

本节的项目以为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

展开阅读全文