Problem with ModelAndView and ModelMap in AnnotationController, Springframework

Posted by saltfactory on Stack Overflow See other posts from Stack Overflow or by saltfactory
Published on 2009-07-10T08:45:51Z Indexed on 2010/04/28 14:13 UTC
Read the original article Hit count: 232

Filed under:
|
|

I have a question that is a point difference between ModelAndView and ModelMap.

I want to maintain modelAndView when requestMethod is "GET" and requestMethod is "POST". My modelAndView saved others.

So I made modelAndView return type to "GET", "POST" methods.

But, Request lost commandObject, form:errors..., if request return showForm on "POST" because request validation failed.

example)

private ModelAndView modelAndView;    

public ControllerTest{
   this.modelAndView = new ModelAndView();
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView showForm(ModelMap model) {
    EntityObject entityObject = new EntityObject();
    CommandObject commandObject = new CommandObject();
    commandObject.setEntityObject(entityObject);
    model.addAttribute("commandObject", commandObject);
    this.modelAndView.addObject("id", "GET");
    this.modelAndView.setViewName("registerForm");

    return this.modelAndView;
}

@RequestMapping(method = RequestMethod.POST)
public ModelAndView submit(@ModelAttribute("commandObject") CommandObject commandObject,
        BindingResult result, SessionStatus status) {

    this.commandValidator.validate(commandObject, result);

    if (result.hasErrors()) {
        this.modelAndView.addObject("id", "POST");
        this.modelAndView.setViewName("registerForm");
        return this.modelAndView;

    } else {
        this.modelAndView.addObject("id", "after POST");
        this.modelAndView.setViewName("success");
    }
    status.setComplete();
    return this.modelAndView;

}

© Stack Overflow or respective owner

Related posts about java

Related posts about spring