1.

一、绑定请求参数到指定对象

  1. public String test1(@ModelAttribute("user") UserModel user)

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

如请求参数包含“?username=zhang&password=123&workInfo.city=bj”自动绑定到user 中的workInfo属性的city属性中。

  1. @RequestMapping(value="/model2/{username}")
  2. public String test2(@ModelAttribute("model") DataBinderTestModel model)

URI 模板变量也能自动绑定到命令对象中, 当你请求的URL 中包含“bool=yes&schooInfo.specialty=computer&hobbyList[0]=program&hobbyList[1]=music&map[key1]=value1&map[key2]=value2&state=blocked”会自动绑定到命令对象上。当URI模板变量和请求参数同名时,URI模板变量具有高优先权。

二、暴露表单引用对象为模型数据

  1. /**
  2. * 设置这个注解之后可以直接在前端页面使用hb这个对象(List)集合
  3. * @return
  4. */
  5. @ModelAttribute("hb")
  6. public List<String> hobbiesList(){
  7. List<String> hobbise = new LinkedList<String>();
  8. hobbise.add("basketball");
  9. hobbise.add("football");
  10. hobbise.add("tennis");
  11. return hobbise;
  12. }

JSP页面展示出来

  1. <br>
  2. 初始化的数据 :    ${hb }
  3. <br>
  4. <c:forEach items="${hb}" var="hobby" varStatus="vs">
  5. <c:choose>
  6. <c:when test="${hobby == 'basketball'}">
  7. 篮球<input type="checkbox" name="hobbies" value="basketball">
  8. </c:when>
  9. <c:when test="${hobby == 'football'}">
  10. 足球<input type="checkbox" name="hobbies" value="football">
  11. </c:when>
  12. <c:when test="${hobby == 'tennis'}">
  13. 网球<input type="checkbox" name="hobbies" value="tennis">
  14. </c:when>
  15. </c:choose>
  16. </c:forEach>

备注:

1、通过上面这种方式可以显示出一个集合的内容

2、上面的jsp代码使用的是JSTL,需要导入JSTL相关的jar包

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

三、暴露@RequestMapping方法返回值为模型数据

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

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

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

2.

@ModelAttribute使用详解

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

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

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

  1. <span style="font-size:12px;">       @RequestMapping(value = "/helloWorld")
  2. public String helloWorld(String abc) {
  3. return "helloWorld";
  4. }</span>
  5. <span style="font-size:12px;">
  6. </span>

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

  1. <span style="font-size:12px;">@ModelAttribute
  2. public Account addAccount(@RequestParam String number) {
  3. return accountManager.findAccount(number);
  4. }
  5. </span>
  6. <span style="font-size:12px;">
  7. </span>

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

  1. <span style="font-size:12px;">@Controller
  2. public class HelloWorldController {
  3. @ModelAttribute("attributeName")
  4. public String addAccount(@RequestParam String abc) {
  5. return abc;
  6. }
  7. @RequestMapping(value = "/helloWorld")
  8. public String helloWorld() {
  9. return "helloWorld";
  10. }
  11. }</span>
  12. <span style="font-size:12px;">
  13. </span>

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

  1. <span style="font-size:12px;">@Controller
  2. public class HelloWorldController {
  3. @RequestMapping(value = "/helloWorld.do")
  4. @ModelAttribute("attributeName")
  5. public String helloWorld() {
  6. return "hi";
  7. }
  8. }</span>
  9. <span style="font-size:12px;">
  10. </span>

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

(1)从model中获取

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

在这个例子里,@ModelAttribute("user") User user注释方法参数,参数user的值来源于addAccount()方法中的model属性。
    此时如果方法体没有标注@SessionAttributes("user"),那么scope为request,如果标注了,那么scope为session
    
    (2)从Form表单或URL参数中获取(实际上,不做此注释也能拿到user对象)

  1. <span style="font-size:12px;">@Controller
  2. public class HelloWorldController {
  3. @RequestMapping(value = "/helloWorld")
  4. public String helloWorld(@ModelAttribute User user) {
  5. return "helloWorld";
  6. }
  7. }</span>
  8. <span style="font-size:12px;">
  9. </span>

Spring的@ModelAttribute注解的更多相关文章

  1. 全面解析Spring中@ModelAttribute注解的用法

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...

  2. Spring MVC @ModelAttribute注解

    在一个Controller内,被@ModelAttribute标注的方法会在此controller的每个handler方法执行前被执行. 被@ModelAttribute标注的方法的参数绑定规则和普通 ...

  3. Spring Boot 2.0 教程 | @ModelAttribute 注解

    欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ...

  4. Spring MVC 中 @ModelAttribute 注解的妙用

    Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...

  5. spring mvc之@ModelAttribute注解

    1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...

  6. Spring MVC SessionAttributes ModelAttribute注解

    说明 本文主要针对 @SessionAttributes注解 和 @ModelAttribute注解的基础用法进行解析.至于为什么会将这两个注解放在一起,是因为它们之间还是有点影响的. @Sessio ...

  7. Spring @SessionAttributes注解 @ModelAttribute注解

    一.@SessionAttribute详解 如果多个请求之间需要共享数据,就可以使用@SessionAttribute. 配置的方法: 在控制器类上标注@SessionAttribute. 配置需要共 ...

  8. Spring ModelAttribute注解失效原因

    public String test(@RequestParam(value = "test") @ModelAttribute("test") String ...

  9. Spring MVC常用注解

    cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...

随机推荐

  1. 手动安装yii2.0-redis扩展

    1.点击下载:yii2.0-redis扩展 2.把下载的扩展文件放到vendor/yiisoft/下,命名为yii2-redis 3.修改vender/yiisoft/下的extensions.php ...

  2. HttpClient 发送 HTTP、HTTPS

    首先说一下该类中需要引入的 jar 包,apache 的 httpclient 包,版本号为 4.5,如有需要的话,可以引一下.     代码 import org.apache.commons.io ...

  3. 图解Java常用数据结构(一)【转载】

    最近在整理数据结构方面的知识, 系统化看了下Java中常用数据结构, 突发奇想用动画来绘制数据流转过程. 主要基于jdk8, 可能会有些特性与jdk7之前不相同, 例如LinkedList Linke ...

  4. 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:

    问题信息详细: 信息: The APR based Apache Tomcat Native library which allows optimal performance in productio ...

  5. gcd,扩展欧几里得,中国剩余定理

    1.gcd: int gcd(int a,int b){ ?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题: ...

  6. 11-matlba-bellman-ford;地杰斯特拉

    求最短路: 1.bellman-ford: %求s到各点的最短距离 function Dist = Bellman_Ford(s) load cityJuli; for i = 1:154 Dist( ...

  7. 【JVM】浅谈双亲委派和破坏双亲委派

    一.前言 笔者曾经阅读过周志明的<深入理解Java虚拟机>这本书,阅读完后自以为对jvm有了一定的了解,然而当真正碰到问题的时候,才发现自己读的有多粗糙,也体会到只有实践才能加深理解,正应 ...

  8. win 下 nginx 与 php的配置

    1.下载需要的软件包 php的windows版本(*注意这里下载非线程安全的,nginx使用的是cgi) http://windows.php.net/download/   nginx的window ...

  9. Mysql、Oracle、SqlServer的JDBC连接实现和对比(提供驱动包)

    首先,我们需要准备数据库连接所需的jar包.目前mysql的驱动包可能比较好找,但是oracle和sqlserver的有很多,要找到能用的要花一点点心思,这里直接把下载地址和版本发送出来. Mysql ...

  10. linux,windows 可执行文件(ELF、PE)

    现在PC平台流行的可执行文件格式(Executable)主要是Windows下的PE(Portable Executable)和Linux的ELF(Executable Linkable Format ...