I have a Spring MVC controller like this (with Spring 5.X version in case needed) for displaying and processing a form as shown below:
@Controller
@RequestMapping("/map")
public class LocalMapController {
// display the form
@RequestMapping(method = RequestMethod.GET)
public ModelAndView list() {
ModelAndView mav = new ModelAndView();
mav.setViewName("map");
mav.addObject("LocalMapForm", new LocalMapForm());
return mav;
}
// process the form
@RequestMapping(method = RequestMethod.POST)
public ModelAndView process(ModelAndView mav,
@ModelAttribute("LocalMapForm") LocalMapForm lmForm, BindingResult result)
throws Exception {
mav.setViewName("map");
//mav.setViewName("message");
//mav.addObject("successMsg", "Saved");
return mav;
}
}
map.jsp
is where I have the form code defined and that’s hitting this controller when submit button is clicked. So when I have the following lines in place in the POST method:
//mav.setViewName("map");
mav.setViewName("message");
mav.addObject("successMsg", "Saved");
After reaching the POST method, it get’s redirected to message view ( I have message.jsp
defined which contains <div class="message">${successMsg}</div>
) with a message “Saved”.
Now, I don’t want to redirect to another page as above and keep it to map
page only where the form is present. Am I doing correct in the code shared above where I have the following defined?
mav.setViewName("map");
//mav.setViewName("message");
//mav.addObject("successMsg", "Saved");
return mav;
Should it stay on the same page after the submit button is clicked?