spring3 redirect(重定向)

第一种:
@RequestMapping(value = "/index.html", method = RequestMethod.GET) 
public String index(Model model)  { 
    return second(model); 
} 
 
@RequestMapping(value = "/second.html", method = RequestMethod.GET) 
public String second(Model model)  { 
    //put some staff in model 
    return "second"; 
} 


第二种:
@RequestMapping(value = "/index.html", method = RequestMethod.GET) 
public View index(Model model)  { 
    return new RedirectView("second.html"); 
} 
 
@RequestMapping(value = "/second.html", method = RequestMethod.GET) 
public String second(Model model)  { 
    //put some staff in model 
    return "second"; 
}


第三种:
@RequestMapping(value = "/rate", method = RequestMethod.POST) 
public String rateHandler(HttpServletRequest request) { 
    //your controller code 
    String referer = request.getHeader("Referer"); 
    return "redirect:"+ referer; 
} 

其中,referer可为:
  • 外部URL。如:"redirect:http://www.baidu.com/",重定位后会打开http://www.baidu.com/
  • 绝对路径。如:"redirect:/redirect/re",如果当前URL为“localhost:8180/spring3/home/redirect”,则重定位后的URL为“http://localhost:8180/spring3/redirect/re”。
  • 相对路径。如:"redirect:compare?input1=123&input2=32",如果当前URL为“localhost:8180/spring3/home/redirect2/re”,则重定位后的URL为“http://localhost:8180/spring3/home/redirect2/compare?input1=123&input2=32”


参考:
http://stackoverflow.com/questions/5077783/redirect-in-controllers-spring3
http://stackoverflow.com/questions/804581/spring-mvc-controller-redirect-to-previous-page

你可能感兴趣的:(redirect)