J'ai une question qui est une différence de point entre ModelAndView et ModelMap.Problème avec ModelAndView et ModelMap dans AnnotationController, Springframework
Je souhaite gérer modelAndView lorsque requestMethod est "GET" et que requestMethod est "POST". Mon modelAndView a enregistré les autres. J'ai donc fait un retour type modelAndView aux méthodes "GET", "POST". Mais, Request lost commandObject, forme: errors ..., si la requête renvoie showForm sur "POST" car la validation de la requête a échoué.
exemple)
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;
}