Spring重定向请求映射

Nim*_*sky 2 java spring spring-mvc

我想重定向我的控制器方法,以便我可以提交一个带有一个URL的表单,然后在浏览器中显示默认URL,像这样的事情我需要更改返回类型postForms,创建一个新模型并查看/ 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()
}
Run Code Online (Sandbox Code Playgroud)

cra*_*man 8

这样的事情怎么样:

@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";
}
Run Code Online (Sandbox Code Playgroud)