2

我有 3 个实体用户、部门和地址。用户可以有多个地址。现在我对构建这个用例的表单有疑问。

我可以直接访问部门,因为它是对象。但我不知道如何引用地址属性,因为它是一个集合。

class Users
{
  private int id;
  private String name;
  private Department dept;

  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "userId", nullable = false)
    private Set<Address> addresses = new HashSet<Address>(0);

  //Getters and setters
}

MyForm.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring 3 MVC Series - Contact Manager</title>
</head>
<body>
<h2>Contact Manager</h2>
<form:form method="post" commandName="userObject" action="addPerson.html">

    <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td> 
    </tr>
    <tr>
        <td><form:label path="userObject.dept.deptName">Department Name</form:label></td>
        <td><form:input path="userObject.dept.deptName" /></td>
    </tr>
    <tr>
        <td><form:label path="userObject.addresses.street">Street Address</form:label></td>
        <td><form:input path="userObject.addresses.street" /></td>
    </tr>

    <tr>
        <td colspan="2">
            <input type="submit" value="Add Contact"/>
        </td>
    </tr>
</table>  

</form:form>

如何使用对象访问我的 jsp 表单中的地址属性。我在 userObject.addresses.street 的 jsp 中遇到问题

4

2 回答 2

2

我会使用 jstl taglib :

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

然后使用 foreach

并做一些像:

<c:forEach var="item" items="${userObject.addresses}">
  ${item.street}
</c:forEach>
于 2014-06-04T11:37:51.380 回答
1

您无法访问 a List,因为它是单个Object,因此您需要遍历User'sadresses并显示街道或每条街道。

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
  <title>Spring 3 MVC Series - Contact Manager</title>
</head>
<body>
  <h2>Contact Manager</h2>
  <form:form method="post" commandName="userObject" action="addPerson.html">

  <table>
    <tr>
      <td><form:label path="name">Name</form:label></td>
      <td><form:input path="name" /></td> 
    </tr>
    <tr>
      <td><form:label path="userObject.dept.deptName">Department Name</form:label></td>
      <td><form:input path="userObject.dept.deptName" /></td>
    </tr>
    <tr>
      <c:forEach var="address" items="${userObject.getAddresses()}">
        <td><form:label path="userObject.addresses.street">Street Address</form:label></td>
        <td><form:input path="userObject.addresses.street" />${address.street}</td> 

    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" value="Add Contact"/>
      </td>
    </tr>
  </table>  

  </form:form>
  ...
</body>
</html>
于 2014-06-04T11:45:29.763 回答