我注意到以下代码将用户重定向到项目内的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; }我希望重定向始终重定向到指定的URL,无论它是否具有有效的协议,并且都不想重定向到视图。我怎样才能做到这一点?
3 回答
吃鸡游戏
TA贡献1829条经验 获得超7个赞
您可以通过两种方式来实现。
第一:
@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);
}
添加回答
举报
0/150
提交
取消