java 如何使用 Spring MVC 将 List 中的项绑定到表单 modelAttribute
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27052236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I bind item from List to form modelAttribute using Spring MVC
提问by brad12s
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. In my jsp table I would like to have a row for each item in my list, something like this:
我已经搜索过但找不到任何类似的东西,或者我正在寻找错误的东西。我正在从我的控制器返回一个项目列表以在我的 jsp 中显示。在我的 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. Is this possible? If so could someone help point me in the right direction.
那么我的控制器会有一个 RequestMethod.POST 来处理保存操作。这可能吗?如果是这样,有人可以帮助我指出正确的方向。
Is this possible I am not sure how to bind the item in the list to the form.
这可能吗我不确定如何将列表中的项目绑定到表单。
回答by brad12s
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. 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:
按照@tofindabhishek 的建议,仍然希望允许每行编辑,我最终实现了一个带有行内按钮的解决方案,用于保存、编辑和删除每一行,并将项目 id 传递回控制器。这是我的桌子主体。它使用数据表、引导程序并打开完整编辑表单的模式。总共提供了一个非常丰富的 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);
...
}
回答by tofindabhishek
<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/)