When using struts2's validation.xml for verification, you need to pay attention to the following aspects:
I. First of all, you should pay attention to the name of validation.xml, and you must add "-validation.xml" to the file name of the Action class name. Enter LoginAction-validation.xml.
The II.LoginAction-validation.xml file must be placed in the same directory as the Action class.
The type attribute of the <field-validator type=""> tag in the III.validation.xml file must be the same as the data type of the attribute defined in VO.
IV. In the <action> tag in the struts.xml file, <result name="input"> and <result name="success"> must be included, otherwise the page cannot be found (404).
V. An example validation.xml code:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"><validators> <field name="user.strUsername"> <!-- Verification string cannot be empty --> <field-validator type="requiredstring"> <!-- Delete spaces --> <param name="trim">true</param> <!-- Error message --> <message>User name cannot be empty</message> </field-validator> <!-- Verify string length--> <field-validator type="stringlength"> <param name="minLength">2</param> <param name="maxLength">20</param> <message>User name should be between 2 and 18 characters</message> </field-validator> </field> <field name="user.strPassword"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>Passage cannot be empty</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">18</param> <message>Password length should be between 6 and 18 characters</message> </field-validator> </field> <!--<field name="user.age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>Age should be between 1 and 150</message> </field-validator> </field> Verify the string of date type<field name="user.birthday"> <field-validator type="date"> <param name="min">1900-01-01</param> <param name="max">2008-10-16</param> <message>Date of birth should be between 1900-01-01 and 2008-10-16</message> </field-validator> </field> --></validators>
Summarize
The above is all the content of this article about the code parsing of struts2 validation.xml verification rules, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!