SpringMVC(七)註解@ModelAttribute
在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:[email protected]
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