在 Spring MVC 中重定向到动态 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9311940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to dynamic URL in Spring MVC
提问by Gruber
I want my Spring MVC application to redirect to a dynamic URL (submitted by the user). So if I have code like this,
我希望我的 Spring MVC 应用程序重定向到一个动态 URL(由用户提交)。所以如果我有这样的代码,
@RequestMapping("/redirectToSite")
protected ModelAndView redirect(
@RequestParam("redir_url") String redirectUrl,
HttpServletRequest request,
HttpServletResponse response)
{
// redirect to redirectUrl here
return ?
}
what should I write to redirect to the submitted URL? For instance http://mySpringMvcApp/redirectToSite?redir_url=http://www.google.comshould redirect to Google.
我应该写什么来重定向到提交的 URL?例如http://mySpringMvcApp/redirectToSite?redir_url=http://www.google.com应该重定向到谷歌。
回答by Tomasz Nurkiewicz
Try this:
尝试这个:
@RequestMapping("/redirectToSite")
protected String redirect(@RequestParam("redir_url") String redirectUrl)
{
return "redirect:" + redirectUrl;
}
This is explained in 16.5.3.2 The redirect: prefixof Spring reference documentation. Of course you can always do this manually:
这在16.5.3.2 The redirect: prefixof Spring reference documentation 中进行了解释。当然,您始终可以手动执行此操作:
response.sendRedirect(redirectUrl);
回答by Airton Bruno B Rivero
@RequestMapping(value="/redirect",method=RequestMethod.GET)
void homeController(HttpServletResponse http){
try {
http.sendRedirect("Your url here!");
} catch (IOException ex) {
}
}

