Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

java - Spring, redirect to external url using POST

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);
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

  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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...