java oval xml 校验

阅读更多
oval framework可以使用注解、xml配置校验,注解就不说了,资料比较丰富。xml配置校验资料很少,中文资料没有,只能看英文很少的参考资料和源码了。渐渐的发现,英文资料质量比中文资料要高,以前经历的好几个问题都是通过查找英文资料才解决的,stackoverflow.com就挺好,是国外的csdn,这些都是题外话。直接上xml:

http://oval.sf.net/oval-configuration" xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://oval.sf.net/oval-configuration ../../../../../../../../main/resources/net/sf/oval/configuration/xml/oval-configuration.xsd" >

这个xml里包含了,,,,等几种校验,比较有特点的是可以自定义方法校验validateWithMethod,有了这个就可以将校验扩展的更加强大,比如说可以将数据跟数据库里的数据进行比较校验。还有其他校验,可以参考源码,或问我。

另外,经查源码,发现方法public List validateFieldValue(final Object validatedObject, final Field validatedField,final Object fieldValueToValidate)可以只校验自己想校验的field,不会校验全部field,这个在有些时候还是蛮有用的。

另外附上,使用xml校验的java源码例子:

import java.lang.reflect.Field; import java.util.List; import net.sf.oval.ConstraintViolation; import net.sf.oval.Validator; import net.sf.oval.configuration.xml.XMLConfigurer; public class TrainData { private String xm; private String sfzh; private String qjStart; private String qjEnd; private String rxrq; // @ValidateWithMethod(methodName = "isValid", parameterType = String.class) private String yhcs; private String kxlh; public TrainData(){} public TrainData(String xm, String sfzh, String qjStart, String qjEnd, String rxrq, String yhcs, String kxlh){ this.xm = xm; this.sfzh = sfzh; this.qjStart = qjStart; this.qjEnd = qjEnd; this.rxrq = rxrq; this.yhcs = yhcs; this.kxlh = kxlh; } private boolean isValid(String xm) { //to do 数据库根据身份证号查询出的姓名进行比较 if("1".equals(xm)) return true; return false; } public static void main(String[] args) { try{ TrainData data = new TrainData("刘艳梅","11010619770201304","","","20050101","2","123"); XMLConfigurer x2 = new XMLConfigurer(); x2.fromXML(XMLConfigurationTest.class.getResourceAsStream("TrainDataValidation.xml")); Validator validator = new Validator(x2); // Validator validator = new Validator(); // Field f=TrainData.class.getDeclaredField("xm"); // List violations = validator.validateFieldValue(new TrainData(), f, "12"); List violations = validator.validate(data); for(ConstraintViolation var:violations){ System.out.println(var.getMessage()); } }catch(Exception ex){ } } }


所有参考的网址:

http://oval.sourceforge.net/userguide.html,

http://oval.sourceforge.net/api/index.html

http://sourceforge.net/projects/oval/forums/forum/488110/topic/4709092

你可能感兴趣的:(java oval xml 校验)