Use the Struts2 validator to verify User Registration Information

Source: Internet
Author: User
Tags valid email address

Verification Framework-based input verification

1. Create a struts2 project testValidators. Struts2 Initial Experience: http://www.cnblogs.com/likailan/p/3280820.html

Registration page, register. jsp:

Use <% @ taglib uri = "/struts-tags" prefix = "s" %> to load the struts2 tag library.

Use the <s: fielderror/> label to output the corresponding field error information. The fieldName attribute is the name of the error field.

Registered Email: Password: Repeated password: Phone:

 

Registered successfully

 

The fields to be verified must provide the corresponding fields and the corresponding get and set methods in the class.

     RegisterAction                                     String execute()                                           .email =                                 .password =                                 .repassword =                                 .phone =   }

 

Add the result tag whose name is input. When verification fails, struts2 automatically transfers the page to the page named "input ".

                                 success.jsp                        register.jsp            

 

Ii. Compile the validator configuration file

1. Naming rules: action class name-validation. xml.
2. one action corresponds to multiple logical processing methods: Specify the way to verify a specific method: action class name-name attribute value-validation. xml. (name attribute name: name attribute value of the corresponding action tag in the strtus configuration file)
3. storage location of the configuration file: Put it in the same folder as the Action.
4. verification rules: first load the action class name-validation. xml, and then load the action class name-name attribute name-validation. xml file.

Checker configuration file, RegisterAction-register-validation.xml:

The mailbox cannot be blank. Incorrect password cannot be blank. 6 password length cannot be less than 6 characters. The two passwords are different from the phone number format incorrect.

The above is written as the field validation style: first specify the validation attribute: Who will check and who will verify it!

The following is a non-field validation style: first specify the validator: Who verifies it and who verifies it!

Prompt message after verification of the parameter values of the fields to be verified fails

Struts2 provides a large number of built-in validators: you can find the following configuration file under com. opensymphony. xwork2.validator. validators of the xwork-core-2.1.6.jar: default. xml. It lists all built-in validators.

: Required. The field value cannot be null.

: Required string validator. The field value cannot be null and the length must be greater than 0.

Property --

: Field Length checker. The value of fidle must be within the specified range. Otherwise, the verification fails.

Attribute -- minLength: specifies the minimum length. MaxLength: specify a large length. Trim: whether to remove spaces before and after the field string before verification.

: Regular Expression validator, which checks whether the field of the split check matches a regular expression.

Attribute -- expression: Specifies the regular expression (regexExpression is used for version 2.3.15 ). CaseSensitive: Specifies whether the regular expression is case sensitive.

: Integer validator, which requires that the integer value of the field must be within the specified range.

Property -- min: specifies the minimum value. Max specifies the maximum value.

: Double-precision floating point checker, requires that the field's double-precision floating point value must be within the specified range.

Property -- min: specifies the minimum value. Max specifies the maximum value.

: OGNL expression validator. The field must meet an ognl expression.

Attribute -- expression: Specifies the ognl expression (the expression must be written in <! [CDATA []> ).

: Email address checker. It is required that the field value be a valid email address if it is not null.

: URL validator. It is required that the field value be a valid url if it is not null.

: Date validator, which requires that the field's date value be within the specified range

Property -- min: specifies the minimum value. Max specifies the maximum value.

: Conversion validator, which specifies that an error message is prompted when type conversion fails.

: Used to verify compound attributes in an action. It identifies a validation file used to verify attributes in the composite attributes.

: OGNL expression validator. The field must meet an ognl expression. This logical expression is evaluated based on ValueStack. This validator cannot be used in the style configuration of the field validator!

Attribute -- expression: Specifies the ognl expression (the expression must be written in <! [CDATA []> ).

 

3. Now the Visitor validator works. view the result:


Page:

The entered error message does not pass the verification. Jump to the input view with the name, that is, register, jsp:

Enter the correct information and the verification is successful:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.