Struts 2 - > validate and interceptor

Keywords: Attribute Struts xml Apache

1.Validate
1.1 Rewriting validate Method

For example, do the login page on the front desk

action:

1.2 checkout framework

1. Write a checking rule file
 It is a typical XML file in the format of ActionName-validation.xml
 The validation file needs to add a dtd constraint description
 The dtd description is in xwork-core
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
2. Configuration of Checkers
 a) Commonly used built-in verifiers
b)  struts-2.3.20.1\src\xwork-core\src\main\resources\com\opensymphony\xwork2\validator\validators\default.xml
 3. Perfect the checking documents and carry out the checking

xml configuration template

<?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>
    <field name="user.username">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>User name cannot be empty!</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">8</param>
            <param name="maxLength">20</param>
            <message>User name length must be ${minLength}-${maxLength}Between</message>
        </field-validator>
    </field>
    <field name="user.password">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Password cannot be empty!</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">6</param>
            <message>Password length must be greater than ${minLength}</message>
        </field-validator>
    </field>
    <field name="repassword">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Confirm that the password cannot be empty!</message>
        </field-validator>
        <field-validator type="fieldexpression">
            <param name="expression">user.password==repassword</param>
            <message>Two password inconsistencies</message>
        </field-validator>
    </field>
    <field name="user.phone">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>The phone number can't be empty!</message>
        </field-validator>
        <field-validator type="regex">
            <param name="regex">1[3578][0-9]{9}</param>
            <message>Incorrect telephone number format!</message>
        </field-validator>
    </field>
    <field name="user.email">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Mailbox can't be empty!</message>
        </field-validator>
        <field-validator type="email">
            <message>The mailbox format is incorrect</message>
        </field-validator>
    </field>
</validators>

2. interceptor

1. What is Interceptor, Interceptor Stack

An interceptor is a mechanism that allows users to do some functional processing before and after an Action is executed
 Each interceptor has only one instance and adopts the singleton mode, so if class variables are used in the interceptor, attention should be paid to synchronization.
Interceptor is an implementation of AOP. The principle of Struts 2 interceptor is relatively simple. When Struts 2 action is requested,
Strts2 looks up the configuration file and instantiates the corresponding interceptor object according to its configuration, if there are multiple interceptors.
Then it connects into a chain to form an interceptor stack, and then calls the interceptors in the interceptor stack one by one.

The sequence of interceptors before action execution and after result execution is contrary. You can see the following figure:

That is to say, the order of execution is: Interceptor 1 - Interceptor 2 - Interceptor 3 - Action - Result - Interceptor 3 - Interceptor 2 - Interceptor 1

Advantages of 2.2 Interceptor

Interceptors and filters
1. The filter belongs to WEB container and can filter all requests (action, servlet, jsp, html);
2. Interceptors belong to struts 2 framework and can only intercept action s (jsp cannot intercept)
3. The filter is implemented by callback function, and the interceptor is implemented by dynamic proxy.

Default 18 interceptors

Application of Interceptor to Record Action Execution Time

Interceptor configuration in struts.xml

Posted by KGodwin on Mon, 10 Dec 2018 10:09:06 -0800