Spring MVC:List <e>需要作为命令对象传递</e>

时间:2013-04-10 11:52:48

标签: list jsp spring-mvc

我需要将List作为commandObject传递。

我的代码

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

我的jsp页面

<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>  

但它没有通过,我犯了错误,可以提出任何建议。

2 个答案:

答案 0 :(得分:2)

您必须将列表包装在一个类中,看起来您已经使用了Employee吗?

它应该看起来像这样:

<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"/>            

答案 1 :(得分:2)

你的forEach循环中断了你应该迭代命令对象列表中的元素。

使用表单标记代替 spring:bind标记

试试这个

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