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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 验证 :{ username =null, age=null} age[required] ( 验证失败 ) 错误信息是 : 必须提供年龄字段! username[required] ( 验证失败 ) 错误信息是 : 必须提供姓名字段! 表单验证失败 验证 :{ username = 龚永生 , age= 很年轻 } age[required] ( 验证通过 ) age[int] ( 验证失败 ) 错误信息是 : 年龄字段必须是整数! username[required] ( 验证通过 ) 表单验证失败 验证 :{ username = 龚永生 , age=28} age[required] ( 验证通过 ) age[int] ( 验证通过 ) username[required] ( 验证通过 ) 表单验证通过 |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |