SpringMVC 国际化-中英文切换
2017-10-19 22:02
429 查看
国际化是为了实现不同地区页面呈现不同的语言问题;
编写程序实现国际化,在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:
效果:
编写程序实现国际化,在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:
效果:
相关文章推荐
- SSH1国际化中英文切换要注意的部份
- Vue国际化处理 vue-i18n 以及项目自动切换中英文
- web,jsp,html网站中英文切换,资源国际化解决方案及代码(其中包含动态切换数据源及URL拦截器).
- 利用Intellij+MAVEN+SpringMVC+URI.js实现页面的中英文切换(国际化)
- Anguarjs实现国际化,中英文切换Demo
- qt 之 国际化(中英文切换)
- python国际化(i18n)和中英文切换
- python国际化(i18n)和中英文切换
- struts2的国际化实现网站整体中英文切换实例代码
- struts国际化实例---登录例子(中英文切换)
- Struts2 关于i18n国际化的问题,可点击链接实现中英文切换
- struts2的国际化(即实现网站整体中英文切换)实例
- Struts2 关于i18n国际化的问题,可点击链接实现中英文切换
- swing界面编程-国际化-中英文切换-简单示例供参考
- struts2的国际化(即实现网站整体中英文切换)实例
- struts2的国际化,中英文切换
- Struts2的国际化,中英文切换
- 【Java篇】springMvc国际化语言切换
- Libgdx之国际化 中英文菜单切换
- Vue国际化处理 vue-i18n 以及项目自动切换中英文