我注意到以下代碼將用戶重定向到項目內的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
提交
取消