Configuration of Struts 2 Verification Framework and validation.xml Verification Rules

Reference resources: http://www.blogjava.net/focusJ/archive/2010/11/15/367272.html
http://blog.csdn.net/hackcoder/article/details/18510337

Naming rules and placement paths for validation.xml:
File name: <ActionClassName>-validation.xml
ActionClassName > is the name of the Action class to verify. To place this file in the same directory as the Class file.

If the Action class has multiple action instances (action names) in the struts configuration, the validation file name rules for an action are as follows:

File name: <ActionClassName> - <aliasName>-validation.xml

For example: UserAction-login-validation.xml

(Note: The above <aliasName> is not the method name, but the name of the action configured in struts.xml)
Content example of validation.xml

<?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="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Please fill in the user name.</message>
</field-validator>
<field-validator type="stringlength"> 
<param name="minLength">4</param>
<param name="maxLength">32</param> 
<message>User name length should be between 4 and 32 characters</message> 
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Please fill in the password.</message>
</field-validator>
<field-validator type="stringlength"> 
<param name="minLength">6</param>
<param name="maxLength">32</param> 
<message>For the sake of security of your account, please set a password of more than 6 letters (up to 32 letters).</message> 
</field-validator>
</field>
</validators>

In fact, message information can also be configurable internationalization: the configuration method is extremely simple < message key = "userpass. required"> </message>, key corresponds to internationalization information in the internationalization configuration file.

Struts 2's validation rules are probably the following:
required: Mandatory Checker
requiredstring: mandatory string checker
int: Integer Checker
double: double-precision floating-point verifier
date: date Checker
Expression: expression checker
Field expression: field expression checker
email: email Verifier
url: Web Checker
visitor:Visitor Checker
conversion: conversion Checker
String length: String length checker
regex: Regular expression checker

Commonly used validation rules:

1. Compulsory examination

<validators>
<field name="username">
<field-validator type="required">
< message > Indicate message for failure of inspection </message >
</field-validator>
</field>
</validators>

2. Mandatory String Check

<validators>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Tips for specifying test failures</message>
</field-validator>
</field>
</validators>

3. Integer Inspector/Floating Point Inspection

<validators>
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>Age must be between 1 and 150</message>
</field-validator>
</field>
</validators>

4. Date test

<validators>
<field name="birth">
<field-validator type="date">
<param name="min">1900-01-01</param>
<param name="max">2050-02-21</param>
<message key="birth.range"/>
</field-validator>
</field>
</validators>

5. Field expression checker (requiring a specified field to satisfy a logical expression)

<validators>
<field name="re_pass">
<field-validator type="fieldexpression">
<! - Specify logical expressions - >.
<param name="expression"> (pass eq re_pass)</param>
<message>Password must be equal to confirmation password </message>
</field-validator>
</field>
</validators>

6. Mail Address Checking

<validators>
<field name="email">
<field-validator type="email">
<message>Your e-mail address must be a valid e-mail address </message>
</field-validator>
</field>
</validators>

7. Web Site Inspection

<validators>
<field name="url">
<field-validator type="url">
<message> Your home address must be a valid address </message>.
</field-validator>
</field>
</validators>

8. String Length Check

<validators>
<field name="user">
<field-validator type="stringlength">
<param name="minlength">4</param>
<param name="maxlength">20</param>
<message>Your username must be between 4 and 20</message>
</field-validator>

There are two problems in the application process.
1. Struts 2 validation. XML does not work
As a result of http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd It is no longer a dtd constraint file.
Open the website and find that opensymphony's website has been migrated, because xwork's stuff has been incorporated into struts2 and become part of apache.

All DTDs have been handed over to http://struts.apache.org/dtds/ This place.

Later, the specification of the struts 2 verifier should be changed to:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

In this way, the report will not be wrong and the operation will be normal.

2. Struts 2 validation. XML regular expressions do not work

<validators>
    <field name="username">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>User name cannot be empty</message>
        </field-validator>
        <field-validator type="regex">
             <param name="regexExpression"><![CDATA[(\w{4,20})]]></param>
            <message>The length is 4.-25</message>
        </field-validator>
    </field>


    <field name="password">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Password cannot be empty</message>
        </field-validator>
        <field-validator type="regex">
             <param name="regexExpression"><![CDATA[(\w{4,20})]]></param>
            <message>Password 4-25</message>
        </field-validator>
    </field>
</validators>

Amendment:

<param name="expression"><![CDATA[(\w{4,25})]]></param>

The parameter name was changed to regexExpression

<param name="regexExpression"><![CDATA[(\w{4,25})]]></param>

Tags: xml Struts Apache encoding

Posted on Sun, 31 Mar 2019 09:09:30 -0400 by isam4m