java-Struts 2验证不适用于动态表单字段

我正在开发一个用户可以输入多个地址的应用程序.我正在使用Struts2 XML验证,并且对于那些字段[地址(国家(地区,城市,街道)),我已经使用了访客验证器,但是它不起作用.
验证在其余字段上工作正常.

谁能建议可能是什么问题?

EmployeeForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
<!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=ISO-8859-1">
<title>Add Employee</title>

<script language="javascript">
    function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var counts=rowCount-1;
        var cell1 = row.insertCell(0);
        var street = document.createElement("input");
        street.type = "text";
        street.name="employeeModel.addresses["+counts+"].street";
        cell1.appendChild(houseNum);

        var cell2 = row.insertCell(1);
        var city = document.createElement("input");
        city.type = "text";
        city.name="employeeModel.addresses["+counts+"].city";
        cell2.appendChild(city);

        var cell3 = row.insertCell(2);
        var country = document.createElement("input");
        country.type = "text";
        country.name="employeeModel.addresses["+counts+"].country";
        cell3.appendChild(country);

    }
</script>

</head>
<body>
<s:form action="addEmployee.action" method="post" >
    <s:textfield label="Name" name="employeeModel.employeeName" size="20" />

    <table id="addressesTable" width="350px" border="1">
    <tr>
        <td>Street</td>
        <td>City</td>
        <td>Country</td>
    </tr>
    <tr>
        <td><input type="text" name="employeeModel.addresses[0].street" /></td>    
        <td><input type="text" name="employeeModel.addresses[0].city" /></td>
        <td><input type="text" name="employeeModel.addresses[0].country" /></td>
    </tr>
    </table>
    <input type="button" value="Add More" onclick="addRow('addressesTable')" />
    <s:submit />

</s:form>
</body>
</html>

EmployeeAction-Validation.xml

<validators>
<field name="employeeModel.employeeName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Name is required.</message>
        </field-validator>
    </field>
<field name="employeeModel.addresses" >
      <field-validator type="visitor">
          <message></message>
      </field-validator>
    </field>
</validators>

EmployeeModel-Validation.xml

<!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="employeeModel.addresses" >
      <field-validator type="visitor">
          <message></message>
      </field-validator>
  </field>
</validators>

EmployeeAction.java

 package com.action;

import com.model.EmployeeModel;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    private EmployeeModel employeeModel;

    public String execute() {
        return SUCCESS;
    }


    public EmployeeModel getEmployeeModel() {
        return employeeModel;
    }

    public void setEmployeeModel(EmployeeModel employeeModel) {
        this.employeeModel = employeeModel;
    }
}

EmployeeModel.java

 package com.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class EmployeeModel implements Serializable{

    private static final long serialVersionUID = -484846216250373240L;
    private String employeeName;
    private List<AddressModel> addresses;

    public String getEmployeeName() {
        return employeeName;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public List<AddressModel> getAddresses() {
        return addresses;
    }
    public void setAddresses(List<AddressModel> addresses) {
        this.addresses = addresses;
    }
}

AddressModel.java

 package com.model;

import java.io.Serializable;

public class AddressModel implements Serializable{

    private static final long serialVersionUID = 6965346469594926268L;
    private String country;
    private String city;
    private String street;

    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }   
}

struts.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name="default" extends="struts-default">
      <action name="addEmployee" class="com.action.EmployeeAction" >
            <result name="success">/Result.jsp</result>
            <result name="input">/EmployeeForm.jsp</result>
      </action>
   </package>
</struts>

解决方法:

您有一个EmployeeModel对象,其中有多个AddressModel对象.

这意味着在Action验证文件中,您需要对EmployeeModel属性使用简单验证,而对AddressModel对象使用访客验证.

您正确地说,必须在此处通过访问者验证来验证AddressModel:

<field name="employeeModel.addresses" >
    <field-validator type="visitor">

但是,然后,您正在尝试对EmployeeModel使用验证文件,而不是AddressModel的验证文件!

代替

EmployeeModel-Validation.xml

你需要使用

AddressModel-Validation.xml

在其内部,您需要验证简单的属性,而无需在它们前面添加任何employeeModel(因为此验证可以由许多不同的验证文件供访问者使用,它们具有包含AddressModel的不同操作和模型,而无需关心其父级对象):

<!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="country">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Country is required.</message>
        </field-validator>
    </field>
</validators>

编辑:

您还忘记了命名AddressModel对象的属性.添加它们:

<td><input type="text" name="employeeModel.addresses[0].street" /></td>
<td><input type="text" name="employeeModel.addresses[0].city"    /></td>
<td><input type="text" name="employeeModel.addresses[0].country"  /></td>

并在您的JavaScript代码中执行相同的操作.

上一篇:uni-app h5端跨域问题解决


下一篇:春季-我正在测试的动作中没有合格的Bean类型