从Spring MVC中的控制器操作重定向到外部URL
我注意到以下代码将用户重定向到项目内的URL,
@RequestMapping(method = RequestMethod.POST)public String processForm(HttpServletRequest request, LoginForm loginForm,
BindingResult result, ModelMap model)
{
String redirectUrl = "yahoo.com";
return "redirect:" + redirectUrl;
}
然而,以下内容已按预期正确重定向,但需要http://或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;
}
我希望重定向始终重定向到指
回答:
你可以通过两种方式来实现。
第一:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Location", projectUrl);
httpServletResponse.setStatus(302);
}
第二:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}
以上是 从Spring MVC中的控制器操作重定向到外部URL 的全部内容, 来源链接: utcz.com/qa/419721.html