@ModelAttribute注解,什么时候使用它?
假设我们有一个实体 Person、一个控制器 PersonController 和一个 edit.jsp 页面(创建一个新的或编辑现有的 person)
控制器
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editPerson(@RequestParam("fname") String fname, Model model) {
if(fname == null || fname.length() == 0){
model.addAttribute("personToEditOrCreate", new Person());
}
else{
Person p = personService.getPersonByFirstName(fname);
model.addAttribute("personToEditOrCreate", p);
}
return "persons/edit";
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String savePerson(Person person, BindingResult result) {
personService.savePerson(person);
return "redirect:/home";
}
edit.jsp
<form:form method="post" modelAttribute="personToEditOrCreate" action="save">
<form:hidden path="id"/>
<table>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td><form:label path="money">Money</form:label></td>
<td><form:input path="money" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add/Edit Person"/>
</td>
</tr>
</table>
</form:form>
我正在尝试上面的代码(没有在 savePerson 方法中使用 @ModelAttribute 注释,并且它工作正确。为什么以及何时需要将注释添加到 person 对象:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String savePerson(@ModelAttribute("personToEditOrCreate") Person person, BindingResult result) {
personService.savePerson(person);
return "redirect:/home";
}
Lets say we have an entity Person, a controller PersonController and an edit.jsp page (creating a new or editing an existing person)
Controller
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editPerson(@RequestParam("fname") String fname, Model model) {
if(fname == null || fname.length() == 0){
model.addAttribute("personToEditOrCreate", new Person());
}
else{
Person p = personService.getPersonByFirstName(fname);
model.addAttribute("personToEditOrCreate", p);
}
return "persons/edit";
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String savePerson(Person person, BindingResult result) {
personService.savePerson(person);
return "redirect:/home";
}
edit.jsp
<form:form method="post" modelAttribute="personToEditOrCreate" action="save">
<form:hidden path="id"/>
<table>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td><form:label path="money">Money</form:label></td>
<td><form:input path="money" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add/Edit Person"/>
</td>
</tr>
</table>
</form:form>
Im trying the code above (without using the @ModelAttribute annotation in the savePerson method, and it works correct. Why and when do i need to add the annotation to the person object:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String savePerson(@ModelAttribute("personToEditOrCreate") Person person, BindingResult result) {
personService.savePerson(person);
return "redirect:/home";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要
@ModelAttribute
(parameter) 只是使用 Bean 作为参数例如,这些处理程序方法可以很好地处理这些请求:
使用
@ModelAttribute
(方法)在每次请求时将默认数据加载到模型中 - 例如从数据库加载,尤其是在使用@SessionAttributes
时。这可以在Controller
或ControllerAdvice
中完成:由
FooController
转发到的任何 JSP:或
任何 JSP:
使用
@ModelAttribute
(参数)如果您想使用@ModelAttribute
(方法)的结果作为默认:使用
@ModelAttribute
(参数)获取存储在flash属性中的对象:使用
@ModelAttribute
(参数)获取由@SessionAttributes
存储的对象You don't need
@ModelAttribute
(parameter) just to use a Bean as a parameterFor example, these handler methods work fine with these requests:
Use
@ModelAttribute
(method) to load default data into your model on every request - for example from a database, especially when using@SessionAttributes
. This can be done in aController
or in aControllerAdvice
:Any JSP forwarded to by
FooController
:or
Any JSP:
Use
@ModelAttribute
(parameter) if you want to use the result of@ModelAttribute
(method) as a default:Use
@ModelAttribute
(parameter) to get an object stored in a flash attribute:Use
@ModelAttribute
(parameter) to get an object stored by@SessionAttributes
您的问题似乎已经得到解答:
What is @ModelAttribute in Spring MVC?
总结答案和博客文章:当您希望表单支持对象(Person 的实例)在请求之间保留时。
否则,如果没有注释,请求映射方法将假定 Person 是一个新对象,并且绝不会链接到您的表单支持对象。顺便说一句,海报引用的博客文章真的很棒,绝对是必读的。
Your question appears to be answered already:
What is @ModelAttribute in Spring MVC?
To summarize the answer and blog post: when you want your form backing object (instance of Person) to be persisted across requests.
Otherwise, without the annotation, the request mapped method will assume Person is a new object and in no way linked to your form backing object. The blog post that poster references is really awesome by the way, definitely a must-read.
方法参数上的 @ModelAttribute 指示将从模型中检索该参数。如果模型中不存在该参数,则该参数将首先实例化,然后添加到模型中。
An @ModelAttribute on a method argument indicates the argument will be retrieved from the model.If not present in the model, the argument will be instantiated first and then added to the model.