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. JQ在光标处插入文字

    内容转载自网络这是一个JQ的扩展方法.在teatarea获得焦点时,往光标处插入文字,扩展代码如下 (function($){ $.fn.extend({ "insert":fun ...

  2. linux性能调试之iostat

    iostat用来监控调试linux系统的IO性能. 一般用法: iostat -xdct time_interval count [disk] -x:显示扩展统计项,如果不使用-x参数只会打印基本统计 ...

  3. websocket使用指南

    前言 最近在一个项目中,需要使用到websocket,之前对websocket不是很了解,于是就花了一点时间来熟悉websocket. 在浏览器与服务器通信间,传统的 HTTP 请求在某些场景下并不理 ...

  4. 详解Tomcat线程池原理及参数释义

    omcat线程池有如下参数: maxThreads, 最大线程数,tomcat能创建来处理请求的最大线程数 maxSpareTHreads, 最大空闲线程数,在最大空闲时间内活跃过,但现在处于空闲,若 ...

  5. 【HTML】 HTML基础知识 一些标签

    额..是这样的,去年为了学习web框架,自己开发了一个zbx配置管理的二次开发系统,当时从零开始接触web开发,也第一次接触到了前端的一些知识.其中最基础的就是html/css了.我把那部分笔记整理上 ...

  6. 由浅入深理解----java反射技术

    java反射机制详解 java反射机制是在运行状态下,对任意一个类可以获取该类的属性和方法,对任意一个对象可以调用其属性和方法.这种动态的获取信息和调用对象的方法的功能称为java的反射机制 clas ...

  7. windows+CMake+mingw 搭建c c++开发环境

    layout: post title: "windows+CMake+mingw 搭建c c++开发环境" date: 2018-03-30 22:23:06 tags: wind ...

  8. gitignore忽略规则

    我们用git提交本地代码时,有些文件或日志是不需要提交的,这个时候可以用.gitignore来解决这个问题: 首先,我们需要创建一个.gitignore文件,用命令输入 touch .gitignor ...

  9. 248&258--高级软件工程第三次作业

    0 小组成员 马帅 / 2017202110248 齐爽爽 / 2017282110258 1 项目 GitHub 地址 GitHub:https://github.com/whumashuai/QT ...

  10. 2017-2018-1 1623 bug终结者 冲刺005

    bug终结者 冲刺005 by 20162323 周楠 今日任务:理清游戏运行逻辑,GameView类为游戏核心代码 简要介绍 游戏中整个地图都是由数组组成 1.整个地图为16×16格,主要元素有墙. ...