Spring foreach form with items
After much searching through the internet I really can't find an solution.
productlist.jsp
Code:
<c:forEach var="item" items="${itemList}" varStatus="status">
<form:form commandName="itemList[$status.index}]">
<form:hidden path="id"/>
<input type="submit" class="addCartBtn" value="Add to cart" />
</form:form>
</c:forEach>
Controller.class
Code:
HashMap<String, Object> myModel = new HashMap<String, Object>();
@RequestMapping(method = RequestMethod.GET)
public ModelAndView getProductList(Model model)
{
Collection<Item> itemList = getCategorieProducts();
model("itemList", itemList);
return new ModelAndView("productList");
}
@RequestMapping(method = RequestMethod.POST)
public addCart(@ModelAttribute("item") Item item, Model model){
//// Item process code
return new ModelAndView("addCart");
}
I have looked into many pages but there was no solution to use form:hidden.
Though there is an alternative solution that i know like typing the XHTML
<input type="hidden" name="id" value="${item.id}" />
but I really want to send and object through the POST method to receive it.