TAGS :Viewed: 3 - Published at: a few seconds ago

[ Spring MVC @ModelAttribute getting filled Entity ]

Today I am stuck with the spring-form with the POST method which doesn't give posted item to the Controller which I wanted. Here is my code.


public class CartController extends CommonController
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addCart(@ModelAttribute("productList") Item item, BindingResult result,Model model){
         System.out.println(item.getId()); /// <-- doesn't gives me the ID
         return new ModelAndView("cart");


/// Loop through the products of search itemlist and generates the forms with the correct items
<c:forEach var="item" items="${productList.items}" varStatus="status">
        <div class="addCart">
        <c:url value="/cart/add.html" var="addURL" />
            <form:form method="POST" action="${addURL}" modelAttribute="productList">
                <form:hidden path="items[${status.index}].id"/>
                <input type="submit" class="addCartBtn" value="Add to cart" />


public class SearchForm implements Serializable
   private Collection<Item> items;
   private String term;
   // getters and setters

The ${productList} is the backingbean which loops through all items.

I don't really know what the problem is why it isn't giving me the correct data it passed through the POST. Many thanks.

Answer 1

Covert your spring:hidden tag to normal html hidden tag:

<form:hidden path="items[${status.index}].id"/>


<input type="hidden" name="id" value="${item.id}"/>