ModelAndViewとModelMapの間に相違点があります。AnnotationController、SpringframeworkのModelAndViewとModelMapの問題
requestMethodが「GET」で、requestMethodが「POST」の場合、modelAndViewを維持したいと考えています。 私のmodelAndViewは他の人を保存しました。
私はmodelAndViewの戻り値の型を "GET"、 "POST"メソッドにしました。
しかし、要求はcommandObject、form:errors ...(要求が失われた)要求の検証が失敗したために "POST"でshowFormを返します。
例)
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;
}