STRUTS2 Study notes-using validator to verify data

Source: Internet
Author: User

We are doing some operations are required to verify the user's input data, such as the registration of the site, the need for data validation of individual data items, STRUTS2 provides some default validators, such as digital detection, mailbox detection, string length detection and so on.

    • The Common validator
Checker Role
Required Required validator, required field must have value
Requiredstring

A required string validator that must have a value and a length greater than 0, which cannot be an empty string. The default is to remove whitespace before and after string

Parameter fieldname: This parameter specifies the field name of the check, and if it is a field check, do not specify the parameter

Parameter trim: This parameter is an optional parameter that specifies whether to defragment the string before validation.

Stringlength

A string length validator that verifies whether the length of a string in a field is within a specified range

Parameter maxLength: Used to specify the maximum string length, which is optional

Parameter minLength: Used to specify the minimum string length, which is optional

Int

Integer validator, which can be configured with integers within a specified range

Parameter min: Specifies the minimum value for the field value, which is optional

Parameter max: Specifies the maximum value of the field value, which is optional

Date

Date validator, which can be configured with a date within a specified range

parameter min: Specifies the minimum value for the field date value, which is optional

Parameter max: Specifies the maximum value for the field date value, which is optional

Email

The mail address checker, which requires the checked field to be a valid e-mail address if it is not empty.

Regex

Checks whether a regular expression can be matched and the parameter is regex

Requirements for the validation framework using STRUTS2:

  Add a configuration file for the validation framework under the corresponding action package, with the file name called the Action class name-validation.xml. If there are multiple methods in the action, You typically use the action class name-action alias-validation.xml. For example Loginaction-adduser-validation.xml.

  In particular, it is important to note that the action that supports the checksum must implement the Validateable interface, generally inheriting the Actionsupport class.

Below write a simple user registration demo to use validator, under the Web project to create a new registered JSP file, named regist.jsp, introduced OGNL tag library, the body part of the code is:

<body> <s:fielderror></s:fielderror> <s:form action="user/regist.action"Validate="true">User name:<input type="text"Name="User.Name"/><br>Password:<input type="Password"Name="User.password"/><br>Date of birth:<input type="text"Name="user.date"/><br>e-mail:<input type="text"Name="User.email"/><br> <input type="Submit"Value="Register"/> </s:form> </body>

Create a new user entity class under the entity package, the code is omitted.

Create a new registaction under the action package, inherit the Actionsupport class, and omit the code.

Create a new Registaction-validation.xml under the action package with the following code:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE validators Public "-//apache struts//xwork Validator 1.0.3//en" "http://struts.apache.org/dtds/ Xwork-validator-1.0.3.dtd ">        <validators>               <Fieldname= "User.Name">                   <Field-validatortype= "Requiredstring">                       <paramname= "Trim">True</param>                       <message>User name cannot be empty</message>                   </Field-validator>                   <Field-validatortype= "Stringlength">                       <paramname= "Trim">True</param>                       <paramname= "MaxLength">10</param>                       <paramname= "MinLength">4</param>                       <message>The user name must be between 4 and 10 in length</message>                   </Field-validator>                              </Field>               <Fieldname= "User.birthday">                   <Field-validatortype= "Date">                       <paramname= "min">1900-01-01</param>                       <paramname= "Max">2016-01-01</param>                       <message>Date does not meet the requirements</message>                   </Field-validator>               </Field>               <Fieldname= "User.email">                   <Field-validatortype= "Email">                       <paramname= "Trim">True</param>                       <message>The mailbox format does not meet the requirements</message>                   </Field-validator>               </Field>        </validators>

Configure action within Struts.xml:

< Packagename= "User"namespace= "/user"extends= "Struts-default">        <Actionname= "Regist"class= "Com.wang.action.RegistAction" >            <result>/index.jsp</result>            <resultname= "Input">/register.jsp</result>        </Action>    </ Package>

If the test fails, it goes to the input page to display an error message, so there must be a JSP page called input in the action configuration. After running, if you enter data that does not meet the requirements, you will be prompted in the browser.

I encountered a problem when I wrote this demo because I used the OGNL tag and reported an error when I entered the URL to access the regist.jsp page :

The Struts dispatcher cannot be found. This was usually caused by using Struts tags without the associated filter. Struts tags is only usable when the request had passed through its servlet filter, which initializes the Struts Dispatche R needed for this tag. -[Unknown Location]

Later, the url-pattern in Web. XML was changed to/* *.action . It is said that there is another way is not to change the above configuration, only need to add a url-pattern for the *.jsp , pending inspection.

STRUTS2 Study notes-using validator to verify data

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.