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

  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 注解的同名命令对象

SpringMvc中@ModelAttribute注解的使用的更多相关文章

  1. springmvc中ModelAttribute注解应用在参数中

    可以用@ModelAttribute来注解方法参数或方法.带@ModelAttribute创建的参数对象会被添加到Model对象中.注解在参数上时,可以从Form表单或URL参数中获取参数并绑定到mo ...

  2. SpringMVC 中 @ControllerAdvice 注解

    SpringMVC 中 @ControllerAdvice 注解 1.@ControllerAdvice 1.1 全局异常处理 1.2 全局数据绑定 1.3 全局数据预处理 原文地址: 江南一点雨:S ...

  3. SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...

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

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

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

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

  6. SpringMVC 中 @ControllerAdvice 注解的三种使用场景!

    @ControllerAdvice ,很多初学者可能都没有听说过这个注解,实际上,这是一个非常有用的注解,顾名思义,这是一个增强的 Controller.使用这个 Controller ,可以实现三个 ...

  7. SpringMvc中@ModelAttribute的运用

    /** * 1. 有 @ModelAttribute 标记的方法, 会在每个目标方法执行之前被 SpringMVC 调用! * 2. @ModelAttribute 注解也可以来修饰目标方法 POJO ...

  8. Spring|SpringMVC中的注解

    文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...

  9. SpringMVC的@ModelAttribute注解简单使用(用户修改信息)

    例如有一个User对象,我们要修改他的值,但是不能修改他的密码!通过表单提交数据之后,password为null,会把原对象的passwod覆盖掉.这时候可以用@ModelAttribute注解处理. ...

随机推荐

  1. stark - 增、删、改

    一.效果图 二.增.删.改 知识点: 1.解决代码重用 {% include 'form.html' %} 2.自定制配置modelform 每张表,就可自定义配置 labels , widges.. ...

  2. D. Little Artem and Dance---cf669D(模拟)

    题目链接:http://codeforces.com/problemset/problem/669/D 给你n个数,一开始是1 2 3 4 5 6 ... n 这样的 现在有两个操作,第一个操作是所有 ...

  3. Flex 布局:实例篇

    上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法.你会看到,不管是什么布局,Flex往往都可以几行命令搞定. ​ 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇& ...

  4. git-【五】远程仓库

    一.准备工作 在了解之前,先注册github[https://github.com/]账号,由于你的本地Git仓库和github仓库之间的传输是通过SSH加密的,所以需要一点设置: 第一步 创建SSH ...

  5. Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, ...

    使用oracle数据库一个多月依赖这问题一直都得不到解决,最近任务不是很忙了,所以决定把这问题解决掉.写一篇文章做记录. 以上错误主要是net程序oracle数据库使用了Microsoft Enter ...

  6. mysql8新特性(一)

    https://www.oschina.net/news/95325/mysql-8-0-ga-released http://blog.itpub.net/28218939/viewspace-21 ...

  7. Hello 2019 Solution

    A. Gennady and a Card Game 签到. #include <bits/stdc++.h> using namespace std; ], t[]; bool solv ...

  8. poj2954 Triangle

    地址:http://poj.org/problem?id=2954 题目: Triangle Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. ts 绘制多边形

    let bg = this.createBitmapByName("123_png"); this.addChild(bg) bg.x = this.stage.width / 2 ...

  10. JavaScript进阶内容笔记1:各种对象类型判断

    该文章主要用来介绍JavaScript中常用的一些对象检测判断方法,整理资源来自书本和网络,如有错误或说明不详之处,望评论提出,本菜定提名感谢……(本文章知识比较基础,大牛请提些意见再绕道,三克油^_ ...