Continue to share with you the data check in struts, before we say the data check in the form of code, and then we'll talk about the XML form.
First, the introduction
1, summary code validation: Tedious, design a lot of repetitive verification logic. For example: non-null validation, numeric validation, email, date, and so on.
2, struts for the common validation, has been encapsulated, that is, to provide a validator, to verify the specified common business logic.
Second, XML validates all the methods in the action:
1. All the validators provided by struts:
1) Path:
Xwork-core-2.3.4.1.jar/com.opensymphony.xwork2.validator.validators/default.xml
2) Validator Code:
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE validators Public "-//apache struts//xwork Validator Definition 1.0//en" "http://struts.apache.org /dtds/xwork-validator-definition-1.0.dtd "> <!--START snippet:validators-default--<validators> < ; Validator Name= "Required" class= "Com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> < Validator name= "requiredstring" class= "Com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/ > <validator name= "int" class= "Com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> < ; Validator name= "Long" class= "Com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/> < Validator Name= "Short" class= "Com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> < Validator Name= "Double" class= "Com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> < Validator Name= "Date "class=" Com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator "/> <validator name=" Expression "class=" Com.opensymphony.xwork2.validator.validators.ExpressionValidator "/> <validator name=" Fieldexpression "class=" Com.opensymphony.xwork2.validator.validators.FieldExpressionValidator "/> < Validator name= "Email" class= "com.opensymphony.xwork2.validator.validators.EmailValidator"/> <validator name = "url" class= "Com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name= "Visitor" class= " Com.opensymphony.xwork2.validator.validators.VisitorFieldValidator "/> <validator name=" Conversion "class=" Com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator "/> <validator name=" Stringlength "class=" Com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator "/> <validator
Name= "regex" class= "Com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name= "Conditionalvisitor" class= " Com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator "/> </validators> <!-- END Snippet:validators-default--
2. How to write XML to define validation rules:
1) XML file name syntax: Actionclassname-validation.xml
Note: This XML needs to be in the same directory as the action currently being validated
Example: Useraction-validation.xml
2) Write XML
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.3//en" "Http://struts.apache.org/dtds/xwo Rk-validator-1.0.3.dtd "> <!--supplemental knowledge *: 0 or more.
: 0 or 1 +: 1 or more-<validators> <!--Verify that each field is in field-<field name= "User.username" > <!--Specifies the authenticator used, requiredstring indicates non-null authentication--<field-validator type= "requiredstring" > <!-- Error message-<message> user name cannot be empty. </message> </field-validator> </field> <field name= "User.pwd" > <!--password Non-free--<field-validator type= "requiredstring" > <message> password cannot be empty. </message> </field-validator> <!--length-<field-validator type= "Stringlengt H "> <param name=" minLength ">6</param> <param name=" MaxLength ">10</param>
; The <message> password must be 6-10 bits. </message> </field-validator> </field> <field name= "User.birthday" > <f Ield-validator type= "Date" > <message> date format is incorrect. </message> </field-validator> </field> <field name= "User.email" > <fiel D-validator type= "Email" > <message> email format is not correct. </message> </field-validator> </field> </validators>
Three, XML validates the method specified in the Action
1, which is approximately the same as the XML validation method: Verifies that the contents of the XML file are unchanged.
2, file name:
1) Syntax: Actionclassname-actionname-validation.xml
2) Example: Useraction-user_register-validation.xml
3) Parsing: Verifying the Register method in useraction