@ModelAttribute注解,什么时候用呢? [英] @ModelAttribute annotation, when to use it?
问题描述
假设我们有一个实体的人,一个控制器PersonController和文件edit.jsp(创建一个新的或修改现有人)
控制器
@RequestMapping(值=/编辑,方法= RequestMethod.POST)
公共字符串editPerson(@RequestParam(其中fname)字符串FNAME,型号model){
如果(FNAME == NULL || fname.length()== 0){
model.addAttribute(personToEditOrCreate,新的Person());
}
其他{
人P = personService.getPersonByFirstName(FNAME);
model.addAttribute(personToEditOrCreate,p)的;
} 回到人/编辑;
}@RequestMapping(值=/保存,方法= RequestMethod.POST)
公共字符串savePerson(人的人,BindingResult结果){ personService.savePerson(人);
返回重定向:/家;
}
文件edit.jsp
<形式:形式方法=邮报的ModelAttribute =personToEditOrCreate行动=拯救>
<形式:隐藏路径=ID/>
<表>
&所述; TR>
< TD><形式:标签路径=名字>首先名称和LT; /表:标签>< / TD>
< TD><形式:输入路径=名字/>< / TD>
< / TR>
&所述; TR>
< TD><形式:标签路径=姓氏>姓< /表:标签>< / TD>
< TD><形式:输入路径=姓氏/>< / TD>
< / TR>
&所述; TR>
< TD><形式:标签路径=钱>金钱与LT; /表:标签>< / TD>
< TD><形式:输入路径=钱/>< / TD>
< / TR>
&所述; TR>
&所述; TD列跨度=2>
<输入类型=提交值=添加/编辑人/>
< / TD>
< / TR>
< /表>< / form:表单>
我试着在code以上(不含在savePerson方法使用@ModelAttribute注解,它的工作原理正确,为什么,当我需要添加注释的人对象:
@RequestMapping(值=/保存,方法= RequestMethod.POST)
公共字符串savePerson(@ModelAttribute(personToEditOrCreate)的人的人,BindingResult结果){ personService.savePerson(人);
返回重定向:/家;
}
您不需要 @ModelAttribute
(参数)刚刚使用一个Bean作为参数
例如,这些处理方法做工精细这些请求:
@RequestMapping(/ A)
无效pathA(SomeBean someBean){
的assertEquals(尼尔,someBean.getName());
}GET / A?NAME =尼尔@RequestMapping(值=/ A,方法= RequestMethod.POST)
无效pathAPost(SomeBean someBean){
的assertEquals(尼尔,someBean.getName());
}POST / A
名称=尼尔
使用 @ModelAttribute
(法)为默认数据加载到每个请求模型 - 例如从一个数据库,使用特别是当 @SessionAttributes
。这可以用控制器来完成
或者在 ControllerAdvice
:
@Controller
@RequestMapping(/ FOOS)
公共类FooController的{ @ModelAttribute(富)
串的getFoo(){
返回栏;
}}
任何JSP转发到 FooController的
:
$ {富} // =酒吧
或
@ControllerAdvice
公共类MyGlobalData { @ModelAttribute(富)
串的getFoo(){
返回栏;
}}
任何JSP:
$ {富} // =酒吧
使用 @ModelAttribute
(参数),如果你想使用的结果 @ModelAttribute
(法)作为的默认的:
@ModelAttribute(attrib1)
SomeBean getSomeBean(){
返回新SomeBean(尼尔);
}@RequestMapping(/ a)的
无效pathA(@ModelAttribute(attrib1)SomeBean someBean){
的assertEquals(尼尔,someBean.getName());
}得到
使用 @ModelAttribute
(参数)来获取存储在闪光属性的对象
@RequestMapping(/ A)
字符串pathA(RedirectAttributes redirectAttributes){
redirectAttributes.addFlashAttribute(attrib1,新SomeBean(从闪存));
返回重定向:/ B;
}@RequestMapping(/ B)
无效pathB(@ModelAttribute(attrib1)SomeBean someBean){
的assertEquals(闪离,someBean.getName());
}得到
使用 @ModelAttribute
(参数)来获得由 @SessionAttributes
存储对象
@Controller
@SessionAttributes(attrib1)
公共类控制器1 { @RequestMapping(/ a)的
无效pathA(型号模型){
model.addAttribute(attrib1,新SomeBean(尼尔)); //这结束会话,由于控制器@SessionAttributes
} @RequestMapping(/ B)
无效pathB(@ModelAttribute(attrib1)SomeBean someBean){
的assertEquals(尼尔,someBean.getName());
}
}得到
GET / B
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";
}
You don't need @ModelAttribute
(parameter) just to use a Bean as a parameter
For example, these handler methods work fine with these requests:
@RequestMapping("/a")
void pathA(SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
GET /a?name=neil
@RequestMapping(value="/a", method=RequestMethod.POST)
void pathAPost(SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
POST /a
name=neil
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 a Controller
or in a ControllerAdvice
:
@Controller
@RequestMapping("/foos")
public class FooController {
@ModelAttribute("foo")
String getFoo() {
return "bar";
}
}
Any JSP forwarded to by FooController
:
${foo} //=bar
or
@ControllerAdvice
public class MyGlobalData {
@ModelAttribute("foo")
String getFoo() {
return "bar";
}
}
Any JSP:
${foo} //=bar
Use @ModelAttribute
(parameter) if you want to use the result of @ModelAttribute
(method) as a default:
@ModelAttribute("attrib1")
SomeBean getSomeBean() {
return new SomeBean("neil");
}
@RequestMapping("/a")
void pathA(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
GET /a
Use @ModelAttribute
(parameter) to get an object stored in a flash attribute:
@RequestMapping("/a")
String pathA(RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("attrib1", new SomeBean("from flash"));
return "redirect:/b";
}
@RequestMapping("/b")
void pathB(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("from flash", someBean.getName());
}
GET /a
Use @ModelAttribute
(parameter) to get an object stored by @SessionAttributes
@Controller
@SessionAttributes("attrib1")
public class Controller1 {
@RequestMapping("/a")
void pathA(Model model) {
model.addAttribute("attrib1", new SomeBean("neil")); //this ends up in session due to @SessionAttributes on controller
}
@RequestMapping("/b")
void pathB(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
}
GET /a
GET /b
这篇关于@ModelAttribute注解,什么时候用呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!