Struts Annotation-based Input Check

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"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="GBK"/>

    <package name="lee" extends="struts-default">
    <!-- User registered Action -->
    <action name="regist" class="org.crazyit.app.action.RegistAction">
        <!-- Type conversion failure, input validation failure, roll in input Corresponding views -->
        <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 com.opensymphony.xwork2.validator.annotations.*;

import java.util.Date;

public class RegistAction extends ActionSupport
{
    // Define four member variables to encapsulate request parameters
    private String name;
    private String pass;
    private int age;
    private Date birth;

    // setter and getter methods of name
    // Use annotations to specify two checking rules for mandatory and regular expressions
    @RequiredStringValidator(key = "name.requried"
        , message = "")
    @RegexFieldValidator(regex = "\\w{4,25}"
        ,key = "name.regex" , message = "")
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // setter and getter methods of pass
    @RequiredStringValidator(key = "pass.requried"
        ,message = "")
    @RegexFieldValidator(regex = "\\w{4,25}"
        ,key = "pass.regex" ,message = "")
    public void setPass(String pass)
    {
        this.pass = pass;
    }
    public String getPass()
    {
        return this.pass;
    }

    // setter and getter methods of age
    @IntRangeFieldValidator(message = ""
        , key = "age.range", min = "1"
        , max = "150")
    public void setAge(int age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return this.age;
    }

    // setter and getter methods of birth
    @DateRangeFieldValidator(message = ""
        , key = "birth.range", min = "1900/01/01"
        , max = "2050/01/21")
    public void setBirth(Date birth)
    {
        this.birth = birth;
    }
    public Date getBirth()
    {
        return this.birth;
    }
}

Four International Information

# Hint information that must be entered in violation of username
 name.requried = you must enter a user name!
# The prompt information that a violation of a user name must match a regular expression
 name.regex = The user name you enter can only be alphabetic and numeric, and the length must be between 4 and 25!
# Hint information that must be entered in violation of password
 pass.requried = You must enter your password!
# A violation of password must match the prompt information of the regular expression
 pass.regex = The password you enter can only be letters and numbers, and the length must be between 4 and 25!
# Age violation must be within a specified range of cues
 age.range = your age must be between ${min} and ${max}!
# Hints that violate birthdays must be within a specified range
 birth.range = your birthday must be between ${min} and ${max}!

Five test

Tags: Struts JSP Java encoding

Posted on Wed, 02 Oct 2019 10:11:20 -0700 by dreamscape