【问题标题】:Spring, redirect to external url using POSTSpring,使用 POST 重定向到外部 url
【发布时间】:2016-04-01 10:07:23
【问题描述】:

在下面的 Spring 3.1 操作中,我要做一些事情并为 POST 请求添加属性,然后通过 POST 将其重定向到外部 URL(我不能使用 GET)。

@RequestMapping(value = "/selectCUAA", method = RequestMethod.POST)
public ModelAndView selectCUAA(@RequestParam(value="userID", required=true) String cuaa, ModelMap model) {
    //query & other...
    model.addAttribute(PARAM_NAME_USER, cuaa);
    model.addAttribute(... , ...);
    return new ModelAndView("redirect:http://www.externalURL.com/", model);
}

但是在这段代码中,使用了 GET 方法(属性附加到http://www.externalURL.com/)。如何使用 POST 方法?它是来自外部 URL 的必填项。

【问题讨论】:

标签: java spring spring-mvc redirect


【解决方案1】:

就像@stepanian 所说,您不能使用 POST 重定向。 但解决方法很少:

  1. 做一个简单的 HttpUrlConnection 并使用 POST。输出响应流后。它可以工作,但我在使用 CSS 时遇到了一些问题。
  2. 在你的控制器中做一些事情,然后将结果数据重定向到一个假页面。此页面将通过 javascript 自动执行 POST,无需用户交互 (more details):

html:

<form name="myRedirectForm" action="https://processthis.com/process" method="post">
    <input name="name" type="hidden" value="xyz" />
    <input name="phone" type="hidden" value="9898989898" />
    <noscript>
        <input type="submit" value="Click here to continue" />
    </noscript>
</form>
    <script type="text/javascript">

        $(document).ready(function() {
            document.myRedirectForm.submit();
        });

    </script>

【讨论】:

【解决方案2】:

您不能使用 POST 进行重定向。您可以在操作中使用带有HttpURLConnection 之类的类的Java 代码发送POST 请求。

【讨论】:

  • 好的,谢谢,我使用了这个例子:stackoverflow.com/questions/3324717/…我的 Spring 控制器 selectCUAA 现在应该返回什么?我需要重定向到新网页。
  • 在code-envoked POST完成后可以重定向。
  • 无属性重定向?这样是不是就像调用一个帖子和一个get之后,所以结果只会是最后一个get? (新的 url 是一个登录页面,需要 post 上的参数)。不过谢谢,星期一我会试一试
  • 可以从post中获取response,在get response中返回给用户。
  • 我在网上看了看,但我变得更加困惑。你能给我看一个修改我的 selectCUAA 控制器的例子吗?
猜你喜欢
  • 2020-07-23
  • 2019-05-02
  • 2012-04-06
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多