简体   繁体   中英

Spring MVC 3 Binding of Complex Objects to the controller

Suppose i have two classes as shown below

Class Student{
   private int id;
   private String name;
   private String address;
}

Class University{
   private String id;     
   private String name;
   private String address;
   private ArrayList<Student> students;
   //Getters and Setter methods
}

Now how can i bind university bean to form such that i can capture the data of student list in the controller ? Something like this

<form:form action ="controller.htm">
//... input tags binded with university id, name and address

<form:input path = "students[0].id">
<form:input path = "students[0].name">
<form:input path = "students[0].address">


<form:input path = "students[1].id">
<form:input path = "students[1].name">
<form:input path = "students[1].address">

//... and so on
</form:form>

Example to iterate over the student collection.

 <tr>
        <td>Favourite Web Frameworks :</td>
        <td><c:forEach items="${studentCollection}" var="student">
               [<c:out value="${student.id}" />]
            </c:forEach>
        </td>
    </tr>

Refer this Example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM