【发布时间】:2010-11-09 15:44:22
【问题描述】:
我有一个问题是 ModelAndView 和 ModelMap 之间的点差。
当 requestMethod 为“GET”且 requestMethod 为“POST”时,我想维护 modelAndView。 我的 modelAndView 救了别人。
所以我将 modelAndView 返回类型设置为“GET”、“POST”方法。
但是,如果请求因请求验证失败而在“POST”上返回 showForm,则请求丢失 commandObject、form:errors...。
示例)
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;
}
【问题讨论】:
标签: java spring spring-mvc