Write handler methods for dynamically generated buttons and text boxes

356 Views Asked by At

I have this small data on JSP form. I use JSTL to iterate over data. In each row, I have two dynamically generated textboxes and a button. Question is: how do I write a generic handler method for this data, which handles dynamically generated textboxes and buttons. Here is my JSP

<c:forEach items="${menus}" var="menu" > 
 <tr>
    <td align="left" >${menu.getMenuId()}</td>
    <td align="right"><input type="text" name="menu_name" value="${menu.getMenuName()}"/></td>
    <td align="right"><input type="text" name="menu_price" value="${menu.getMenuPrice()}"/></td>
    <td align="right"><c:out value="${menu.getIsAvailable()}" /></td>
    <td align="right"><input type="submit" value="Add Item ${menu.getMenuId()}"></td>
  </tr>
</c:forEach>

 <h4>Add Product</h4>
 Name: <input type="text" name="chosen_menu_name" />
 Price: <input type="text" name="chosen_menu_price" />
 <input type="submit" value="Add to cart">

And here is my controller (though I dont know what to put it there - at the moment I am using two seperate textbox and a button for taking the input)

@RequestMapping(method = RequestMethod.POST)
public ModelAndView AddMenu(@RequestParam("chosen_menu_name") String mymenu, @RequestParam("chosen_menu_price") String menu_price, @ModelAttribute("cart") ArrayList<Menu> mycart, Model model)
{
    Menu menu = new Menu();
    menu.setMenuId(0);
    menu.setMenuName(mymenu);
    menu.setMenuPrice(Double.parseDouble(menu_price));


    model.addAttribute("menus", GetMenus());
    mycart.add(menu);

    return new ModelAndView("edit_menu");        
    //return "show_menu";
}

As one can see from the JSP, I am using two seperate textboxes and a button for taking input and passing it to the controller. How do I write a generic handler method for this data, which handles dynamically generated textboxes and buttons?

1

There are 1 best solutions below

2
On

I'm assuming that you don't want those other two field and button. You want to add items directly from table.
You have to make each and every row as one form like below :

<c:forEach items="${menus}" var="menu" > 
<tr>
<form method="POST" action="controllerName">
    <td align="left">
        ${menu.getMenuId()}
        <input type="hidden" name="menu_id" value="${menu.getMenuId()}"/>
    </td>
    <td align="right">
        <input type="text" name="menu_name" value="${menu.getMenuName()}"/>
    </td>
    <td align="right">
        <input type="text" name="menu_price" value="${menu.getMenuPrice()}"/>
    </td>
    <td align="right">
        <c:out value="${menu.getIsAvailable()}"/>
    </td>
    <td align="right">
        <input type="submit" value="Add Item">
    </td>
</form>
</tr>
</c:forEach>

And controller like below:

@RequestMapping(method = RequestMethod.POST)
public ModelAndView AddMenu(@RequestParam("menu_id") String menuId, @RequestParam("menu_name") String mymenu, @RequestParam("menu_price") String menu_price, @ModelAttribute("cart") ArrayList<Menu> mycart, Model model)
{
    Menu menu = new Menu();
    menu.setMenuId(menuId);
    menu.setMenuName(mymenu);
    menu.setMenuPrice(Double.parseDouble(menu_price));

    model.addAttribute("menus", GetMenus());
    mycart.add(menu);

    return new ModelAndView("edit_menu");        
    //return "show_menu";
}