Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 Portlet MVC stay on same page on error. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 Portlet MVC stay on same page on error.

    I was working with doing form validation and realized that there doesn't seem to be an easy way to return back to the original form that the client requested if an error was to occur. i.e.:

    Code:
    @RenderMapping(params = "action=form")
    public ModelAndView form(@ModelAttribute("formInfo") @Valid Form info, BindingResult binder, Map oldData){
    		
    	if(binder.hasErrors()){
    		//Have to add old data back on
    		oldData.put("oldAgentData", info);
    		//Must create new ModelAndView, and conjure up the previous page url
    		return new ModelAndView("lastPage",oldData);
    	}
    		return new ModelAndView("formSuccess");
    }
    I noticed in the docs (ch 18.9.4) that you can get a map, model, or modelmap and are able to return a model, but the model doesn't seem to be keeping the values.

    Do you bypass this issue by storing the previous page as a hidden field in the form, then on submit grabbing the view and inserting it back in? Just seems like there should be an easier way...

  • #2
    The problem lies in what you return in your controller. If you return a new ModelAndView object at the end of your method, how can you expect that the old model containing previous data is still there? You should do something like that instead:

    Code:
    @RenderMapping(params = "action=form")
    public String form(@ModelAttribute("formInfo") @Valid Form info, BindingResult binder, ModelMap model){
    		
    	if(binder.hasErrors()){
    		//Here you should add to the model the error info to display, something like
                    model.addAttribute("errorMessages", binder.getErrors());
                    return "lastPage"; //go back to form
    	}
            else
    		return "formSuccess"; //navigate to other page
    }

    Comment


    • #3
      yeah that is true. I can't be expecting Spring to know about the old response because the new request being sent from the client side won't contain enough information.

      Comment

      Working...
      X