Multiple forms in one jsp

Skyverian picture Skyverian · Jan 3, 2013 · Viewed 11.6k times · Source

I have two form on one jsp page. The first form doesn't use the modelAttribute and the second one uses an modelAttribute. What the problem is, is that if I post the first form which I doesn't use the modelAttribute will claim an error that I haven't bind the modelAttribute.

I have searched on the internet to look for solutions but I can't find one which was helpfull.

changeAddress.jsp

<form method="post">
     <input type="hidden" name="id" value="0" />
     <input type="submit" name="exist" value="send to this address" />
</form>
<form:form method="post" modelAttribute="addressForm">
     <form:input path="street" />     
     <input type="submit" name="add" value="send to this address" />  
</form:form>

OrderController.java

@RequestMapping(value="changeAddress",method = RequestMethod.GET)
public ModelAndView showChangAddress(Model model)
{
     model.addAttribute("addressForm", new AddressForm());
     return new ModelAndView("body.changeaddress");
}

@RequestMapping(value="changeAddress", params="add", method = RequestMethod.POST)
public ModelAndView addChangAddress(@ModelAttribute("addressForm") @Valid AddressForm af, BindingResult result, Model model)
{
     System.out.println("a");
     return new ModelAndView("body.changeaddress");
}

@RequestMapping(value="changeAddress", params="exist", method = RequestMethod.POST)
public ModelAndView processChangAddress(@RequestParam(value="id") String id, Model model)
{
     System.out.println("b");
     return new ModelAndView("body.changeaddress");
}

Much appriciated for help :)

Answer

micha picture micha · Jan 3, 2013

The spring form taglib documentation about the <form> tag:

This tag renders an HTML 'form' tag and exposes a binding path to inner tags for binding. It puts the command object in the PageContext so that the command object can be accessed by inner tags.

I think you don't need anything from the spring <form> tag in your first form. So you can use a simple html form instead:

<form method="post" action="...">
     <input type="hidden" name="id" value="0" />
     <input type="submit" name="exist" value="send to this address" />
<form>