Announcement Announcement Module
Collapse
No announcement yet.
Spring foreach form with items Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 11:16 AM.

  • #2
    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.

    Comment

    Working...
    X