1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <form-validation> <global> <validator name="int" classname="org.i505.validator.MyTypeValidator" method="validateInt" methodParams="java.lang.Object,org.apache.commons.validator.Field" msg="errors.int"/> <validator name="required" classname="org.i505.validator.MyValidator" method="validateRequired" methodParams="java.lang.Object,org.apache.commons.validator.Field" msg="errors.required"/> </global> <formset> <form name="nameForm"> <field property="username" depends="required"> <arg0 key="nameForm.username.displayname"/> </field> <field property="age" depends="required,int"> <arg0 key="nameForm.age.displayname"/> </field> </form> </formset> </form-validation> |
1 2 3 4 5 6 | # validatoraction 对象的消息 errors.required= 必须提供 {0} 字段! errors.int= {0} 字段必须是整数! # nameForm 输入表单的各个输入数据的显示信息 nameForm.username.displayname= 姓名 nameForm.age.displayname= 年龄 |
1 2 3 4 5 6 7 8 9 10 11 | package org.i505.validator;import org.apache.commons.validator.Field; import org.apache.commons.validator.GenericTypeValidator; import org.apache.commons.validator.ValidatorUtil; public class MyTypeValidator { public static Integer validateInt(Object bean, Field field) { String value = ValidatorUtil.getValueAsString(bean, field.getProperty()); Integer x= GenericTypeValidator.formatInt(value); return x; } } |
1 2 3 4 5 6 7 8 9 10 | package org.i505.validator; import org.apache.commons.validator.Field; import org.apache.commons.validator.GenericValidator; import org.apache.commons.validator.ValidatorUtil; public class MyValidator { public static boolean validateRequired(Object bean, Field field) { String value = ValidatorUtil.getValueAsString(bean, field.getProperty()); return !GenericValidator.isBlankOrNull(value); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class ValidateBean extends Object { String username;String age; public void setUsername (String username) { this. username = username; } public String getUsername () { return this.username; } public void setAge (String age) { this.age = age; } public String getAge () { return this.age; } public String toString() { return "{ username =" + this.username + ", age=" + this.age + "}"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public static void main(String[] args) throws IOException, ValidatorException { InputStream in = null; try { ValidatorResources resources = new ValidatorResources(); in = ValidateExample.class.getResourceAsStream("myvalidator-example.xml"); ValidatorResourcesInitializer.initialize(resources, in); ValidateBean bean = new ValidateBean(); Validator validator = new Validator(resources, "nameForm"); validator.addResource(Validator.BEAN_KEY, bean); ValidatorResults results = null; results = validator.validate(); printResults(bean, results, resources); bean.setUsername("龚永生"); bean.setAge("很年轻"); results = validator.validate(); printResults(bean, results, resources); bean.setAge("28"); results = validator.validate(); printResults(bean, results, resources); } finally { if (in != null) { in.close(); } } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |