SpringMvc中@ModelAttribute注解的使用
一、绑定请求参数到指定对象
- public String test1(@ModelAttribute("user") UserModel user)
只是此处多了一个注解@ModelAttribute("user"),它的作用是将该绑定的命令对象以“user”为名称添加到模型对象中供视图页面展示使用。我们此时可以在视图页面使用${user.username}来获取绑定的命令对象的属性。
如请求参数包含“?username=zhang&password=123&workInfo.city=bj”自动绑定到user 中的workInfo属性的city属性中。
- @RequestMapping(value="/model2/{username}")
- 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模板变量具有高优先权。
二、暴露表单引用对象为模型数据
- /**
- * 设置这个注解之后可以直接在前端页面使用hb这个对象(List)集合
- * @return
- */
- @ModelAttribute("hb")
- public List<String> hobbiesList(){
- List<String> hobbise = new LinkedList<String>();
- hobbise.add("basketball");
- hobbise.add("football");
- hobbise.add("tennis");
- return hobbise;
- }
JSP页面展示出来
- <br>
- 初始化的数据 : ${hb }
- <br>
- <c:forEach items="${hb}" var="hobby" varStatus="vs">
- <c:choose>
- <c:when test="${hobby == 'basketball'}">
- 篮球<input type="checkbox" name="hobbies" value="basketball">
- </c:when>
- <c:when test="${hobby == 'football'}">
- 足球<input type="checkbox" name="hobbies" value="football">
- </c:when>
- <c:when test="${hobby == 'tennis'}">
- 网球<input type="checkbox" name="hobbies" value="tennis">
- </c:when>
- </c:choose>
- </c:forEach>
备注:
1、通过上面这种方式可以显示出一个集合的内容
2、上面的jsp代码使用的是JSTL,需要导入JSTL相关的jar包
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
三、暴露@RequestMapping方法返回值为模型数据
- public @ModelAttribute("user2") UserModel test3(@ModelAttribute("user2") UserModel user)
大家可以看到返回值类型是命令对象类型,而且通过@ModelAttribute("user2")注解,此时会暴露返回值到模型数据( 名字为user2 ) 中供视图展示使用
@ModelAttribute 注解的返回值会覆盖@RequestMapping 注解方法中的@ModelAttribute 注解的同名命令对象
SpringMvc中@ModelAttribute注解的使用的更多相关文章
- springmvc中ModelAttribute注解应用在参数中
可以用@ModelAttribute来注解方法参数或方法.带@ModelAttribute创建的参数对象会被添加到Model对象中.注解在参数上时,可以从Form表单或URL参数中获取参数并绑定到mo ...
- SpringMVC 中 @ControllerAdvice 注解
SpringMVC 中 @ControllerAdvice 注解 1.@ControllerAdvice 1.1 全局异常处理 1.2 全局数据绑定 1.3 全局数据预处理 原文地址: 江南一点雨:S ...
- SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...
- Spring MVC 中 @ModelAttribute 注解的妙用
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- SpringMVC 中 @ControllerAdvice 注解的三种使用场景!
@ControllerAdvice ,很多初学者可能都没有听说过这个注解,实际上,这是一个非常有用的注解,顾名思义,这是一个增强的 Controller.使用这个 Controller ,可以实现三个 ...
- SpringMvc中@ModelAttribute的运用
/** * 1. 有 @ModelAttribute 标记的方法, 会在每个目标方法执行之前被 SpringMVC 调用! * 2. @ModelAttribute 注解也可以来修饰目标方法 POJO ...
- Spring|SpringMVC中的注解
文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...
- SpringMVC的@ModelAttribute注解简单使用(用户修改信息)
例如有一个User对象,我们要修改他的值,但是不能修改他的密码!通过表单提交数据之后,password为null,会把原对象的passwod覆盖掉.这时候可以用@ModelAttribute注解处理. ...
随机推荐
- 词频统计 in office
ROSTCM6 1. http://www.writewords.org.uk/word_count.asp 2. http://darylkinsman.ca/tools/wordfreq.shtm ...
- 设计模式之——visitor模式
visitor模式,又叫访问者模式,把结构和数据分开,编写一个访问者,去访问数据结构中的元素,然后把对各元素的处理全部交给访问者类.这样,当需要增加新的处理时候,只需要编写新的 访问者类,让数据结构可 ...
- scrapy爬虫系列之六--模拟登录
功能点:如何发送携带cookie访问登录后的页面,如何发送post请求登录 爬取网站:bilibili.github 完整代码:https://files.cnblogs.com/files/book ...
- Django - Jsonp、CORS
一.同源策略 https://www.cnblogs.com/yuanchenqi/articles/7638956.html 同源策略(Same origin policy)是一种约定,它是浏览器最 ...
- SQL基础--查询之五--查询语句一般格式
SQL基础--查询之五--查询语句一般格式
- (1.2)DML增强功能-4大排名函数与top ties/tablesample
关键字:sql server窗口函数.分析函数.四大窗口函数 1.row_number() over( partition by column order by column) (1)测试数据 (2 ...
- 使用Atom预览markdown
1.打开任意.md文件(markdown源文件)2.windows : ctrl + shift + pmac : command + shift + p这条命令跟Sublime Text是一样的,打 ...
- Linq to Sharepoint--如何获取Linq Query 生成的CALM
我们知道Linq to sharepoint 实际最终还是转化成了CALM来对Sharepoint进行访问,那么我们怎样才能知道我们编写的Query语句最终转化成的CALM语句是什么样子呢. 我们可以 ...
- Linux下安装系统清理软件 BleachBit 1.4
sudo add-apt-repository ppa:n-muench/programs-ppasudo apt-get updatesudo apt-get install bleachbit
- Codeforces Round #532 (Div. 2) Solution
A. Roman and Browser 签到. #include <bits/stdc++.h> using namespace std; ]; int get(int b) { ]; ...