Struts2 provides server-side and client-side input verification
1. Rewrite the validate () method:
When the verification fails, the Action returns the logic view named input, and the error message can be output through <s: fielderror/> in this view. Note that the action should inherit ActionSupport. Use addFieldError ("field name", "error message") in ActionSupport to add the error message to the corresponding field. If there is a struts form tag, an error message is displayed at the corresponding field, validate verifies all the methods in the action.
Index. jsp
<Body> <s: fielderror> </s: fielderror> <! -- Display Error information --> <s: fielderror fieldName = "name" theme = "simple"/> <! -- With style --> <br/> <s: property value = "errors. name [0]"/> <! -- Without style --> <s: debug> </s: debug> <! -- Value stack-> <form action = "$ {pageContext. request. contextPath}/person/manage_save "method =" post "> name: <input name =" username "type =" text "/> <br> mobile phone: <input name = "mobile" type = "text"/> <br> <input type = "submit" value = "submit"/> </form> </body>
:
Struts. xml:
<Action name = "manage _ *" class = "cn. ljf. personAction "method =" {1} "> <result name =" message ">/message. jsp </result> <result name = "input">/index. jsp </result> <! -- Name must be input --> </action>
Action:
Public class PersonAction extends ActionSupport {@ Overridepublic void validate () {if (this. username = null | "". equals (username. trim () this. addFieldError ("username", "user name cannot be blank! "); If (this. mobile = null |" ". equals (mobile. trim () this. addFieldError (" mobile "," the mobile phone number cannot be blank! "); Else {if (! Pattern. compile ("^ 1 [358] \ d {9} $ "). matcher (this. mobile ). matches () this. addFieldError ("mobile", "Incorrect mobile phone number format! ") ;}} Private String username; private String mobile; // getter setter}
To verify the specified method, you need to write a method validateXxx, where xxx is the name of the method to be executed.
For example, validateUpdate ()
If the intermediate data conversion fails (either basic or custom), information will be added to fielderror. If the struts2 form exists, the corresponding fields of the form are also prompted. We can also change the default type conversion information. For example, enter a letter for int data.
Type 2: xml configuration
<! DOCTYPE validators PUBLIC "-// Apache Struts // XWork Validator 1.0.3 //" http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd "> <validators> <field name =" username "> <field-validator type =" requiredstring "> <param name =" trim "> true </param> <message> the user name cannot be blank! </Message> <! -- Error message --> </field-validator> </field> <field name = "mobile"> <field-validator type = "requiredstring"> <message> the mobile phone number cannot be blank. </message> </field-validator> <field-validator type = "regex"> <param name = "expression"> <! [CDATA [^ 1 [358] \ d {9} $]> </param> <message> incorrect mobile phone format </message> </field-validator> </ field> </validators>
Adding client verification is very simple. You can use the struts2 label to generate a form and add the validate = "true" attribute to the form.
<Head> <s: head/> <title> title </title>
The following is a configuration instance | (note the short circuit verification short-circuit settings)
<! -- Verify the root element of the file --> <validators> <! -- Verify the name attribute of Action --> <field name = "name"> <! -- The specified name attribute must meet the required rule --> <field-validator type = "requiredstring" short-circuit = "true"> <param name = "trim"> true </param> <! -- If verification fails, name is output. the international information corresponding to requried --> <message >$ {getText ("name. requried ") }</message> </field-validator> <! -- The specified name attribute must match the Regular expression --> <field-validator type = "regex"> <param name = "expression"> <! [CDATA [(\ w {4, 25})]> </param> <! -- If verification fails, name is output. internationalization information corresponding to regex --> <message >$ {getText ("name. regex ") }</message> </field-validator> </field> <! -- Verify the pass attribute of an Action --> <field name = "pass"> <! -- Specify that the pass attribute must meet the required rule --> <field-validator type = "requiredstring" short-circuit = "true"> <param name = "trim"> true </param> <! -- If verification fails, pass is output. the international information corresponding to requried --> <message >$ {getText ("pass. requried ") }</message> </field-validator> <! -- The specified pass attribute must match the specified Regular expression --> <field-validator type = "regex"> <param name = "expression"> <! [CDATA [(\ w {4, 25})]> </param> <! -- If verification fails, pass is output. the international information corresponding to regex --> <message >$ {getText ("pass. regex ") }</message> </field-validator> </field> <! -- The specified age attribute must be in the specified range --> <field name = "age"> <field-validator type = "int"> <param name = "min"> 1 </ param> <param name = "max"> 150 </param> <! -- If the verification fails, output age. internationalization information corresponding to range --> <message >$ {getText ("age. range ") }</message> </field-validator> </field> <! -- The specified birth attribute must be in the specified range --> <field name = "birth"> <field-validator type = "date"> <! -- When the date string is specified below, the date format of this Locale must be used --> <param name = "min"> 1900-01-01 </param> <param name = "max"> 2050-02-21 </param> <! -- If verification fails, generate birth. internationalization information corresponding to range --> <message >$ {getText ("birth. range ") }</message> </field-validator> </field> </validators>
Built-in Checker:
Annotation-based verification can also be performed.