在Spring MVC中重定向到动态URL
我希望我的Spring MVC应用程序重定向到动态URL(由用户提交)。 所以如果我有这样的代码,
@RequestMapping("/redirectToSite")
protected ModelAndView redirect(
@RequestParam("redir_url") String redirectUrl,
HttpServletRequest request,
HttpServletResponse response)
{
// redirect to redirectUrl here
return ?
}
我应该写些什么来重定向到提交的URL? 例如http://mySpringMvcApp/redirectToSite?redir_url=http://www.google.com
应该重定向到Google。
Gruber asked 2020-08-11T02:00:47Z
2个解决方案
83 votes
试试这个:
@RequestMapping("/redirectToSite")
protected String redirect(@RequestParam("redir_url") String redirectUrl)
{
return "redirect:" + redirectUrl;
}
在16.5.3.2 Spring参考文档的redirect:前缀中对此进行了解释。 当然,您始终可以手动执行此操作:
response.sendRedirect(redirectUrl);
7 votes
@RequestMapping(value="/redirect",method=RequestMethod.GET)
void homeController(HttpServletResponse http){
try {
http.sendRedirect("Your url here!");
} catch (IOException ex) {
}
}