标签:
1 //新建一个validators.xml在src根资源下,会覆盖default.xml的validators,所以你懂得
//接着,若使用声明式校验,则要把配置文件xxxAction-validation.xml放入与Action类同级目录下
//看default.xml的源码,找一个validator的类看看里面的写法,发现继承此类即可
2 public class MyValidators extends FieldValidatorSupport {
3
4 //object表示校验的Action类
5 public void validate(Object object) throws ValidationException {
6 // TODO Auto-generated method stub
7 //拿到字段名
8 String fieldName = getFieldName();
9
10 String value = (String)this.getFieldValue(fieldName, object);
13 // if (value == null) {
14 // addFieldError(fieldName, object);
15 // }
16 //若值不为空那么业务逻辑才能继续
17 if(StringUtils.isNotEmpty(value)) {
18 // this.addFieldError(value, object);
19
20 //若密码强度为弱,则校验不通过
21 if(!validatePass(value)) {
//这里不用写错误提示,因为错误消息是在xml的配置文件中指定的,声明式验证在message写错误信息的
22 this.addFieldError(fieldName, object);
23 }
24 }
25 }
26
27 //以下是校验密码强度的
28 private static final String GROUP1 = "abcdefghijklmnopqrstuvwxyz";
29 private static final String GROUP2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
30 private static final String GROUP3 = "0123456789";
31 protected boolean validatePass(String password) {
32 boolean ok1 = false;//有一个小写字母就为true
33 boolean ok2 = false;//有一个大写字母就为true
34 boolean ok3 = false;//有一个数字就为true
35 int length = password.length();
36 for(int i=0;i<length;i++){
37 if(ok1&&ok2&&ok3)
38 break;
39 String character = password.substring(i,i+1);
40 if(GROUP1.contains(character)){
41 ok1 = true;
42 continue;
43 }
44 if(GROUP2.contains(character)){
45 ok2 = true;
46 continue;
47 }
48 if(GROUP3.contains(character)){
49 ok3 = true;
50 continue;
51 }
52 }
53 return ok1&&ok2&&ok3;
54 }
55 }
标签:
原文地址:http://www.cnblogs.com/freed0m/p/4784071.html