1 Validation Framework
In Struts Framework, the ActionForm object is used to package the HTML table data (including request) and return to the data that is returned to the user. They must be complete JavaBean, inherit. The Actionform class inside. At the same time, the user can selectively cover two default method validate () and reset ().
Disadvantages of this: 1. Calibration code redundancy and cannot be reused.
2. It is difficult to maintain it. Validation Framework separates the calibration part in the ActionForm and implements the configuration file. The following will be written under the experience in using the Validation. Due to time, it is basically no modification for the original text, and it may be a bit chaos. Please forgive me. 1.1 Composition and Configuration 1.1.1 Composition Package: Commons-Validator.jar, Jakarta-oro.jar,
Profile: Validator-Rules.xml, Validation.xml
1.1.2 Configuration requirements: 1, there is validation.xml, validator-rules.xml file in web-inf / directory
2, there is the following in web-inf / struts-config.xml
plug-in>
1.2 Validation-Rules.xml Validation-Rules.xml defines a set of universal verification rules that will be referenced in Validaion.xml. You can also customize some rules to this file, or you can separate a file separately. For example: Name = "minlength" ClassName = "org.apache.struts.util.strutsvalidator" Method = "ValidateMinLength" Methodparams = "java.lang.object, Org.apache.commons.valiDator.validatorAction, Org.apache.commons.validator.field, Org.apache.struts.Action.Actionerro, Javax.Servlet.http.httpservletRequest " Depends = "Required, INTEGER" Msg = "ErrorS.minLength"> validator> Attributes description Name Specify a rule name to be referenced by other rules Classname Class containing check rules Method This verification rule actually corresponds to the class Methodparams Parameters of the Method method Depends Reference other rules (called before the current rules) MSG The keywords in the resource file, when the verification is not passed, the information in this keyword will be added to the ERRORS collection. JSFunctionName Specify a JavaScript function name "Rg.apache.struts.util.strutsvalidator" defines the following methods: ValidateByte Validatecreditcard Validatedate Validatedouble ValidateEmail Validatefloat ValidateInteger Validatelong Validatemask · Validateminlength ValidateMaxLength ValidateRange ValidateRequired ValidateShort 1.3 Validation.xml Validation.xml Association ActionForm and Validaion-Rules.xml define the verification rules for each attribute in the ActionForm. E.g: constant> global>
var>
var>
field>
form>
formset>
form-validation>
This profile specifies the "Phone" attribute in "Required, Mask, MINLENGTH" to verify "CheckoutForm": "Phone" is not empty, must be greater than or equal to 5 characters, the character must meet $ {Phone } The rule defined.