我有一個問題,即ModelAndView和ModelMap之間的點差異。AnnotationController中的ModelAndView和ModelMap存在問題,Springframework
當requestMethod爲「GET」且requestMethod爲「POST」時,我想維護modelAndView。 我的模型和視圖保存了其他人。
所以我把modelAndView的返回類型改爲「GET」,「POST」方法。
但是,請求丟失的commandObject,形式:錯誤...,如果請求返回showForm「POST」,因爲請求驗證失敗。
例子)
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;
}