<H1> preface <H1> getting started Java code
Demoentity de = new demoentity (); // demoentity is a javabean. // General cloudbindingresult result = validatorutils. validateentity (de, "str", notemptyrule. class); system. out. println (result. getfielderrormessage ("str"); // custom verification information result = validatorutils. validateentity (de, "str", new notemptyrule ("here is an example of specifying verification information! "); System. out. println (result. getfielderrormessage ("str"); // verify both attributes result = validatorutils. validateentity (de, "num, date", notnullrule. class); system. out. println (result. getfielderrormessage ("num") + "|" + result. getfielderrormessage ("date"); // use multiple verification rules result = validatorutils. validateentity (de, "str", notemptyrule. class, singleemailrule. class); system. out. println (result. getfielderrormessa Ge ("str"); // check the printed verification information! // Multiple attributes use multiple rules at the same time. result = validatorutils. validateentity (de, "str, col", notemptyrule. class, singleemailrule. class); system. out. println (result. getfielderrormessage ("str") + "|" + result. getfielderrormessage ("col "));
<Br> the running result is:
Java code
Cannot be blank! Here is an example of specifying verification information! Cannot be blank! | Cannot be blank! Cannot be blank !; Incorrect email format! Cannot be blank !; Incorrect email format! | Cannot be blank !; Incorrect email format!
<Br> 3. Note that sometimes, we need to verify whether a single string or number meets a certain rule. At this time, such a value is not a value in the javabean, in this case, the validatevalue method in validatorutils solves such separate verification and can directly verify the given content. Example:
Java code
// Note that the return value of the following method is of the string type. System. out. println (validatorutils. validatevalue ("hello", notemptyrule. class, singleemail. class); system. out. println (validatorutils. validatevalue ("", notemptyrule. class, singleemail. class); system. out. println (validatorutils. validatevalue (null, notemptyrule. class, singleemail. class); system. out. println (validatorutils. validatevalue ("hello", notemptyrule. class ));
<Br> the running result is as follows:
Java code
Incorrect email format! Cannot be blank !; Incorrect email format! Cannot be blank !; Incorrect email format!
<H1> Custom Rules Java code
Package com. cloudframework. validation. rules;/*** API used to verify the rule. All specific verification rules are implemented from this interface. ** @ Author taofucheng **/public interface validaterule {/*** verifies the logic of specific rules. ** @ Param value * indicates the specific verification content. * @ Return true: verification passed; false: Verification Failed */public boolean validate (object value);/*** get the error message that the verification failed. If the user-specified authentication information is null, the default error message is returned. ** @ Return returns the verification error message */public string getvalidateinfo ();}
<Br> for specific implementation and rule use examples, refer to the existing three rule classes (notemptyrule, notnullrule, singleemailrule ).
<H1> Design The relationship between the design idea and each other has been described in detail. I will not go into details here.
<H1> postscript <H2> <br> extensible as annotation <H2> supplement