views:

469

answers:

3

I'm trying to figure out how to "preserve" the BindingResult so it can be used in a subsequent GET via the Spring <form:errors> tag. The reason I want to do this is because of Google App Engine's SSL limitations. I have a form which is displayed via HTTP and the post is to an HTTPS URL. If I only forward rather than redirect then the user would see the https://whatever.appspot.com/my/form URL. I'm trying to avoid this. Any ideas how to approach this?

Below is what I'd like to do, but I only see validation errors when I use return "create".

@RequestMapping(value = "/submit", method = RequestMethod.POST)
public final String submit(
    @ModelAttribute("register") @Valid final Register register,
    final BindingResult binding) {

    if (binding.hasErrors()) {
        return "redirect:/register/create";
    }

    return "redirect:/register/success";
}
A: 
Ichiro Furusato
I will give this a try later today, but it already works fine if I forward. I'm trying to get it to work when I redirect.
Taylor Leese
Understood, I was only suggesting the forward if after adding the BindingResult you don't actually see it.
Ichiro Furusato
I just tried it and it's not working on a redirect still.
Taylor Leese
A: 

The problem is you're redirecting to a new controller, rather than rendering the view and returning the processed form page. You need to do something along the lines of:

String FORM_VIEW = wherever_your_form_page_resides

...

if (binding.hasErrors())
    return FORM_VIEW;

I would keep the paths outside of any methods due to code duplication of strings.

Lewis
The whole point is I want to do the redirect and still display the form errors. I know it will work fine if I forward to a view directly.
Taylor Leese
A: 

The only way to persist objects between requests (ie a redirect) is to store the object in a session attribute. So you would include "HttpServletRequest request" in method parameters for both methods (ie, get and post) and retrieve the object via request.getAttribute("binding"). That said, and having not tried it myself you may need to figure out how to re-bind the binding to the object in the new request.

Another "un-nicer" way is to just change the browser URL using javascript

klonq