一) Validator接口
Spring的Validation功能设计很简单。使用Validator功能即可:
public class Person {
private String name;
private int age;
// the usual getters and setters...
}
public class PersonValidator implements Validator {
/**
* This Validator validates just Person instances
*/
public boolean supports(Class clazz) {
return Person.class.equals(clazz);
}
public void validate(Object obj, Errors e) {
ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
Person p = (Person) obj;
if (p.getAge() < 0) {
e.rejectValue("age", "negativevalue");
} else if (p.getAge() > 110) {
e.rejectValue("age", "too.darn.old");
}
}
}
Spring的Validate策略就是一个类(Person)对应一个校验类(PersonValidator)。至于校验类的绑定,可以代码中显式的调用,也可以是采用annotation。个人比较推荐用annotation的方法。而如果在一个类的校验中需要用到另一个类的校验,我们可以这样写:
public class CustomerValidator implements Validator {
private final Validator addressValidator;
public CustomerValidator(Validator addressValidator) {
if (addressValidator == null) {
throw new IllegalArgumentException(
"The supplied [Validator] is required and must not be null.");
}
if (!addressValidator.supports(Address.class)) {
throw new IllegalArgumentException(
"The supplied [Validator] must support the validation of [Address] instances.");
}
this.addressValidator = addressValidator;
}
/**
* This Validator validates Customer instances, and any subclasses of Customer too
*/
public boolean supports(Class clazz) {
return Customer.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "field.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "field.required");
Customer customer = (Customer) target;
try {
errors.pushNestedPath("address");
ValidationUtils.invokeValidator(this.addressValidator,customer.getAddress(), errors);
} finally {
errors.popNestedPath();
}
}
}
在CustomerValidator 中就用到了addressValidator。另外对于Errors类,可在Spring MVC模块中结合使用。具体使用方式我们在Spring MVC模块的介绍中再详细介绍。
分享到:
相关推荐
spring-modules-validation-0.6.jar
odelay.zip,提供延迟执行操作的灵长类动物延迟反应
java运行依赖jar包
java运行依赖jar包
非常不错的数据校验jar,与spring的无缝接入,是java pojo对象校验的好框架。
java运行依赖jar包
java运行依赖jar包
lib内容: 1\spring-aop-4.3.0.RELEASE.jar 2\spring-aspects-4.3.0.RELEASE.jar 3\spring-beans-4.3.0.RELEASE.jar 4\spring-bridge-2.3.0-b10.jar 5\spring-context-4.3.0....21\validation-api-1.1.0.Final.jar
'springboot-mybatis-redis','springboot-mybatis-redis-annotation','springboot-properties','springboot-restful','springboot-validation-over-json','springboot-webflux','spring-data-elasticsearch-crud','...
spring注解-validation所用到的jar包
javax.validation javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi ...
资源包中,包含: spring.jar spring-aspects.jar spring-mock.jar ...org\springframework\validation org\springframework\web org\springframework\orm org\springframework\jdbc .....
常用jar包
可以找到使用 Spring ApplicationContext 特 性时所需的全部类, JDNI 所需的全部类, UI 方面的用来与模板 (Templating) 引擎如 Velocity、 FreeMarker、JasperReports 集成的类,以及校验 Validation 方面的相关...
资源包中,包含: spring.jar spring-aspects.jar spring-mock.jar ...org\springframework\validation org\springframework\web org\springframework\orm org\springframework\jdbc .....
主要介绍了Spring-Validation 后端数据校验的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
aop,beans,cache,context,core,dao,ejb,instument,jca,jdbc,jms,jmx,jndi,mail,metadate,mock,orm,remoting,scheduling,scripting,stereotype,test,transcation,ui,util,validation,web 以上数十子模块源码全部为...
Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...
java运行依赖jar包
java运行依赖jar包