从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;

    }

我希望重定向始终重定向到指定的URL,无论它是否具有有效的协议,并且都不想重定向到视图。我怎样才能做到这一点?


慕容森
浏览 2374回答 3
3回答

吃鸡游戏

您可以通过两种方式来实现。第一:@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);}
打开App,查看更多内容
随时随地看视频慕课网APP