user1071914
user1071914

Reputation: 3393

Change a Spring MVC redirect from a GET to a POST?

I'm doing a redirect from a Spring MVC controller by returning a String containing the URL:

return "redirect:/my/form/newpage.html?pid=".concat(myform.getId().toString());

this gives a string like this:

redirect:/my/form/newpage.html?pid=456

The trouble is, the Spring ModelFactory class appends all our session attributes to the query string and it looks horrible. I'd really like to change this redirect from a GET to a POST, but I have no idea how to do that. Can anyone help?

Upvotes: 1

Views: 4655

Answers (1)

Oskar Dajnowicz
Oskar Dajnowicz

Reputation: 1770

You can't really change the HTTP Method of redirect but

you can try this to avoid exposing variables to path (instead these explicitly added like pid):

public ModelAndView redirectToSomewhere() {
    RedirectView redirectView = new RedirectView("/my/form/newpage.html?pid=".concat(myform.getId().toString());
    redirectView.setExposeModelAttributes(false); // these
    redirectView.setExposePathVariables(false); //two depend on the way you set your variables
    return new ModelAndView(redirectView);
}

Upvotes: 2

Related Questions