0

I am trying to fire onClick event and Spring controller one by one. In the click event of the button, I set a hidden field and then I want the controller to be executed AND the hidden field to be available in it. Here is my code for the JSP and the Spring Controller

JSP

<html:hidden property="MenuID" name="MenuID" id="hidMenuID" />
<c:forEach items="${menus}" var="menu" > 
<tr class="cartItem">
   <td align="left" >${MenuId}</td>
   td align="right"><input type="text" name="menu_name" value="${Name}"/></td>
   <td align="right"><input type="text" name="menu_price" value="${MenuPrice}"/></td>
   <td align="right"><input type="submit" class="button" name="add_menu" value="Add" onclick="return SetMenuID();"></td>
   <td align="right"><input type="submit" class="button" name="remove_menu" value="Remove" onclick="return SetMenuID();"></td>
</tr>
</c:forEach>  

JavaScript

<script type="text/javascript">
    function SetMenuID()
    {  
        var MenuID = document.getElementById('hidMenuID');    
        MenuID.value = 'The new value';                
        return true;
    }
</script>

Spring MVC

@RequestMapping(params = "add_menu", method = RequestMethod.POST)
public ModelAndView AddMyMenu(@RequestParam("MenuID") String menu_id, Model model, @ModelAttribute("cart") ArrayList<Menu> mycart)
{
    int nMenuId = Integer.parseInt(menu_id);
    Menu menu = m_arrMenus.get(nMenuId);

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

    return new ModelAndView("edit_menu");     
}

As you might guess, I am populating data from database. I have an onClick function associated with each ADD button. I set hidden field in thid function and want it to be available in my controller. But a runtime exception comes that

Error Status 400 - Required string MenuID is not present

What should I do to achieve similar result ? Thanks for any input.

Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
Muzammil
  • 11
  • 1
  • 7

1 Answers1

0

@RequestParam expects a parameter my name in the request url. Since your form is submitted and there are no request param that the action recieves. This is a reason you are getting 400 http error from server.

See the difference between @RequestParam vs @PathVariable

So either change the @RequestParam annotation or add (required = false) to it.

Community
  • 1
  • 1
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23
  • Yes. After adding `(required = false)`, I dont get HTTP 400 error anymore. But the value is NULL and I want to set it **BEFORE** the form gets submitted , in the `onclick()` event and be available in the controller method. How could I do that ?? – Muzammil Aug 08 '15 at 06:04
  • just change `@RequestParam("MenuID") String menu_id` to String `menu_id`. it should work – Ekansh Rastogi Aug 08 '15 at 06:14
  • I changed it to `public ModelAndView AddMyMenu(String menu_id, Model model....) { some code}` but still showing null value. Am I missing something or just a dumb to not to get you ?? – Muzammil Aug 08 '15 at 06:22
  • in your form your text field name is `MenuID` and your cannot bind it to `menu_id`, change it to `MenuID` in controller or vice versa – Ekansh Rastogi Aug 08 '15 at 15:23