Spring MVC中注解 @ModelAttribute

1、@ModelAttribute放在方法之上,在当前Control内的所有方法映射多个URL的请求,都会执行该方法

 @ModelAttribute
 public void itemsCommon(HttpServletRequest request,Model model){
       model.addAttribute("common", "common111");
 }

2、如果在请求链接中没有参数abc,塞入到model中的参数值为null

    @ModelAttribute
    public void itemsCommon(HttpServletRequest request,Model model,String abc){
    model.addAttribute("common", abc);
    }

3、和上述方法的 model.addAttribute("common", abc);效果相同

 @ModelAttribute(value="common")
 public String itemsCommon(HttpServletRequest request,Model model,String abc){
  return abc;
 }

4、如果不设置value值,则attribute的key值隐式设置为类型(首字母小写),如下理解为 model.addAttribute("string", abc)

如果返回类型为model,则为model类名的小写,如model.addAttribute("persion", abc)

    @ModelAttribute
    public String itemsCommon(HttpServletRequest request,Model model,String abc){
    return abc;
    }

5、@ModelAttribute和@RequestMapping可以同时放在一个方法上

6、@ModelAttribute可以放在参数前面

@RequestMapping("/testCommon")
public String itemsCommon(@ModelAttribute String abc){
return abc;
}
原文地址:https://www.cnblogs.com/liuwt365/p/5876919.html