Results 1 to 1 of 1

Thread: Spring MVC @ModelAttribute getting filled Entity

  1. #1
    Join Date
    Dec 2012
    Posts
    3

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

    Controller.java
    Code:
    @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");
       }
    }
    productList.jsp
    Code:
    /// 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>
    Backingbean.java
    Code:
    public class SearchForm implements Serializable
    {
       private Collection<Item> items;
       private String term;
       
       // getters and setters
    }
    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.
    Last edited by skytang; Dec 17th, 2012 at 08:09 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •