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

时间:2016-04-27 05:59:50

标签: java validation jsp struts2 visitor-pattern

我正在开发一个用户可以输入多个地址的应用程序。我正在使用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>

1 个答案:

答案 0 :(得分:3)

您有一个 EmployeeModel个对象,该对象包含多个 AddressModel个对象。

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

您正确地说AddressModel必须通过访问者验证进行验证:

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

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

而不是

<强> EmployeeModel-validation.xml中

你需要使用

<强> AddressModel-validation.xml中

并且在其中,您需要验证简单属性,前面没有任何employeeModel(因为这个验证可能是访问者使用的许多不同的验证文件,不同的操作和模型包含{{1那不关心它的父对象):

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

并在您的javascript代码中执行相同操作。