我得到“ Bean名称'person1'的BindingResult和普通目标对象都不能作为请求属性” 。这个问题的大多数答案都说我错过了 @ModelAttribute 批注,但是我使用它仍然会遇到该异常。
我使用多种控制器方法,并希望具有相同的模型以在不同的方法中使用不同的名称。当我使用 @ModelAttribute(name =“ person1”)时,会发生问题。如果我不命名,它将起作用,但是即使所有表格中我都填写相同的模型,并且在所有表格中都显示相同的验证错误。
这些是我的示例控制器方法:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView index(HttpSession session) {
logger.info("opening index page");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("person1", new Person("Maria Smith", null));
modelAndView.addObject("person", new Person("John Doe", null));
return modelAndView;
}
@RequestMapping(value = "/getPersonTree", method = RequestMethod.POST)
public ModelAndView getPersonTree(@Valid @ModelAttribute("person1") Person person, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return new ModelAndView("index", "person1", person);
} else {
int id = databaseModel.getPersonID(person);
person = databaseModel.setPersonTreeParents(databaseModel.getPersonTree(id));
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("getPersonTreeSuccess");
modelAndView.addObject("person1", person);
return modelAndView;
}
} }
@RequestMapping(value = "/insertPerson", method = RequestMethod.POST)
public ModelAndView insertPerson(@Valid @ModelAttribute Person person,
BindingResult bindingResult) {
logger.info("insert person controller");
if (bindingResult.hasErrors()) {
return new ModelAndView("index", "person", person);
} else {
databaseModel.insert(person);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("insertSuccess");
modelAndView.addObject("person", person);
return modelAndView;
}
}
这些是我的示例jsps:
<f:form class="form-inline" action="getPersonTree"
modelAttribute="person1">
<f:label path="name">Person Name:</f:label>
<f:input path="name" type="text" />
<f:errors path="name" class="alert alert-danger"></f:errors>
<button class="btn btn-primary" type="submit">Get Person
Tree</button>
</f:form>
<f:form class="form-inline" action="insertPerson"
modelAttribute="person">
<f:label path="name">Person Name:</f:label>
<f:input path="name" type="text" />
<f:errors path="name" class="alert alert-danger"></f:errors>
<f:label path="birthDate">Date of birth:</f:label>
<f:input type="date" path="birthDate" />
<f:errors path="birthDate" class="alert alert-danger"></f:errors>
<f:label path="gender">Gender:</f:label>
<%-- <f:input path="gender" type="text" /> --%>
<f:radiobutton path="gender" value="male" />M
<f:radiobutton path="gender" value="female" />F
<f:errors path="gender" class="alert alert-danger"></f:errors>
<button class="btn btn-primary" type="submit">Add Person</button>
</f:form>
我希望自定义名称能起作用,因为有一些教程显示了带有自定义名称的此类示例,但我遇到了例外。因此,我的问题是:谁能在同一页面上以多种形式显示正确的方式来处理多个 ModelAttributes ,每种形式一个 ModelAttribute ,但名称不同?
答案 0 :(得分:0)
在GET
处理程序中:
modelAndView.addObject("thisIsYourCommandName", person);
在JSP中:
<form modelAttribute="thisIsYourCommandName" ..../>
在POST
处理程序中:
@RequestMapping(value = "/getPersonTree")
public ModelAndView getPersonTree(@ModelAttribute("thisIsYourCommandName") Person person, BindingResult result) {
// ...
}
如果您的命令名称是“ person”而不是“ thisIsYourCommandName”,则可以执行此操作
@ModelAttribute Person person
与
相同@ModelAttribute("person") Person person
所以只需执行以下操作:
GET
处理程序:
modelAndView.addObject("person", new Person());
modelAndView.addObject("x", new Person());
<{> Person
的表单:
<form:form modelAttribute="person" ...>
<{> X
的表单:
<form:form modelAttribute="x" ...>
POST
处理程序:
@ModelAttribute("person") Person person
或@ModelAttribute Person person
并且:
@ModelAttribute("x") Person person
或@ModelAttribute Person x