SpringMVC 国际化-中英文切换

国际化是为了实现不同地区页面呈现不同的语言问题;

编写程序实现国际化,在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>


jsp:

  <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:




效果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值