SpringMVC Validator、国际化及文件操作
1.验证器
1.1注解及其含义
Bean Validation 中内置的 constraint
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(regex=,flag=) 被注释的元素必须符合指定的正则表达式
Hibernate Validator 附加的 constraint
@NotBlank(message =) 验证字符串非null,且长度必须大于0
@Email 被注释的元素必须是电子邮箱地址
@Length(min=,max=) 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range(min=,max=,message=) 被注释的元素必须在合适的范围内
1.2使用
-
Maven的pom.xml
版本控制
<validation.version>1.1.0.Final</validation.version> <hibernate.version>4.3.0.Final</hibernate.version>
坐标
<!-- 验证器--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>${validation.version}</version> </dependency>
2.国际化
2.1 springmvc.xml配置
<!--单组配置文件
例如:若messges有以下两个文件则指的是这两个文件
message_ch.properties, message_es.properties-->
<!-- 国际化配置 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"></property>
</bean>
<!--或者-->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/resource/message"></property>
</bean>
<!--多组配置文件
其中name要设置为 basenames-->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="classpath:message">
<list>
<value>classpath:message</value>
<value>classpath:reout</value>
</list>
</property>
</bean>
2.2 语言区域选择实现
-
SpringMVC中可以使用语言区域解析器bean选择语言区域
该类的三个实现:
- AcceptHeaderLocaleResolver
依据浏览器http header中内容获取accept-language域,不能调用Locationresolver接口的setLocate()方法
- SessionLocaleResolver
依据本次会话的语言设定选择显示语言区域, 就是从HttpSession作用域中获取用户所设置的语言区域,controller处理需要将具体语言设置在session中
- CookieLocaleResolver
依据Cookie判定语言区域,controller处理需要将具体语言设置在session中
-
使用SessionLocaleResolver
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <property name="defaultLocale" value="zh_CN"></property> </bean>
-
使用AcceptHeaderLocaleResolver和CookieLocaleResolver
使用这两个需配置拦截器,需添加拦截器配置如下
<mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>
总体配置:
spring-mvc.xml
<mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <property name="defaultLocale" value="zh_CN"></property> </bean>
applicationContext.xml
<!-- 国际化配置 --> <bean id