在SpringMVC中使用@ModelAttribute,存在三种情况
1. 应用在方法上
2. 应用在方法参数上
3. 应用在方法上,并且方法上应用了@RequestMapping
目录
应用在方法上,并且方法使用注解@RequestMapping
应用在方法
@ModelAttribute注解的方法,会在Controller注解的方法前执行,如果Controller存在多个匹配路径,需要慎用
@ModelAttribute注解返回void方法
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService service;
@ModelAttribute
public void myMode(@RequestParam(required = false)String abc,Model model){
model.addAttribute("abc",abc);
}
@RequestMapping("/list")
public String queryBooks(Model model){
boolean flag = model.containsAttribute("abc");
System.out.println(flag);
model.addAttribute("list",service.queryBook());
return "bookList";
}
}
浏览器输入http://localhost:8080/book/list?abc=123,首先执行的myMode方法,这里获取请求参数,并设置model设置,这个model将带到后面的queryBooks方法
合并后的效果就是平常开发中的方法
@RequestMapping("/list")
public String queryBooks(@RequestParam(required = false)String abc,Model model){
model.addAttribute("abc",abc);
//.....
}
使用@ModelAttribute注解带有返回值的方法
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService service;
@ModelAttribute
public String myMode(@RequestParam(required = false)String abc,Model model){
return abc;
}
@ModelAttribute
public int myMode1(int a){
return a;
}
@ModelAttribute
public Book myModel2(String name){
Book book = new Book();
book.setName(name);
return book;
}
@RequestMapping("/list")
public String queryBooks(@RequestParam(required = false)String abc,Model model){
Object[] str = model.asMap().keySet().toArray();
for (int i = 0; i < str.length; i++) {
System.out.println("key:"+str[i]+",value:"+model.asMap().get(str[i]));
}
model.addAttribute("list",service.queryBook());
return "bookList";
}
}
浏览器访问地址http://localhost:8080/book/list?abc=123&a=111&name=从入门到精通,controller先执行myMode方法,再去执行querybooks
执行myModel方法,这种情况下,是以返回值类型的首字母小写为key,返回值为value,存入model中.
输出结果:
key:string,value:123
key:book,value:com.example.chapter_1_web.domain.Book@913c3ed
key:int,value:111
但是,这种key的设置有点死板,可以在@ModelAttribute(value="mykey"),启用value属性自定义key
@ModelAttribute(value = "abc")
public String myMode(@RequestParam(required = false)String abc,Model model){
return abc;
}
这就相当于,model.addAttribute("abc",abc);
@ModeAttribute应用在方法参数上
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService service;
@ModelAttribute(value = "attributeName")
public String myMode(@RequestParam(required = false)String abc,Model model){
return abc;
}
@ModelAttribute
public void myMode1(Model model){
model.addAttribute("age",13);
model.addAttribute("name","zhangsan");
}
@RequestMapping("/list")
public String queryBooks(Model model,
@ModelAttribute("attributeName")String abc,
@ModelAttribute("age")int age,
@ModelAttribute("name")String name
){
System.out.println("abc:"+abc+",age:"+age+",name:"+name);
model.addAttribute("list",service.queryBook());
return "bookList";
}
}
浏览器访问地址http://localhost:8080/book/list?abc=123,queryBooks方法输出结果:
abc:123,age:13,name:zhangsan
也就是,@ModelAttribute加载方法参数上表示,从前面的model中取值,赋值给方法参数
应用在方法上,并且方法使用注解@RequestMapping
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService service;
@RequestMapping("/list")
@ModelAttribute("abc")
public String bookList(Model model,String abc
){
model.addAttribute("list",service.queryBook());
return abc;
}
}
此时,abc还是存到model中,但是返回值不再表示视图名,视图名是RequestMapping的值 list ,路径则是按照book/list.html解析
参考博客: https://blog.csdn.net/lovesomnus/article/details/78873089