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.
Controller.java
productList.jspCode:@Controller @RequestMapping("/cart") 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"); } }
Backingbean.javaCode:/// 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="itemTag">${item.name}</div> <a href="<c:url value="/product/${item.name}.html" />" class="moreInfo" ><span>Meer informatie</span></a> <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" /> </form:form> </div> </c:forEach>
I don't really know what the problem is why it isn't giving me the correct data it passed through the POST.Code:public class SearchForm implements Serializable { private Collection<Item> items; private String term; // getters and setters }
Many thanks.


Reply With Quote
