Spring的@ModelAttribute注解
1.
一、绑定请求参数到指定对象
- 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 注解的同名命令对象
2.
1.@ModelAttribute注释方法
例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用。
(1)@ModelAttribute注释void返回值的方法
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @ModelAttribute
- public void populateModel(@RequestParam String abc, Model model) {
- model.addAttribute("attributeName", abc);
- }
- @RequestMapping(value = "/helloWorld")
- public String helloWorld() {
- return "helloWorld";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
这个例子,在获得请求/helloWorld 后,populateModel方法在helloWorld方法之前先被调用,它把请求参数(/helloWorld?abc=text)加入到一个名为attributeName的model属性中,在它执行后helloWorld被调用,返回视图名helloWorld和model已由@ModelAttribute方法生产好了。
这个例子中model属性名称和model属性对象有model.addAttribute()实现,不过前提是要在方法中加入一个Model类型的参数。
当URL或者post中不包含次参数时,会报错,其实不需要这个方法,完全可以把请求的方法写成,这样缺少此参数也不会出错
- <span style="font-size:12px;"> @RequestMapping(value = "/helloWorld")
- public String helloWorld(String abc) {
- return "helloWorld";
- }</span>
- <span style="font-size:12px;">
- </span>
(2)@ModelAttribute注释返回具体类的方法
- <span style="font-size:12px;">@ModelAttribute
- public Account addAccount(@RequestParam String number) {
- return accountManager.findAccount(number);
- }
- </span>
- <span style="font-size:12px;">
- </span>
这种情况,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回Account类型,那么这个model属性的名称是account。
这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无须要特定的参数。
(3)@ModelAttribute(value="")注释返回具体类的方法
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @ModelAttribute("attributeName")
- public String addAccount(@RequestParam String abc) {
- return abc;
- }
- @RequestMapping(value = "/helloWorld")
- public String helloWorld() {
- return "helloWorld";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
这个例子中使用@ModelAttribute注释的value属性,来指定model属性的名称。model属性对象就是方法的返回值。它无须要特定的参数。
(4)@ModelAttribute和@RequestMapping同时注释一个方法
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @RequestMapping(value = "/helloWorld.do")
- @ModelAttribute("attributeName")
- public String helloWorld() {
- return "hi";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
这时这个方法的返回值并不是表示一个视图名称,而是model属性的值,视图名称由RequestToViewNameTranslator根据请求"/helloWorld.do"转换为逻辑视图helloWorld。
Model属性名称有@ModelAttribute(value=””)指定,相当于在request中封装了key=attributeName,value=hi。
2.@ModelAttribute注释一个方法的参数
(1)从model中获取
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @ModelAttribute("user")
- public User addAccount() {
- return new User("jz","123");
- }
- @RequestMapping(value = "/helloWorld")
- public String helloWorld(@ModelAttribute("user") User user) {
- user.setUserName("jizhou");
- return "helloWorld";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
在这个例子里,@ModelAttribute("user") User user注释方法参数,参数user的值来源于addAccount()方法中的model属性。
此时如果方法体没有标注@SessionAttributes("user"),那么scope为request,如果标注了,那么scope为session
(2)从Form表单或URL参数中获取(实际上,不做此注释也能拿到user对象)
- <span style="font-size:12px;">@Controller
- public class HelloWorldController {
- @RequestMapping(value = "/helloWorld")
- public String helloWorld(@ModelAttribute User user) {
- return "helloWorld";
- }
- }</span>
- <span style="font-size:12px;">
- </span>
Spring的@ModelAttribute注解的更多相关文章
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- Spring MVC @ModelAttribute注解
在一个Controller内,被@ModelAttribute标注的方法会在此controller的每个handler方法执行前被执行. 被@ModelAttribute标注的方法的参数绑定规则和普通 ...
- Spring Boot 2.0 教程 | @ModelAttribute 注解
欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ...
- Spring MVC 中 @ModelAttribute 注解的妙用
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
- spring mvc之@ModelAttribute注解
1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...
- Spring MVC SessionAttributes ModelAttribute注解
说明 本文主要针对 @SessionAttributes注解 和 @ModelAttribute注解的基础用法进行解析.至于为什么会将这两个注解放在一起,是因为它们之间还是有点影响的. @Sessio ...
- Spring @SessionAttributes注解 @ModelAttribute注解
一.@SessionAttribute详解 如果多个请求之间需要共享数据,就可以使用@SessionAttribute. 配置的方法: 在控制器类上标注@SessionAttribute. 配置需要共 ...
- Spring ModelAttribute注解失效原因
public String test(@RequestParam(value = "test") @ModelAttribute("test") String ...
- Spring MVC常用注解
cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...
随机推荐
- SpringMVC TaskExecutor线程池
一.配置jdbc.properties添加: #------------ Task ------------ task.core_pool_size=5 task.max_pool_size=50 t ...
- 参考 - spring boot 静态变量注入值
参考http://blog.csdn.net/zhayuyao/article/details/78553417 @Component public class A { private static ...
- ThreadPoolExecutor的execute源码分析
上一篇文章指出,ThreadPoolExecutor执行的步骤如下: 向线程池中添加任务,当任务数量少于corePoolSize时,会自动创建thead来处理这些任务: 当添加任务数大于corePoo ...
- Minimum Cost(最小费用最大流,好题)
Minimum Cost http://poj.org/problem?id=2516 Time Limit: 4000MS Memory Limit: 65536K Total Submissi ...
- jquery 赋值时不触发change事件解决
$("#optionsId").change(function(){ $("#selectOptionsText").val('测试'); }); $(&quo ...
- 3.滑雪-深搜&dp
//Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑 ...
- MySQL主从复制解决数据库单点问题
一.单个数据库服务器的缺点 数据库服务器存在单点问题: 数据库服务器资源无法满足增长的读写请求: 高峰时数据库连接数经常超过上限. 二.如何解决单点问题 增加额外的数据库服务器,组建数据库集群: 同一 ...
- ROS Learning-009 beginner_Tutorials ROS服务 和 ROS参数
ROS Indigo beginner_Tutorials-08 ROS服务 和 ROS参数 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14 ...
- php 使用html5 XHR2 上传文件 进度显示
思路:只要我们知道上传文件的总大小,还有上传过程中上传文件的大小,那么就可以实现进度显示了. 在html5中,XMLHttpRequest对象,传送数据的时候,progress事件用来返回进度信息. ...
- JSF控件的immediate属性和页面生命周期
JSF中的控件基本都有immediate属性,对于这个属性的使用总结如下,更详细内容可参考Oracle官方文档. 1,为了更好的理解immediate属性,先看一下JSF页面的生命周期: JSF页面的 ...