Spring MVC提供了以下几种途径输出模型数据:
1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据;
2)Map及Model:处理方法入参为org.springframework.ui.Model、org.springframework.ui.ModelMap或java.util.Map时,处理方法返回时,Map中的数据会自动被添加到模型中;
3)@SessionAttributes:将模型中的某个属性暂存到HttpSeession中,以便多个请求之间可以共享这个属性;
4)@ModelAttribute:方法入参标注该注解后,入参的对象就会放到数据模型中。

在SpringMVC的hanlder类中用@ModelAttribute标注的方法,会在该handler内所有目标(action)方法执行之前被SpringMVC调用。

用法示例:

Account.java

 package com.dx.springlearn.entities;

 public class Account {
public Integer id;
private String username;
private String password;
private String registerDate;
private String registerIP; public Account() {
} public Account(Integer id, String username, String password, String registerDate, String registerIP) {
super();
this.id = id;
this.username = username;
this.password = password;
this.registerDate = registerDate;
this.registerIP = registerIP;
} public Account(String username, String password, String registerDate, String registerIP) {
super();
this.username = username;
this.password = password;
this.registerDate = registerDate;
this.registerIP = registerIP;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getRegisterDate() {
return registerDate;
} public void setRegisterDate(String registerDate) {
this.registerDate = registerDate;
} public String getRegisterIP() {
return registerIP;
} public void setRegisterIP(String registerIP) {
this.registerIP = registerIP;
} @Override
public String toString() {
return "Account [id=" + id + ", username=" + username + ", password=" + password + ", registerDate="
+ registerDate + ", registerIP=" + registerIP + "]";
} }

Handler类TestModelData.java

 package com.dx.springlearn.hanlders;

 import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.dx.springlearn.entities.Account; @Controller
public class TestModelData {
private final String SUCCESS = "success"; @ModelAttribute
public void getAccount(@RequestParam(name = "id", required = false) Integer id, Map<String, Object> map) {
if (id != null) {
System.out.println("read account(id=" + id + ") from db");
Account account = new Account(1, "tommy", "123456", "2018-01-20 21:56:09", "127.0.0.1");
map.put("account", account);
} else {
System.out.println("the acount id is null");
}
} @RequestMapping("/testModelAttribute")
public ModelAndView testModelAttribute(Account account) {
System.out.println("accept account:" + account);
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
modelAndView.addObject("currentTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); return modelAndView;
}
}

测试表单页面:

    <form id="form_testModelAttribute" action="testModelAttribute"
method="POST">
<input name="id" type="hidden" value="1"/>
username:<input name="username" type="text" value="tommy" /><br>
<!-- password:<input name="password" type="password" /><br> -->
register date:<input name="registerDate" type="text" value="2018-01-20 21:56:09" /><br>
register ip:<input name="registerIP" type="text" value="127.0.0.1"/><br>
<input type="submit" value="Submit"/>
</form>

除了handler目标方法参数第一个参数名称与存放到map中的对象名称一致外,也可以使用@ModelAttribute标注handler目标方法参数:

package com.dx.springlearn.hanlders;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.dx.springlearn.entities.Account; @Controller
public class TestModelData {
private final String SUCCESS = "success"; @ModelAttribute
public void getAccount(@RequestParam(name = "id", required = false) Integer id, Map<String, Object> map) {
if (id != null) {
System.out.println("read account(id=" + id + ") from db");
Account account = new Account(1, "tommy", "123456", "2018-01-20 21:56:09", "127.0.0.1");
map.put("testabc", account);
} else {
System.out.println("the acount id is null");
}
} @RequestMapping("/testModelAttribute")
public ModelAndView testModelAttribute(@ModelAttribute("testabc") Account account) {
System.out.println("accept account:" + account);
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
modelAndView.addObject("currentTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); return modelAndView;
}
}

运行流程:

1)运行有@ModelAttribute注解标注的方法:从数据库中取出对象,把对象放入到Map中,键值为:user;

2)SpringMVC从Map中取出User对象,并把表单的请求参数赋值给该User对象的对应属性;

3)SpringMVC把上述对象传入目标方法的参数(参数名称必须与Map中的键值一致)。

注意:在@ModelAttribute修饰的方法中,放入Map是的键类型要与目标方法入参类型一直,而且Map是的键名称要与目标方法入参的参数名一致。

SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute的更多相关文章

  1. SpringMVC(十):SpringMVC 处理输出模型数据之Map及Model

    Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...

  2. SpringMVC(九):SpringMVC 处理输出模型数据之ModelAndView

    Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...

  3. SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes

    Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...

  4. springmvc学习(五)——处理模型数据

    Spring MVC 提供了以下几种途径输出模型数据: ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据Map 及 Model: 入参 ...

  5. SpringMVC系列(六)处理模型数据

    Spring MVC 提供了以下几种途径输出模型数据:  ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加模型数据  Map 及 Model: ...

  6. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  7. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...

  8. Spring MVC 处理模型数据(@ModelAttribute)

    SpringMVC中的模型数据是非常重要的,因为MVC中的控制(C)请求处理业务逻辑来生成数据模型(M),而视图(V)就是为了渲染数据模型的数据. 直白来讲,上面这句话的意思就是:当有一个查询的请求, ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)--S ...

随机推荐

  1. IPFS开发团队是如何工作的?

    小编不是一个很八卦的人,连当红明星都认不全.不过,今天还是带领大家来扒一扒ipfs开发团队是如何工作的. 工作方式: 全体会议:每周一有一个全体会议,这个会议是提前安排好的一个日程 任务讨论:把大任务 ...

  2. 在js中实现新窗口打开页面

    我们都知道可以在html代码中使用<a href="xxxx" target="_blank"></a>这种方式来打开一个新的窗口打开一 ...

  3. poj-1146 ID codes

    Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  4. 插入排序—直接插入排序(Straight Insertion Sort)

    基本思想: 将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表.即:先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插插入到已入,直至整个序列有序为止. 要点: ...

  5. Java虚拟机16:Metaspace

    被废弃的持久代 想起之前面试的时候有面试官问起过我一个问题:Java 8为什么要废弃持久代即Metaspace的作用.由于当时使用的Java 7且研究重心不在JVM上,一下没有回答上来,今天突然想起这 ...

  6. alpha-咸鱼冲刺day8

    一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 正在进行页面整合.然后还有注册跟登陆的功能完善-- 四,问题困难 数据流程大概是搞定了.不过语法不是很熟悉,然后还有各种判定. ...

  7. 微信小程序测试总结

    概述 由于项目中,微信前端和后端对接出现错误.所以Alpha测试分为微信小程序前端,管理员web测试. 测试工具选择 微信小程序的前端使用微信小程序开发工具测试. 管理员web使用web测试. 测试工 ...

  8. 设计模式NO.2

    设计模式NO.2 本次博客内容为第二次设计模式的练习.根据老师的要求完成下列题目: 题目1 如果需要开发一个跨平台视频播放器,可以在不同操作系统平台(如Windows.Linux.UNIX等)上播放多 ...

  9. vue.js下载及安装配置

    环境 Deepin15.4 下载及配置 node下载地址:http://nodejs.cn/download/ 解压到文件夹 /home/maskerk/vue/ 下 设置软连接: $ ln -s / ...

  10. android数据库持久化框架, ormlite框架,

    前言 Android中内置了SQLite,但是对于数据库操作这块,非常的麻烦.其实可以试用第3方的数据库持久化框架对之进行结构上调整, 摆脱了访问数据库操作的细节,不用再去写复杂的SQL语句.虽然这样 ...