Struts completes input verification manually based on validateXxx method

One view

1registForm.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title>Please enter your registration information.</title>
    <s:head/>

</head>


<body>
<h2>Please enter your registration information.</h2>
<s:fielderror/>
<s:form action="regist">
    <s:textfield name="name" label="User name"/>
    <s:textfield name="pass" label="Password"/>
    <s:textfield name="age" label="Age"/>
    <s:textfield name="birth" label="Birthday"/>
    <s:submit value="register"/>
</s:form>

</body>

</html>

2 show.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title>Check success</title>

</head>


<body>
<h3>Check success</h3>
    //User name: <s:property value="name"/><br/>
    //Password: <s:property value="pass"/><br/>
    //Age: <s:property value="age"/><br/>
    //Birthday: <s:property value="birth"/><br/>

</body>

</html>

II. Configuration files

<?xml version="1.0" encoding="GBK"?>
<!-- Appoint Struts 2 Configuration file DTD information -->
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- Specifies the character set encoded by the application. -->
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">
        <!-- Define what handles user requests regist Action,Use RegistAction Of
            regist Method to process user requests -->
        <action name="regist" class="org.crazyit.app.action.RegistAction"
            method="regist">
            <result name="input">/WEB-INF/content/registForm.jsp</result>
            <result>/WEB-INF/content/show.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>

Three action

package org.crazyit.app.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.*;


public class RegistAction extends ActionSupport
{
    private String name;
    private String pass;
    private int age;
    private Date birth;

    // setter and getter methods of name
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // setter and getter methods of pass
    public void setPass(String pass)
    {
        this.pass = pass;
    }
    public String getPass()
    {
        return this.pass;
    }

    // setter and getter methods of age
    public void setAge(int age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return this.age;
    }

    // setter and getter methods of birth
    public void setBirth(Date birth)
    {
        this.birth = birth;
    }
    public Date getBirth()
    {
        return this.birth;
    }

    public String regist()
    {
        return SUCCESS;
    }

    public void validate()
    {
        System.out.println("Get into validate()Method Checking" + name);
        // Require that the user name must contain a crazyit substring
        if(!name.contains("crazyit"))
        {
            addFieldError("user" , "Your username must contain crazyit!");
        }
    }
    public void validateRegist()
    {
        System.out.println("Get into validateRegist()Method Checking" + name);
        // Requires that the user name must contain. org substrings
        if(!name.contains(".org"))
        {
            addFieldError("user" , "Your username must contain.org!");
        }
    }
}

Four International Information

The xwork. default. invalid. field value={0} field is invalid
 name.requried = you must enter a user name!
name.regex = The user name you enter can only be alphabets and arrays, and the length must be between 4 and 25!
pass.requried = You must enter your password!
pass.regex = The password you enter can only be letters and arrays, and the length must be between 4 and 25!
age.range = your age must be between ${min} and ${max}!
birth.range = your birthday must be between ${min} and ${max}!

V. Verification documents

<?xml version="1.0" encoding="GBK"?>
<!-- Specifies the validation profile DTD information -->
<!DOCTYPE validators PUBLIC
    "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<!-- Check the root element of the file -->
<validators>
    <!-- check Action Of name attribute -->
    <field name="name">
        <!-- Appoint name Attributes must satisfy the required rules -->
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="name.requried"/>
        </field-validator>
        <!-- Appoint name Attributes must match regular expressions -->
        <field-validator type="regex">
            <param name="regex"><![CDATA[(\w{4,25})]]></param>
            <message key="name.regex"/>
        </field-validator>
    </field>
    <!-- check Action Of pass attribute -->
    <field name="pass">
        <!-- Appoint pass Attributes must satisfy the required rules -->
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="pass.requried"/>
        </field-validator>
        <!-- Appoint pass Property must match the specified regular expression -->
        <field-validator type="regex">
            <param name="regex"><![CDATA[(\w{4,25})]]></param>
            <message key="pass.regex"/>
        </field-validator>
    </field>
    <!-- Appoint age Property must be within the specified range-->
    <field name="age">
        <field-validator type="int">
            <param name="min">1</param>
            <param name="max">150</param>
            <message key="age.range"/>
        </field-validator>
    </field>
    <!-- Appoint birth Property must be within the specified range-->
    <field name="birth">
        <field-validator type="date">
            <!-- When specifying the date string below, you must use this Locale Date format -->
            <param name="min">1900-01-01</param>
            <param name="max">2050-02-21</param>
            <message key="birth.range"/>
        </field-validator>
    </field>
</validators>

Six test

Keywords: Struts JSP Apache Java

Added by Maugrim_The_Reaper on Thu, 03 Oct 2019 17:24:57 +0300