前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >注解 @ModelAttribute 运用详细介绍

注解 @ModelAttribute 运用详细介绍

作者头像
微风-- 轻许--
发布2020-01-02 11:04:24
9150
发布2020-01-02 11:04:24
举报
文章被收录于专栏:java 微风java 微风java 微风

1.@ModelAttribute注释方法   例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用。 (1)@ModelAttribute注释void返回值的方法

 1 @Controller 
 2 public class HelloWorldController { 
 3     @ModelAttribute 
 4     public void populateModel(@RequestParam String abc, Model model) { 
 5          model.addAttribute("attributeName", abc); 
 6       } 
 7 
 8     @RequestMapping(value = "/helloWorld") 
 9     public String helloWorld() { 
10        return "helloWorld"; 
11         } 
12  }

  这个例子,在获得请求/helloWorld 后,populateModel方法在helloWorld方法之前先被调用,它把请求参数(/helloWorld?abc=text)加入到一个名为attributeName的model属性中,在它执行后 helloWorld被调用,返回视图名helloWorld和model已由@ModelAttribute方法生产好了。   这个例子中model属性名称和model属性对象有model.addAttribute()实现,不过前提是要在方法中加入一个Model类型的参数。   当URL或者post中不包含次参数时,会报错,其实不需要这个方法,完全可以把请求的方法写成,这样缺少此参数也不会出错

1 @RequestMapping(value = "/helloWorld") 
2 public String helloWorld(String abc) { 
3     return "helloWorld"; 
4 }

(2)@ModelAttribute注释返回具体类的方法

1 @ModelAttribute 
2 public Account addAccount(@RequestParam String number) { 
3     return accountManager.findAccount(number); 
4 } 

  这种情况,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回Account类型,那么这个model属性的名称是account。   这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无须要特定的参数。

(3)@ModelAttribute(value="")注释返回具体类的方法

 1 @Controller 
 2 public class HelloWorldController { 
 3     @ModelAttribute("attributeName") 
 4     public String addAccount(@RequestParam String abc) { 
 5         return abc; 
 6       } 
 7 
 8     @RequestMapping(value = "/helloWorld") 
 9     public String helloWorld() { 
10         return "helloWorld"; 
11           } 
12    }

  这个例子中使用@ModelAttribute注释的value属性,来指定model属性的名称。model属性对象就是方法的返回值。它无须要特定的参数。

(4)@ModelAttribute和@RequestMapping同时注释一个方法

1 @Controller 
2 public class HelloWorldController { 
3     @RequestMapping(value = "/helloWorld.do") 
4     @ModelAttribute("attributeName") 
5     public String helloWorld() { 
6          return "hi"; 
7       } 
8   }

  这时这个方法的返回值并不是表示一个视图名称,而是model属性的值,视图名称由RequestToViewNameTranslator根据请求"/helloWorld.do"转换为逻辑视图helloWorld。 Model属性名称有@ModelAttribute(value=””)指定,相当于在request中封装了key=attributeName,value=hi。

2.@ModelAttribute注释一个方法的参数 (1)从model中获取

 1 @Controller 
 2 public class HelloWorldController { 
 3     @ModelAttribute("user") 
 4     public User addAccount() { 
 5         return new User("jz","123"); 
 6      } 
 7 
 8     @RequestMapping(value = "/helloWorld") 
 9     public String helloWorld(@ModelAttribute("user") User user) { 
10            user.setUserName("jizhou"); 
11            return "helloWorld"; 
12         } 
13   }

  在这个例子里,@ModelAttribute("user") User user注释方法参数,参数user的值来源于addAccount()方法中的model属性。   此时如果方法体没有标注@SessionAttributes("user"),那么scope为request,如果标注了,那么scope为session

(2)从Form表单或URL参数中获取(实际上,不做此注释也能拿到user对象)

  @ModelAttribute具有如下三个作用:

 ①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑定流程,而且自动暴露为模型数据用于视图页面展示时使用。

  其实  @ModelAttribute 此处对于供视图页面展示来说与 model.addAttribute("attributeName", abc); 功能类似。

1 @Controller 
2 public class HelloWorldController { 
3     @RequestMapping(value = "/helloWorld") 
4     public String helloWorld(@ModelAttribute User user) { 
5         return "helloWorld"; 
6      } 
7 }

此处多了一个注解@ModelAttribute("user"),它的作用是将该绑定的命令对象以“user”为名称添加到模型对象中供视图页面展示使用。我们此时可以在视图页面使用${user.username}来获取绑定的命令对象的属性。

 ②暴露@RequestMapping 方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用。

public @ModelAttribute("user2") UserModel test3(@ModelAttribute("user2") UserModel user)  

  大家可以看到返回值类型是命令对象类型,而且通过 @ModelAttribute("user2") 注解,此时会暴露返回值到模型数据( 名字为user2 ) 中供视图展示使用

   @ModelAttribute 注解的返回值会覆盖 @RequestMapping 注解方法中的 @ModelAttribute 注解的同名命令对象

 ③暴露表单引用对象为模型数据:放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用对象,如注册时需要选择的所在城市等,而且在执行功能处理方法( @RequestMapping 注解的方法)之前,自动添加到模型对象中,用于视图页面展示时使用;

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档