Spring 3 MVC Handle multiple forms with a controller.
I am developing a JSP page with several forms. 1) Customer search, 2) Product search, 3) Printing something, etc. I have another form binding object attached to each form, and my controller code is 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) {
The above, of course, does not work, as I expected. I get an exception saying "The request method" POST "is not supported . " If I have only one POST method inside the controller, let's say searchProducts it works well. But with POST there will be no more than one method. I also tried adding a hidden parameter to the JSP and changing the method signatures like below, just to get the same exception again.
@RequestMapping(method = RequestMethod.POST, params="pageAction=searchProduct") public ModelAndView searchProducts(@ModelAttribute("productSearch") ProductSearchCriteria productSearchCriteria, BindingResult result, SessionStatus status) {
Can anyone suggest the right way to achieve the above? Any reference to the source material or further reading will be greatly appreciated. Thanks.
EDIT # 1: The above approach with parameters = "pageAction = searchProduct" works fine as far as you can get your hidden parameter directly in JSP (see comment below). In addition to this, the answers @Bozho and @Biju Kunjummen are also very helpful and are a good (maybe better?) Alternative to resolving multiple submit forms.
java-ee spring-mvc controller
Tman
source share