Problem with ModelAndView and ModelMap in AnnotationController, Springframework
- by saltfactory
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;
}