[英]How do I bind item from List to form modelAttribute using Spring MVC
I have searched and couldnt find anything similar or i am serching for the wrong thing. 我已经搜索过并且找不到类似的东西,或者我正在寻找错误的东西。 I am returning a list of items from my Controller for display in my jsp. 我从控制器返回了一个项目列表,以显示在我的jsp中。 In my jsp table I would like to have a row for each item in my list, something like this: 在我的jsp表中,我想为列表中的每个项目设置一行,如下所示:
<tbody>
<c:forEach items="${productList}" var="product" varStatus="status">
<tr>
???? This next line is seudo-code. I dont know how to bind the item to form ???
<form:form method="post" modelAttribute="${productList}[status.index]">
<td><form:input path="price" class="input-mini" type="text" /></td>
<td><button id="save" name="save"></td>
</form
</tr>
</c:forEach>
</tbody>
then my controller would have a RequestMethod.POST for handleing the save action. 那么我的控制器将具有RequestMethod.POST来处理保存操作。 Is this possible? 这可能吗? If so could someone help point me in the right direction. 如果可以的话,有人可以帮助我指出正确的方向。
Is this possible I am not sure how to bind the item in the list to the form. 我不确定如何将列表中的项目绑定到表单吗?
Following the suggestion from @tofindabhishek and still wanting to allow editing per row I ended up implementing a solution with in-row buttons for saving, editing and deleting for each row and pass the item id on post back to the controller. 遵循@tofindabhishek的建议,但仍然希望允许对每一行进行编辑,我最终实现了一个解决方案,该方法带有行内按钮,用于保存,编辑和删除每一行,并将项目ID传递回控制器。 Here is my table body. 这是我的桌子主体。 Its using datatables, bootstrap and opens a modal for the full edit form. 它使用数据表,引导并打开完整编辑表单的模式。 Altogether provides a very rich CMS IMO: 总共提供了非常丰富的CMS IMO:
<tbody>
<c:forEach items="${productManagerForm.products}" var="product" varStatus="status">
<c:url value="/product/detail/${product.id}" var="detailUrl" />
<tr>
<td><a href="${detailUrl}">${product.id}</a> <form:hidden path="products[${status.index}].id" value="${product.id}" /></td>
<td><form:input path="products[${status.index}].name" class="input-xlarge" type="text"/></td>
<td><form:input path="products[${status.index}].price" class="input-mini" type="text" /></td>
<td><form:input path="products[${status.index}].shippingPrice" class="input-mini" type="text" /></td>
<td><button id="save" name="save" value="${product.id}" class="btn btn-success"><i class="fa fa-save"></i> Save </button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#editProduct${product.id}"><i class="fa fa-edit"></i> Edit</button>
<button id="delete" name="delete" value="${product.id}" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
</c:forEach>
</tbody>
Here is on of my POST handlers, from this you can see how I used the RequestMapping to map the handler and the RequestParam to bind the Id: 这是我的POST处理程序中的内容,从中您可以看到我如何使用RequestMapping映射处理程序以及如何使用RequestParam绑定ID:
@RequestMapping(method = RequestMethod.POST, params = "delete")
public String deleteProduct(@RequestParam(value = "delete") int deleteProductId) {
Product product = productService.findProduct(deleteProductId);
productService.deleteProduct(product);
...
}
<tbody>
<form:form method="post" modelAttribute="${productList}">
<c:forEach items="${productList}" var="product" varStatus="status">
<tr>
<td><form:input path="{productList[[${status.index}].price}" class="input-mini" type="text" /></td>
<td><button id="save" name="save"></td>
</form
</tr>
</c:forEach>
</tbody>
this code would submit the form along with product List,in post you need to write logic to save product list. 此代码将与产品列表一起提交表单,在发布后,您需要编写逻辑来保存产品列表。 for further help you can refer following link. 要获得更多帮助,您可以参考以下链接。
( http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/ ) ( http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/ )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.