Spring,使用POST重定向到外部URL [英] Spring, redirect to external url using POST

查看:474
本文介绍了Spring,使用POST重定向到外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the following Spring 3.1 action, I've to do some stuff and add attribute to a POST request, and then redirect it to external URL through POST (I can't use 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方法(属性会附加到< a href =http://www.externalURL.com/ =noreferrer> http://www.externalURL.com/ )。我该如何使用POST方法?这是外部URL的强制性。

But with this code the GET method is used (the attributes are appended to http://www.externalURL.com/). How can I use the POST method? It's mandatory from the external URL.

推荐答案

像@stepanian所说的那样,你不能用POST重定向。
但是有一些解决方法:

Like @stepanian said, you can't redirect with POST. But there are few workarounds:


  1. 做一个简单的HttpUrlConnection并使用POST。输出响应流后。它有效,但我遇到了一些CSS问题。

  2. 在控制器中执行操作并将结果数据重定向到虚假页面。此页面将通过javascript自动执行POST,无需用户交互(更多细节):

  1. Do a simple HttpUrlConnection and use POST. After output the response stream. It works, but I had some problem with CSS.
  2. Do stuff in your controller and after redirect the result data to a fake page. This page will do automatically the POST through javascript with no user interaction (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>

这篇关于Spring,使用POST重定向到外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆