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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
| private static ResourceBundle apps =
ResourceBundle.getBundle(
"org.i505.validator.myapplicationResources");
public static void printResults(
ValidateBean bean,
ValidatorResults results,
ValidatorResources resources) {
boolean success = true;
Form form = resources.get(Locale.getDefault(), "nameForm");
System.out.println("\n\n 验证 :");
System.out.println(bean);
Iterator propertyNames = results.get();
while (propertyNames.hasNext()) {
String propertyName = (String) propertyNames.next();
Field field = (Field) form.getFieldMap().get(propertyName);
String prettyFieldName = getGBKMsg(apps.getString(
field.getArg0().getKey()));
ValidatorResult result = results.getValidatorResult(propertyName);
Map actionMap = result.getActionMap();
Iterator keys = actionMap.keySet().iterator();
while (keys.hasNext()) {
String actName = (String) keys.next();
ValidatorAction action=resources.getValidatorAction(actName);
System.out.println(
propertyName
+ "["
+ actName
+ "] ("
+ (result.isValid(actName) ? "验证通过" : "验证失败")
+ ")");
if (!result.isValid(actName)) {
success = false;
String message = getGBKMsg(apps.getString(action.getMsg()));
Object[] args = { prettyFieldName };
System.out.println(
"错误信息是 : "
+ MessageFormat.format(message, args));
}
}
}
if (success) {
System.out.println("表单验证通过");
}
else {
System.out.println("表单验证失败");
}
}
public static String getGBKMsg(String msg){
String gbkStr="";
try {
gbkStr=new String(msg.getBytes("iso-8859-1"),"gbk");
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return gbkStr;
}
|