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. c++趣味之难以发现的bug

    这些bug可能够你喝一壶的. 1.被断言(assert)包含的代码 常发生在切换到release版本时,执行结果乖乖的,最终查找结果是assert括号里的代码在release下是不执行的. 发现:跟踪 ...

  2. Java面试宝典-2017

    Java面试宝典2017版 一. Java基础部分........................................................................... ...

  3. 使用 win10 的正确姿势

    17年9月初,写了第一篇<使用 win10 的正确姿势>,而现在半年多过去,觉得文章得更新一些了,索性直接来个第二版吧. -----2018.3.24 写 一. 重新定义桌面 我的桌面: ...

  4. java swing 下拉框与文本框

    import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; publi ...

  5. 深入解读 Js 中的面向对象编程

    前言:今天看了一篇文章觉得很不错,所以给大家分享一下,也许很多人都看过面向对象编程甚至写过这样博客,觉得面向对象编程就那样,没啥好说的,那可能是因为你对这方面知识已经了解,可以选择性跳过.那如果有更通 ...

  6. 设计模式 --> (6)原型模式

    原型(Prototype)模式 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知 ...

  7. java操作数据库的通用的类

    package cn.dao; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; ...

  8. dom4j 最常用最简单的用法(转)

    要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/目前最新dom4j包下载地址:http://nchc.dl.sourceforg ...

  9. 去除input的自动填充色

    input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #ffffff inset !important; }

  10. (译文)开始学习Vue.js特性--Scoped Slots

    什么是scoped slots A scoped slot is a special type of slot that functions as a reusable template (that ...