SpringMVC数据校验
Spring的Validation校验框架
spring自带Validation校验框架,其中重要的接口和类有:
- Validator接口,有两个方法supports()和validate()
- Errors,存放错误信息的接口
- ValidationUtils,校验工具类
- LocalValidatorFactoryBean,该类实现了Spring的Validator接口,也实现了JSR 303的Validator接口
JSR 303校验
JSR 303是java为Bean数据合法性校验所提供的一个标准规范,叫做Bean Validation。Bean Validation为JavaBean验证定义了相应的元数据类型和API。在应用程序中,通过在Bean属性上标注类似@NotNull、@Max等标准的注解指定校验规则,并通过标注的验证接口对Bean进行验证。Bean Validation是一个可以运行时数据验证框架,在验证之后验证的错误信息会被立即返回。
需要引入的jar包:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>6.0.5.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.1.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
网友评论