国际化是为了实现不同地区页面呈现不同的语言问题;
编写程序实现国际化,在xml文件中做响应的配置,然后编写.properties文件即可。
实现登录界面的中英文切换:
xml文件配置:
<!-- 开启注解的功能 加载数据验证配置 、配置国际化的资源、Spring mvc 读取国际化资源-->
<mvc:annotation-driven validator="validator" conversion-service="conversion-service"/>
<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="message"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> <!-- 需放在拦截器中 -->
</mvc:interceptors>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<property name="validationMessageSource" ref="messageSource"></property>
</bean>
<body>
<a href="${pageContext.request.contextPath}/login.do?new&locale=zh_CN">中文</a>
<a href="${pageContext.request.contextPath}/login.do?new&locale=en_us">英文</a>
</body>
entity实现类通过注解验证:
public class User {
@Size(min=3,max=50,message="{fullName}")
private String fullName;
@Size(min=3,max=20,message="{userName}")
//@Pattern(regexp="^[a-zA-Z0-9]+$",message="用户姓名之间不能存在空格")
private String userName;
@Size(min=6,max=20,message="{password}")
private String password;
@Pattern(regexp="[A-Za-z0-9._%+-]+@[A-Za-z0-9]+.[A-Za-z]{2,4}",message="{email}")
private String email;
properties:
效果: