Results 1 to 2 of 2

Thread: Spring foreach form with items

  1. #1
    Join Date
    Dec 2012
    Posts
    3

    Question 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.
    Last edited by skytang; Dec 15th, 2012 at 10:16 AM.

  2. #2
    Join Date
    Dec 2012
    Posts
    3

    Default

    I solved it by making an backing bean with an itemlist and adding the backing bean to the Model model.
    Then passing it through the controller to the jsp view.

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
  •