Spring MVC : List need to pass as command objec

2019-09-03 05:11发布

问题:

I need to pass List as commandObject.

My code

public class Employee 
{       
List<Dept> dlist = new ArrayList<Dept>();    
public List<Dept> getDlist()
    {
return dlist;
}    
public void setDlist(List<Dept> dlList)
    {
this.dlist = dlist;
}       
}

My jsp page

<c:forEach var="d" items="${dlist}">
<spring:bind path="dlist[0].projectId">
<input type="text" name="projectId" value='<c:out value="${d.projectId}" />' />
</spring:bind>
</c:forEach>  

But it not passing, where i making mistake, can any suggest.

回答1:

Your forEach loop is broken you should iterate over the elements in your List of the command Object.

Use form tags instead of spring:bind tags

Try like this

<c:forEach var="d" items="${command.dlist}" varStatus="status">
  <form:input path="dlist[status.index].projectId" />
</c:forEach>


回答2:

You will have to wrap the list in a class, which it looks like you have done with Employee ?

It should look something like this then :

<form:form id="frmEmployee" commandName="myEmployee" action="/Whatever/" method="POST">
    <c:forEach items="${myEmployee.dlist}" var="dept" varStatus="status"><tr>
      <tr><td>
        <form:input path="dlist[status.index].projectId" id="title"/>