我希望我的Spring MVC应用程序重定向到动态URL(由用户提交)。所以,如果我有这样的代码,
@RequestMapping("/redirectToSite")
protected ModelAndView redirect(
@RequestParam("redir_url") String redirectUrl,
HttpServletRequest request,
HttpServletResponse response)
{
// redirect to redirectUrl here
return ?
}
我应该写什么来重定向到提交的网址?例如,http://mySpringMvcApp/redirectToSite?redir_url=http://www.google.com
应重定向到Google。
答案 0 :(得分:83)
试试这个:
@RequestMapping("/redirectToSite")
protected String redirect(@RequestParam("redir_url") String redirectUrl)
{
return "redirect:" + redirectUrl;
}
这在Spring 16.5.3.2 The redirect: prefix的reference documentation中有解释。当然,您可以随时手动执行此操作:
response.sendRedirect(redirectUrl);
答案 1 :(得分:6)
@RequestMapping(value="/redirect",method=RequestMethod.GET)
void homeController(HttpServletResponse http){
try {
http.sendRedirect("Your url here!");
} catch (IOException ex) {
}
}