Spring MVC一对多表单提交

时间:2014-09-07 23:26:31

标签: spring spring-mvc

我有一个“信息”类,其中包含用户信息,例如姓名,电子邮件,电子邮件。

我还有另一个名为“地址”的课程,其中包含用户的地址信息。

Info类与地址的一对多关系。所以Info类有一个Address列表。例如:列表<地址> addList;

现在我想要一个表单,以便用户可以放置他们的信息。在表单中,用户将输入他们的Info类信息和Address类信息。我试图创建一个jsp页面,从用户获取所有参数,但我在这个jsp页面上收到错误。

<c:forEach items="info.addList" var="address" varStatus="status">

  <div class="form-group" > 
    <form:label path="address.address1">Street Name</form:label>
    <form:input path="address.address1" cssClass="form-control"/>
  </div>
  <div class="form-group">
    <form:label path="address.address2">Apt/Suite</form:label>
    <form:input path="address.address2" cssClass="form-control"/>
  <div>
  <div class="form-group">
    <form:label path="address.city">City</form:label>
    <form:input path="address.city" cssClass="form-control"/>
  </div>
</c:forEach>


org.springframework.beans.NotReadablePropertyException: Invalid property 'address' of bean class     [  com.ttg.mvc.beans.Info]: Bean property 'address' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:725)

所以这是Info Class:

other properties................
private String gender;
@Column(name = "dob")
private String dob;
@Column(name = "ssn")
private String ssn;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId", fetch = FetchType.EAGER)
private Collection<Sales> salesList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId", fetch = FetchType.EAGER)
private Collection<Address> addlist;

地址类:

other properties..........
private String country;
@Column(name = "zipcode")
private String zipcode;
@Column(name = "type")
private String type;

@JoinColumn(name = "userId", referencedColumnName = "userId")
@ManyToOne(optional = false)
private Info userId;

这是该网址的请求映射,以显示注册表单

@RequestMapping("/register")
public String showRegistrationForm(Model model) {
    Info info= new Info();
    model.addAttribute("info", info);
    return "registration";
}

2 个答案:

答案 0 :(得分:1)

我认为jsp代码应该如下所示,因为Address是一个集合,所以我们需要使用index。索引将是动态的,因为用户可以添加更多地址或您希望维护它的任何方式。

<c:forEach items="info.addList" var="address" varStatus="status">

  <div class="form-group" > 
    <form:label path="address[0].address1">Street Name</form:label>
    <form:input path="address[0].address1" cssClass="form-control"/>
  </div>
  <div class="form-group">
    <form:label path="address[0].address2">Apt/Suite</form:label>
    <form:input path="address[0].address2" cssClass="form-control"/>
  <div>
  <div class="form-group">
    <form:label path="address[0].city">City</form:label>
    <form:input path="address[0].city" cssClass="form-control"/>
  </div>
</c:forEach>

答案 1 :(得分:0)

地址是集合,您必须借助JSP上的索引访问。

<c:forEach items="info.addList" var="address" varStatus="status">

      <div class="form-group" > 
        <form:label path="address[status.index].address1">Street Name</form:label>
        <form:input path="address[status.index].address1" cssClass="form-control"/>
      </div>
      <div class="form-group">
        <form:label path="address[status.index].address2">Apt/Suite</form:label>
        <form:input path="address[status.index].address2" cssClass="form-control"/>
      <div>
      <div class="form-group">
        <form:label path="address[status.index].city">City</form:label>
        <form:input path="address[status.index].city" cssClass="form-control"/>
      </div>
    </c:forEach>