Struts2的输入校验

struts.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>

    <constant name="struts.custom.i18n.resources" value="mess"></constant>

    <constant name="struts.i18n.encoding" value="GBK"></constant>
    
    <package name="lee" extends="struts-default">
        <action name="login" class="com.lee.action.LoginAction">
            <result name="input" type="">/login.jsp</result>
            <result name="success">/welcome.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
        
        <action name="regist" class="com.lee.action.RegistAction">
            <result name="input">/regist.jsp</result>
        </action>
    </package>
</struts>

 action类

package com.lee.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    
    private String userName;
    
    private String password;
    
    private Integer age;
    
    private Date birthday;
    
    public String execute() throws Exception{
        
        //TODO
        return null;
        
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the age
     */
    public Integer getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * @return the birthday
     */
    public Date getBirthday() {
        return birthday;
    }

    /**
     * @param birthday the birthday to set
     */
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    

}

RegistAction-validation.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Vlidators 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="username">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>必须输入用户名。</message>
        </field-validator>
        <field-validator type="regex">
            <param name="expression"><![CDATA[(^[A-Za-z0-9]{4,25}$)]]></param>
            <message>用户名必须是字母或者数字,长度在4到25位之间。</message>
        </field-validator>
    </field>
    
    <field name="password">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>必须输入密码。</message>
        </field-validator>
        <field-validator type="regex">
            <param name="expression"><![CDATA[^[A-Za-z0-9]{4,25}$]]></param>
            <message>密码必须是字母或者数字,长度在4到25位之间。</message>
        </field-validator>
    </field>
    
    <field name="age">
        <field-validator type="int">
            <param name="min">1</param>
            <param name="max">150</param>
            <message>年龄必须在1到150之间。</message>
        </field-validator>
    </field>
    
    <field name="birthday">
        <field-validator type="date">
            <param name="min">1900-01-01</param>
            <param name="max">2100-12-31</param>
            <message>生日必须在${min}到${max}之间。</message>
        </field-validator>
    </field>
</validators>

regist.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title><s:text name="registpage"></s:text></title>
</head>
<body>
    <s:form action="regist">
        <s:fielderror cssStyle="color:red"/>
        <s:textfield name="username" label="用户名"></s:textfield>
        <s:password name="password" label="密码"></s:password>
        <s:textfield name="age" label="年龄"></s:textfield>
        <s:textfield name="birthday" label="生日"></s:textfield>
        <s:submit value="注册"></s:submit>
    </s:form>
</body>
</html>

使用XWork提供的Annotation的输入校验

@RequiredStringValidator(key="name.required",message="用户名不能为空。")
    public void setUserName(String userName) {
        this.userName = userName;
    }

 如果Struts的校验器无法满足需求,可以通过重写ActionSupport类的validate方法手动完成校验。

    @Override
    public void validate(){
        
        if(!getUserName().contains("lee")){
            addFieldError("username","********************");
        }
    }
原文地址:https://www.cnblogs.com/harryV/p/3696203.html