Announcement Announcement Module
Collapse
No announcement yet.
POST GET Redirect - form errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • POST GET Redirect - form errors

    Hi all.

    Fairly new to Spring. I'm making a project that does not use Spring Web Flow, but is using the new annotation style in Spring 2.5.

    I have a form which I validate and, if there are errors, I go back into the form to fix them. The problem is that when I do so, the ugly browser pop-up asking to re-post the data comes up.

    I've tried returning a redirect: url, but doing that loses all the error messages that come up from validation.

    Is there an easy way to avoid this behavior?

    Code:
    @Controller
    @RequestMapping("edit")
    public class EditController {
    
       @RequestMapping(method=RequestMethod.GET)
       public String showPage(@RequestParam(value="start", required=false) Boolean start, HttpSession session, ModelMap map)
       {
          if (start != null && start)
             map.put("start", true);
          else
             map.put("start", false);
          return "edit";
       }
    
       @ModelAttribute("myObject")
       public Notification getObject(@RequestParam(value="start", required=false) Boolean start, HttpSession session)
       {
          if (start != null && start)
          {
             return new MyClass();
          }
          return (MyClass)session.getAttribute("myObject");
       }
    
       @RequestMapping(method=RequestMethod.POST)
       public String submit(HttpSession session, ModelMap map, @ModelAttribute("myObject") MyClass myObject,
             BindingResult result, SessionStatus status) {
          new MyValidator().validate(myObject, result);
          result.setNestedPath("");
          if (result.hasErrors())
          {
             map.put("myObject", myObject);
             return "redirect:edit";
          }
          session.setAttribute("myObject", myObject);
          status.setComplete();
          return "redirect:confirm";
       }     
    }
Working...
X