Java – Spring redirect request mapping

javaspringspring-mvc

I want to redirect my controller methods, so that I can submit a form with one url and then display a default url in browser, somethign like this do I need to change the return type postForms, create a new model and view/redirectview :

@RequestMapping(value = "/Home", method = RequestMethod.GET)
public String getHome( Model model){
  //view name append with .jsp
  return "myHome"; 
}

@RequestMapping(value = "/FormA", method = RequestMethod.POST)
public String postFormA(Email Email, Model model){
  //do stuff then
  //redirect to different requestMapping broswer url "/Home"
  getHome()    
}

@RequestMapping(value = "/FormB", method = RequestMethod.POST)
public String postFormB( Model model){
  //do stuff then
  //redirect to different requestMapping and display in broswer url "/Home"
  getHome()
}

Best Answer

How about something like this:

@RequestMapping(value = "/Home", method = RequestMethod.GET)
public ModelAndView getHome(){
  //view name append with .jsp
  return new ModelAndView("myHome"); 
}

@RequestMapping(value = "/FormA", method = RequestMethod.POST)
public String postFormA(Email Email, Model model){
  //do stuff then
  //redirect to different requestMapping broswer url "/Home"
  return "redirect:/Home";
}

@RequestMapping(value = "/FormB", method = RequestMethod.POST)
public String postFormB( Model model){
  //do stuff then
  //redirect to different requestMapping and display in broswer url "/Home"
  return "redirect:/Home";
}