Spring重定向请求映射

时间:2012-03-23 10:15:01

标签: 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()
}

1 个答案:

答案 0 :(得分:6)

这样的事情怎么样:

@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";
}