I have noticed the following code is redirecting the User to a URL inside the project,
@RequestMapping(method = RequestMethod.POST)
public String processForm(HttpServletRequest request, LoginForm loginForm,
BindingResult result, ModelMap model)
{
String redirectUrl = "yahoo.com";
return "redirect:" + redirectUrl;
}
whereas, the following is redirecting properly as intended, but requires http:// or https://
@RequestMapping(method = RequestMethod.POST)
public String processForm(HttpServletRequest request, LoginForm loginForm,
BindingResult result, ModelMap model)
{
String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;
}
I want the redirect to always redirect to the URL specified, whether it has a valid protocol in it or not and do not want to redirect to a view. How can I do that?
Thanks,
In short
"redirect:yahoo.com"
or"redirect://yahoo.com"
will lend you toyahoo.com
.where as
"redirect:yahoo.com"
will lend youyour-context/yahoo.com
ie for ex-localhost:8080/yahoo.com
You can use the
RedirectView
. Copied from the JavaDoc:Example:
You can also use a
ResponseEntity
, e.g.And of course, return
redirect:http://www.yahoo.com
as mentioned by others.Looking into the actual implementation of UrlBasedViewResolver and RedirectView the redirect will always be contextRelative if your redirect target starts with /. So also sending a //yahoo.com/path/to/resource wouldn't help to get a protocol relative redirect.
So to achieve what you are trying you could do something like:
Did you try RedirectView where you can provide the contextRelative parameter?
For me works fine:
For external url you have to use "http://www.yahoo.com" as the redirect url.
This is explained in the redirect: prefix of Spring reference documentation.
will redirect relative to the current Servlet context, while a name such as
will redirect to an absolute URL