Validation framework usage example
Sample code:
@Validation(mode = Validation.MODE.FULL) public class UserBase { @VRequried(msg = "{0}不能为空") @VLength(min = 3, max = 16, msg = "{0}长度必须在3到16之间") @VField(label = "用户名称") private String username; @VRequried @VLength(max = 32) private String password; @VRequried @VCompare(cond = VCompare.Cond.EQ, with = "password") private String repassword; @VModel @VField(name = "ext") private UserExt userExt; // // 此处省略了Get/Set方法 // } public class UserExt { @VLength(max = 10) private String sex; @VRequried @VNumeric(min = 18, max = 30) private int age; @VRequried @VEmail private String email; // // 此处省略了Get/Set方法 // } public static void main(String[] args) throws Exception { YMP.get().init(); try { Map<String, Object> _params = new HashMap<String, Object>(); _params.put("username", "lz"); _params.put("password", 1233); _params.put("repassword", "12333"); _params.put("ext.age", "17"); _params.put("ext.email", "@163.com"); Map<String, ValidateResult> _results = Validations.get().validate(UserBase.class, _params); // for (Map.Entry<String, ValidateResult> _entry : _results.entrySet()) { System.out.println(_entry.getValue().getMsg()); } } finally { YMP.get().destroy(); } }
Execution result:
username : 用户名称长度必须在3到16之间 repassword : repassword must be eq password. ext.age : ext.age numeric must be between 30 and 18. ext.email : ext.email not a valid email address.
Function annotation description:
@Validation
: Verification mode configuration, The default is NORMAL;
- NORMAL - short-circuit verification, that is, if the verification fails, the verification result will be returned;
- FULL - all verification results will be returned after all verification of the target object attributes;
@VField
: Customize the member or method parameter name to be verified;
- name: Customize the parameter name , the upper and lower parameters are separated by '.' during nested verification;
- label: Custom parameter label name, if @VField is used nested, the function will not be available;
@VModel
: Declare whether the target object is a JavaBean object, and object nested verification will be performed;