代码拉取完成,页面将自动刷新
首先按如下说明,你可以自定义一个新注解叫PersonName:
public @interface PersonName {
public String name() default "";
public int errorCode() default 0;// 错误返回码,随便写不写都可以,自己要用就写
public String errorMsg() default "姓名验证无法通过";// 错误描述,同上
public String mapClazz() default "com.beanverify.test.verifyinterface.impl.VerifyPersonName";// 必要的方法,非可选,名称需要和mapClazz保持一致,通过这个方法将PersonName这个注解和VerfiyPersonName这个执行类映射起来,以实现验证行为。
}
VerifyPersonName类实现接口 AnnotationVerifier 即可,接口中只有一个doVerify(Object paramValue, String paramName, Anno annotation)方法需要实现。其中 paramValue是被注解字段的属性值,以如下Person类中的age举例,假设我们后面给age赋值为1,那么这个paramValue的值就是1。paramName当然就是age本身的名字。annotation就是@Number这个注解。因此,很显然,当你自己实现这个age头上的@Number注解的时候,@Number对应的判断方法的逻辑内部,你首先要判断的就是这个paramValue是否是一个数字。然后才是其他判断,比如你设定了大小限制,不能为负数之类的,这些实现细节,你可以参考内置验证方法的逻辑代码。
下面是如何使用,你可以定义类似这样一个bean:Person
public class Person {
@Null(errorMsg = "姓名不能为空",errorCode = 1)
@PersonName(name="GoodName")
private String name;
@Number(maxSize=1)
private String age;
@Email
private String mailAdress;
@Ip
private String myIp;
@Date(format="yyyyMMddHHmmss")
private String today;
@Null
private Person person;
Person person = new Person();
person.setName("GoodName");
person.setAge("1");
person.setMyIp("127.0.0.1");
person.setMailAdress("34897@qq.com");
person.setToday("2013-05-06 14:00:00");
person.setPerson(null);
开始做验证:
VerifyResult result = MyBeanChecker.doCheck(person);
Assert.assertTrue(result.isSuccess());//判断结果
OK.完毕!
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型