Spring MVC中的@ModelAttribute
来源:互联网 发布:无线游戏鼠标 知乎 编辑:程序博客网 时间:2024/06/02 02:17
@ModelAttribute 绑定请求参数到命令对象
@ModelAttribute一个具有如下三个作用:
①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑
定流程,而且自动暴露为模型数据用于视图页面展示时使用;
②暴露表单引用对象为模型数据:放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用
对象,如注册时需要选择的所在城市等,而且在执行功能处理方法(@RequestMapping 注解的方法)之前,自动添加
到模型对象中,用于视图页面展示时使用;
③暴露@RequestMapping 方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为
模型数据,用于视图页面展示时使用。
一、绑定请求参数到指定对象
- 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 注解的同名命令对象
转自: http://hbiao68.iteye.com/blog/1948380
- Spring MVC中的@ModelAttribute
- Spring MVC @ModelAttribute 详解
- [Spring MVC] - @ModelAttribute使用
- Spring MVC -----@ModelAttribute注解
- Spring MVC @ModelAttribute
- Spring MVC @ModelAttribute 详解
- [Spring MVC] - @ModelAttribute使用
- Spring MVC - @ModelAttribute使用
- Spring MVC 07 - @ModelAttribute
- Spring MVC @ModelAttribute 详解
- Spring MVC @ModelAttribute 使用
- spring mvc @ModelAttribute使用
- Spring MVC @ModelAttribute详解
- Spring MVC @ModelAttribute 详解
- Spring MVC @ModelAttribute 详解
- Spring MVC @ModelAttribute 详解
- spring mvc @ModelAttribute注解使用
- Spring MVC之@ModelAttribute详解
- 原型模式
- Android四大组件-内容提供者
- Zurmo开发的小技巧(8)
- QML之Grid网格定位器
- eclipse maven项目中修改 tomcat 部署路径
- Spring MVC中的@ModelAttribute
- scrollview 嵌套gridview 和 listview 会有冲突
- Spring RestTemplate关联HttpClient4.5的配置HttpClient和自身的BUG
- maven项目读取resources目录下文件
- 通过反射得到一个类的对象,如何对该对象对应的类的父类中的变量赋值
- Android多分辨率布局,多个layout
- I/O多路复用之select函数
- Python数据采集之Scrapy框架
- 嵌入式Linux中的根文件系统