Java 在 Spring MVC 中重定向到外部 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18066134/
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 an external URL in Spring MVC
提问by Jake
In a Spring Controller Action, I am using the following statement to redirect to an external URL:
在 Spring Controller Action 中,我使用以下语句重定向到外部 URL:
String redirectUrl = "www.yahoo.com";
return "redirect:" + redirectUrl;
However, it appears that it's redirecting the url locally and not replacing the entire address bar URL with www.yahoo.com.
但是,它似乎是在本地重定向 url,而不是用 www.yahoo.com 替换整个地址栏 URL。
Ex: With the above redirection, my address bar now looks like:
例如:通过上述重定向,我的地址栏现在看起来像:
http://localhost/myApp/auth/www.yahoo.com
How do I resolve this? I even tried redirecting to a view and then having the view redirect the URL, but still the same result. The only way it seems to work is if I have http://www.yahoo.comor https://www.yahoo.comBut I wanted it to redirect the URL as specified and not necessarily mention the protocol. Ex: yahoo.com is similiar to http://www.yahoo.comif you go directly in the address bar.
我该如何解决?我什至尝试重定向到视图,然后让视图重定向 URL,但结果仍然相同。它似乎工作的唯一方法是如果我有http://www.yahoo.com或https://www.yahoo.com但我希望它按照指定重定向 URL 而不必提及协议。例如:如果您直接进入地址栏,yahoo.com 与http://www.yahoo.com类似。
Thanks
谢谢
采纳答案by Reimeus
The protocol is required if the host is different to that of the current host
如果主机与当前主机不同,则需要该协议
String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;
Have a look at the redirect: prefix
section from Spring Web MVC framework
看一看在redirect: prefix
从部分的Spring Web MVC框架
A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect:http://myhost.com/some/arbitrary/pathwill redirect to an absolute URL.
诸如 redirect:/myapp/some/resource 之类的逻辑视图名称将相对于当前 Servlet 上下文重定向,而诸如 redirect: http://myhost.com/some/arbitrary/path 之类的名称将重定向到绝对 URL。