【问题标题】:Redirect to external link using spring controller [duplicate]使用弹簧控制器重定向到外部链接[重复]
【发布时间】:2014-07-04 21:28:31
【问题描述】:

我遇到了一个案例,我必须将链接从特定控制器重定向到外部 url。我尝试过很多类似的案例

response.sendRedirect(url_location);

也尝试过类似的方法

response.setHeader("location",url_location);

也尝试将返回类型作为模型和视图发送。但没有任何结果。谁能帮我解决它。我看到很多建议使用 sendRedirect 和 setHeader 方法,这在我的情况下不起作用。所以我也想知道使用这些HTTPServeletResponse的方法是否需要进行任何配置。

【问题讨论】:

标签: java spring http jakarta-ee


【解决方案1】:
 @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public ModelAndView method() {
            return new ModelAndView("redirect:" + projectUrl);

    }

【讨论】:

    【解决方案2】:

    如何尝试

    return new ModelAndView("redirect:" + url_location);
    

    return "redirect:" + url_location;
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

      @RequestMapping(value = "/redirect", method = RequestMethod.GET)
      public ModelAndView method() {
              return new ModelAndView("redirect:" + redirectUrl);
      
      }
      

      或者

       @RequestMapping(value = "/redirect", method = RequestMethod.GET)
      public void method(HttpServletResponse httpServletResponse) {
          httpServletResponse.setHeader("Location", redirectUrl);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        相关资源
        最近更新 更多