所需的Jar包
其中红色箭头为数据校验使用的主要Jar包,其他为Spring与SpringMVC环境包。
beans.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启自动扫描 -->
<context:component-scan base-package="com.yf"></context:component-scan>
<!-- 启用注解驱动 conversion-service指定Date类型转换器 validator指定校验器 -->
<mvc:annotation-driven conversion-service="conversionService" validator="validator"></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- Date类型转换器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.yf.domain.DateType"></bean>
</list>
</property>
</bean>
<!-- 校验器 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property><!-- 校验器提供者 -->
<property name="validationMessageSource" ref="messageSource"></property><!-- 依赖Properties -->
</bean>
<!-- 校验信息错误配置文件 就是一个储存错误信息的properties文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<array>
<value>classpath:resourceMessage</value><!-- 在类目录下创建resourceMessage.properties文件 -->
</array>
</property>
<property name="fileEncodings" value="utf-8"></property><!-- 指定文件编码 -->
</bean>
</beans>
配置完后再需要校验的对象前加上注解即可
@RequestMapping("login.do")
public ModelAndView login(@Validated User user,BindingResult bindingResult) {
System.out.println("user:"+user);
if(bindingResult.hasErrors()) {
List<ObjectError> error=bindingResult.getAllErrors();
for(ObjectError e:error) {
System.out.println(e.getDefaultMessage());
}
}
return null;
}
public class User {
//@Size(min=3,max=6,message="{UserIdErrorMessage}")
//@Length(min=3,max=6,message="{UserIdErrorMessage}")
//@DecimalMin(value="99",message = "{UserIdErrorMessage}")
//@DecimalMax(value="1000000",message = "{UserIdErrorMessage}")
//@Range(min=99,max=100000,message="id不在99-100000范围内")\
@Max(value=999,message = "id不小于等于999")
private Integer id;
@NotBlank(message = "username为null或去前后空格后长度为0")
private String username;
@Pattern(regexp = "^[0-9]*$",message = "password只能是数字")
@Length(min=6,max=16,message = "password长度只能为6-16字符")
@Size(min=6,max=16,message = "password长度只能为6-16字符")
@NotEmpty(message = "password不能为空!")
private String password;
private Character sex;
// @Max(value=999,message = "price不小于等于999")
// @Min(value=99,message = "price不大于等于99")
// @DecimalMin(value="99",message = "price不大于等于99")
// @DecimalMax(value="1000",message = "price不小于等于1000")
@Range(min=99,max=1000,message="price不在99-1000范围内")
private Double price;
private Boolean state;
private Date birthday;