Vi. struts2 Input Validation

Source: Internet
Author: User
Tags ranges
Document directory
  •  
  • 2. Data Validation File Content
  • Client verification method:
  • Short Circuit validator
  • Rules of the short-circuit validator:
  • The running sequence of the validator.

 

1. Introduction to struts2 Input Validation

 

The input checksum and type conversion of struts2 process the request parameters.

As the name implies, the input verification indicates whether the request parameters meet certain requirements;

 

Client Verification & server Verification

Client verification refers to the preliminary verification on the browser side through JavaScript, in order to reduce the load on the server side;

Server-side verification is the last line of defense for data verification;

 

In struts2, data verification does not need to write any code, but only requires a configuration file to configure the verification conditions, which is very simple; therefore, the data verification file is the most important part of data verification;

 

Ii. Data Validation rule File

 

1. Data validation rules file rules:

 

1. Some parameter validation conditions are specified in this file;

2. It can include the field type validator and non-field type validator;

3. Naming rules: ActionName-validation.xml, where actionname is the name of the action class;

4. Each action has a validation file, which is placed in the same directory as the action;

5. In the configuration logic action we mentioned earlier, if you need to configure a data validation file for a logical action, the naming rule is: ActionName-LogicActionName-validation.xml;

6. Note: When you need to verify the logical action, if there is a common form of ActionName-validaton.xml file, it will also verify the logical action;

7. after the input validation fails, the error will be encapsulated into fielderror and put into action context like the type conversion. Therefore, you can add <s: fielderror/> in JSP to display the error, this content is also configured in the data validation file;

8. When the input validation fails, the logic view returned is the same as the type conversion. Therefore, the <result name = "input"> </input>

9. After the input validation fails, the struts form tag will automatically output an error message;

 

Field Type validators & non-field type validators

 

Field Type validator: Write each action attribute in one unit, that is, write in the following style:

Attribute 1

Rule 1

Rule 2

Attribute 2

Rule 1

Rule 2

 

 

Non-field validators: rules are written in the following format:

Rule 1

Attribute

Rule 2

Attribute

Rule 3

Attribute

2. Data Validation File Content

 

1. The data verification file uses <validators> as the root element;

2. The field validator is based on attributes. The content template is as follows:

 

<validators><field name=""><field-validator type=""><param name=""></param><message></message></field-validator></field></validators>

 

3. The non-field validator is in the unit of the validator. Content template:

 

<validator type=""><param name="fieldName"></param><param name=""></param><message></message></validator>

 

 

Sample Code:

 

In this Code, the data of the string, date, integer, and email are verified;

Validation01action. Java

 

package org.validation.action;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class Validation01Action extends ActionSupport {private String name;private String email;private int age;private Date date;public String execute()throws Exception{return SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}}

1. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

 

IfField validator Configuration, Refer to the following:

Validation01Action-validation.xml

(1) <field name = ""> is an attribute, and name is the action attribute name;

(2) <field-validator type = ""> Configure A validator. type indicates the type of the validator;

(3) <param> is the configuration parameter, such as the minimum and maximum values;

(4) <message> indicates the error message. If you want to internationalize the error message, you canGlobal Resource file<Message key = "key"/>. The key is the key of the international resource file;

 

 

<! Doctype validators public "-// Apache struts // xwork validator 1.0.2 //" http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd "> <validators> <field name =" name "> <field-validator type =" requiredstring "> <Param name =" trim "> true </param> <message> name cannot be blank </message> </field-validator> <field-validator type =" regEx "> <Param name =" expression "> <! [CDATA [(\ W {4, 10})]> </param> <message> the name must be 4-10 characters long </message> </field-validator> </field> <field name = "Age"> <field-validator type = "requiredstring"> <Param name = "trim"> true </param> <message> Age cannot be blank </message> </field-validator> <field-validator type = "int"> <Param name = "min"> 0 </param> <Param name = "Max"> 200 </param> <message> age range: 0-< /message> </field-validator> </field> <field name = "email"> <field-validator type = "requiredstring"> <Param name = "trim"> true </param> <message> email cannot be blank </message> </field-validator> <field-validator type = "email"> <message> Incorrect email format </ message> </field-validator> </field> <field name = "date"> <field-validator type = "requiredstring"> <Param name = "trim"> true </param> <message> the date cannot be blank </message> </field-validator> <field-validator type = "date"> <Param name = "min"> 2010-01-01 </param> <Param name = "Max"> 2010-12-31 </param> <message> the date ranges from to </message> </field-validator> </field> </validators>

IfNon-field validator, Then

(1) <validator type = ""> indicates a validator. The type attribute indicates the type of the validator;

(2) <Param name = "fieldname"> </param> this element is necessary and specifies the name of the Action attribute;

(3) <message> indicates the error message;

<! Doctype validators public "-// Apache struts // xwork validator 1.0.2 // en" http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd "> <validators> <validator type =" requiredstring "short-circuit =" true "> <Param name = "fieldname"> name </param> <Param name = "trim"> true </param> <Message key = "AA"> </message> </ validator> <validator type = "requiredstring" short-circuit = "true"> <Param name = "fieldname"> age </param> <Param na Me = "trim"> true </param> <message> Age cannot be blank </message> </validator> <validator type = "requiredstring" short-circuit = "true"> <Param name = "fieldname"> email </param> <Param name = "trim"> true </param> <message> email cannot be blank </message> </ validator> <validator type = "requiredstring" short-circuit = "true"> <Param name = "fieldname"> date </param> <Param name = "trim"> true </param> <message> the date cannot be blank </message> </validator> <validator type = "RegEx"> <Param name = "fieldname"> name </param> <Param name = "expression"> <! [CDATA [(\ W {4, 10})]> </param> <message> the name must be 4-10 characters long </message> </validator> <validator type = "int"> <Param name = "fieldname"> age </ param> <Param name = "min"> 0 </param> <Param name = "Max"> 200 </param> <message> age range: 0-</message> </validator> <validator type = "email"> <Param name = "fieldname"> email </param> <message> Incorrect email format </message> </validator> <validator type = "date"> <Param name = "fieldname"> date </param> <Param name = "min"> 2010-01-01 </param> <Param name = "Max "> </param> <message> the date ranges from to </message> </validator> </validators>

 

Client verification method:

 

Add the attribute validate = "true" to the <s: Form> element of JSP. After the attribute is added, the rule in the validation rule file is changed to the client validation rule, that is, no verification requires access to the server;

Note: If you directly access the JSP page, the client verification cannot be reflected. The client verification takes effect only when you access an action and jump to the JSP page;

 

Short Circuit validator

 

In general, if we configure two validators for name: the first one is "cannot be blank", and the second one is "length greater than 4 and less than 10", if the name field is empty, the two prompts will be output at the same time, but in fact, if the user input is empty, only the "cannot be empty" should be output, which requires a short circuit validator;

Add short-circut = "true" to the <validator> or <field-validator> element;

 

Rules of the short-circuit validator:

 

1. When the non-field validators fail to verify, all subsequent field validators will not be executed, and other non-field validators will not be affected;

2. When the verification fails, all subsequent field validators will not execute the verification;

 

The running sequence of the validator.

 

1. The non-field validator is executed first than the field validator;

2. previously executed;

 

 

Iii. built-in checker

 

The built-in checker is defined in the COM/opensymphony/xwork2/validator/validators/Default. xml of the xwork-core-Xxx.jar;

The following describes the built-in validators. The following describes the field-type validators. Non-field validators only need simple transformations;

 

1. Required validator

 

This validator requires that the field be non-empty;

 

2. requiredstring validator

 

This validator requires that the check string is not empty and must be greater than 0 in length, that is, it cannot be ""

Optional parameters:

(1) TRIM: if true, the leading and trailing spaces are removed, similar to the trim function of string;

 

3.int validator

 

This validator requires that the integer be within a certain range;

Optional parameters:

(1) min: Minimum value;

(2) max: maximum value;

 

4. Date validator

 

The required date is within a certain range;

Optional parameters:

(1) min: Minimum date;

(2) max: The maximum date;

 

5. fieldexpression validator

 

The validator must meet the specified logic expression;

Optional parameters:

(1) expression: logical expression. Format: <! [CDATA [(logical expression)]>

 

6. Email validator

 

The required fields must meet the email format;

 

7. url validator

 

The Field Format must meet the URL format;

 

8. stringlength checker

 

The required field is string and the length is within a certain range;

Optional parameters:

(1) TRIM: the front and back spaces are deleted;

(2) minlength: minimum length;

(3) maxlength: Maximum length;

 

9. RegEx checker

 

The specified regular expression is required;

Optional parameters:

(1) expression: Regular Expression. Format: <! [CDATA [(Regular Expression)]>

(2) casesensitive: case sensitive;

 

Iv. Manual verification

 

If the built-in validator does not meet the requirements, you can customize the validator by overwriting the public void validate () method in the action class. If the conditions are not met, call addfielderror ("name ", "Message"), and then return to the input logic view;

 

Of course, if an action class has multiple logical actions, the validate method will not work. Therefore, you need to define the validatexxx () method. For example:

If the Logical Action processing method is fun (), the validatefun () method is defined to implement the same method as the validate method;

 

Note: If the verification function of the Logical Action is validatexxx (), The Validate () method will also be called, because the validate () method will always work as long as the physical action is verified. This is the same as the preceding validation rule file;

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.