Spring Bean校验器设计

来源:互联网 发布:软件培训招生要求 编辑:程序博客网 时间:2024/04/28 02:36

Bean:

 

package Bean.Validator;

public class Person {
  
private String name;
  
private int age;
  
public Person(String name,int age){
      
this.name=name;
      
this.age=age;
  }

  
public Person(){
      
  }

public int getAge() {
    
return age;
}

public void setAge(int age) {
    
this.age = age;
}

public String getName() {
    
return name;
}

public void setName(String name) {
    
this.name = name;
}

}

 配置文件:主要配置资源文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  
<property name="basenames">
    
<list>
      
<value>ApplicationResources</value>
    
</list>
  
</property>
</bean>
 
</beans>

校验器:

 

package Bean.Validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class PersonValidator implements Validator {

    
public boolean supports(Class arg0) {
        
        
return Person.class.equals(arg0);
    }


    
public void validate(Object obj, Errors e) {
        
//拒绝name属性为空
        ValidationUtils.rejectIfEmpty(e, "name""name.empty");
        Person p
=(Person)obj;
        
if(p.getAge()<0){
            e.rejectValue(
"age""negativevalue");
        }
else if(p.getAge()>100){
            e.rejectValue(
"age","tooold");
        }


    }


}

 

测试代码:

getMessage()需要一个数据作为参数,此处可以设置一个空数组即可

package Bean.Validator;

import java.util.List;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;

public class Test {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
          String path
=new Test().getClass().getResource("/").getPath();
            String realpath
=path.substring(1, path.length());
            ApplicationContext context
=new FileSystemXmlApplicationContext(realpath+"/validator.xml");
        Person p
=new Person();
        p.setName(
null);
        p.setAge(
200);
        Errors errors
=new BindException(p,"person");
        
//创建校验器
        Validator personValidator=new PersonValidator();
        
//校验
        personValidator.validate(p, errors);
        
//打印错误数量
        System.out.println(errors.getErrorCount());
        List errorList
=errors.getAllErrors();
        
for(int i=0;i<errorList.size();i++){
            ObjectError oe
=(ObjectError)errorList.get(i);
            Object b[]
={};
            System.out.println(context.getMessage(oe.getCode(), b,Locale.getDefault()));
        }


    }


}


运行结果:

2
Person实例的name属性不能为空
Person实例的age不能大于100