Spring Mvc提交/删除表中的已检查(选定)记录
我正在尝试从表格中提交选定的项目,并对它们进行一些修改,但无法正常工作。
MyObject.java
public class MyObject{ boolean checkControl = true; //default true
private String name;
private String code;
//getters & setters
}
MyObjectForm.java
public class MyObjectForm { private List<MyObject> myList;
public List<MyObject> getMyList() {
return myList;
}
public void setMyList(List<MyObject> myList) {
this.myList= myList;
}
}
list-myObjects.jsp
<form:form action="submitList" method="post" modelAttribute="myObjectForm"> <table>
<tbody>
<c:forEach items="${myObjectForm.myList}" var="row" varStatus="status">
<tr>
<td>
<spring:bind path="myList[${status.index}].checkControl">
<input type="checkbox" value="<c:out value="${status.value}"/>" name="isChecked" <c:if test="${row.checkControl}"> checked="checked" </c:if> />
</spring:bind>
</td>
<td>${row.name}</td>
<td>${row.code}</td>
</tr>
</c:forEach>
</tbody>
</table>
<button type="submit">Submit</button>
</form:form>
和控制器
@RequestMapping(value = "/submitList", method = RequestMethod.POST)public String save(@ModelAttribute("myObjectForm") MyObjectForm myObjectForm, Model model) {
List<MyObject> selectedtList = myObjectForm.getMyList(); //returns null
if (selectedtList == null) {
System.out.println("no objects selected");
}
else {
//Make some computation
}
model.addAttribute("resultArray", selectedtList);
return "display-items";
}
回答:
听起来像一个具有约束力的问题。您是否尝试过使用Spring的<form:checkbox>
标签而不是<spring:bind>
?它会自动生成复选框属性,并添加一个Spring用来确定该复选框是“打开”还是“关闭”的隐藏字段。
<form:form action="submitList" method="post" modelAttribute="myObjectForm"> <table>
<tbody>
<c:forEach items="${myObjectForm.myList}" var="row" varStatus="status">
<tr>
<td>
<form:checkbox path="myList[${status.index}].checkControl"/>
</td>
<td>${row.name}</td>
<td>${row.code}</td>
</tr>
</c:forEach>
</tbody>
</table>
<button type="submit">Submit</button>
</form:form>
以上是 Spring Mvc提交/删除表中的已检查(选定)记录 的全部内容, 来源链接: utcz.com/qa/401845.html