Spring 3 MVC Handle multiple form submit with a Controller.
I am developing JSP page with multiple forms. 1) Search Customer, 2) Search Product, 3) Print Something etc. I've a different form bind object tied to each form and my controller code looks similar to below
@Controller
@RequestMapping(value="/search.do")
public class SearchController {
@RequestMapping(method = RequestMethod.GET)
public String pageLoad(ModelMap modelMap) {
modelMap.addAttribute("productSearch", new ProductSearchCriteria());
modelMap.addAttribute("customerSearch", new CustomerSearchCriteria());
modelMap.addAttribute("print", new PrintForm());
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView searchProducts(@ModelAttribute("productSearch") ProductSearchCriteria productSearchCriteria,
BindingResult result, SessionStatus status) {
//Do Product search
return modelAndView;
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView searchCustomers(@ModelAttribute("customerSearch") CustomerSearchCriteria customerSearchCriteria,
BindingResult result, SessionStatus status) {
//Do Customer search
return modelAndView;
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView printSomething(@ModelAttribute("print") PrintForm printForm,
BindingResult result, SessionStatus status) {
//Print something
return modelAndView;
}
}
Above certainly doesn't work as I assumed it would. I get exception saying 'Request method 'POST' not supported'. If I have only one POST method inside above controller say searchProducts it works well. But it won't with more than one methods with POST. I also tried adding hidden parameter in JSP and changing method signatures similar to below only to get the same exception again.
@RequestMapping(method = RequestMethod.POST, params="pageAction=searchProduct")
public ModelAndView searchProducts(@ModelAttribute("productSearch") ProductSearchCriteria productSearchCriteria,
BindingResult result, SessionStatus status) {
//Do Product search
return modelAndView;
}
Can anyone please suggest correct way to achieve above? Also any reference to source material or further reading will be greatly appreciated. Thanks.
EDIT #1: The above approach with params="pageAction=searchProduct" works perfectly as far as you get your hidden parameter right in JSP (see comment below). In addition to that, answers by @Bozho and @Biju Kunjummen is also very helpful and a good (possibly better?) alternative to tackle multiple form submit.