milkphany
milkphany

Reputation: 145

Spring redirect keeping parameters after POST

I am using spring 3.2.4 and I have a simple controller that takes a parameter, set it, and redirects the page. For some reason, it is keeping the param even after redirecting. i.e. The page starts at "/" and ends with "/?globalMessage=hereforever", I have tried clearing the modelMap, but that didn't work. I may be misunderstanding something along the lines. I am also adding the model to postHandle in HandlerInterceptor.

@RequestMapping(value = "globalMessage", method = RequestMethod.POST)
public String setGlobalMessage(@RequestParam String globalMessage) {
    globalProperties.setProperty("globalMessage", globalMessage);

    return "redirect:/";
}

Here is the jsp code in the front

<form method="post" action="/globalMessage">
    <input name="globalMessage" type="text" name="message"/>
    <input id="submitbutton" type="submit"/>
</form>

Upvotes: 2

Views: 7644

Answers (1)

Will Keeling
Will Keeling

Reputation: 22994

Spring's redirect view will automatically expose model attributes as URL parameters: https://jira.springsource.org/browse/SPR-1294. If the globalMessage is set in the model somewhere, then this will get appended to the redirect URL.

You can tell Spring not to do this using the setExposeModelAttributes method.

@RequestMapping(value = "globalMessage", method = RequestMethod.POST)
public String setGlobalMessage(@RequestParam String globalMessage) {
    globalProperties.setProperty("globalMessage", globalMessage);

    RedirectView redirect = new RedirectView("/");
    redirect.setExposeModelAttributes(false);
    return redirect;
}

Upvotes: 4

Related Questions