简体   繁体   中英

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. In my jsp table I would like to have a row for each item in my list, something like this:

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

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

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

@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/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM