输出模型数据:

  • ModelAndView:处理方法返回值类型为 ModelAndView 时 , 其中包含视图和模型信息。方法体即可通过该对象添加模型数据 , 即 SpringMVC 会把 ModelAndView 中的 model 模型放入到 request 域对象中。
  • Map, Model, ModelMap:目标方法的入参中包含 Map, Model, ModelMap 类型的数据, 返回值是 String 类型,Spring MVC 会自动把 Map, Model, ModelMap转化成模型信息放入到 request 域对象中,把返回的 String 类型数据转化成视图信息。
  • @SessionAttributes:只能定义在 Class, interface , enum 上。@SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中。Spring MVC 将在模型中对应的属性暂存到 HttpSession 中。
    1. value:指定的属性名。可以是 String[] 类型的数据。
    2. type:模型属性对应的类型。可以是 Class<?>[] 类型的数据。
  • @ModelAttribute:方法入参标注该注解后 , 入参的对象就会放到数据模型中(暂时不太懂)。
 package com.itdoc.springmvc.entities;

 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map; /**
* @BLOG http://www.cnblogs.com/goodcheap
* @DESCRIBE
* @AUTHOR WángChéngDá
* @DATE 2017-03-09 13:55
*/
@Controller
/**
* @SessionAttributes 只能定义在 Class, interface , enum 上。
* @SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,
* 还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中。
* Spring MVC 将在模型中对应的属性暂存到 HttpSession 中。
* •value:指定的属性名。可以是 String[] 类型的数据。
* •type:模型属性对应的类型。可以是 Class<?>[] 类型的数据。
*/
//@SessionAttributes(value = {"time", "names"}, types = {String.class, User.class})
public class TestModelAndView { public static final String SUCCESS = "success"; @ModelAttribute
public void getUser(@RequestParam(value = "id", required = false) Integer id,
Map map) {
System.out.println("I am TestModelAndView's getUser method...");
if (id != null) {
//模拟从数据库获取对象
User user = new User(1, "Tom", 12, "123456", "a@aaa.com");
System.out.println("从数据库中获取一个对象: " + user);
map.put("user", user);
}
} @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
public String updateUser(User user) {
System.out.println("修改: " + user);
return SUCCESS;
} @RequestMapping("/getModel")
public String getModel(Model model) {
model.addAttribute("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
model.addAttribute("time", new Date());
model.addAttribute("school", "东北财经大学");
model.addAttribute("user", new User("Tom", 13));
return SUCCESS;
} @RequestMapping("/getModelMap")
public String getModelMap(ModelMap modelMap) {
List<String> strings = Arrays.asList("Tom", "Jack", "Lucy", "Mark");
modelMap.put("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
modelMap.put("time", new Date());
modelMap.put("school", "东北财经大学");
modelMap.put("user", new User("Tom", 13));
return SUCCESS;
} /**
* 目标方法的入参中包含 Map, Model, ModelMap 类型的数据, 返回值是 String 类型,
* Spring MVC 会自动把 Map, Model, ModelMap 转化成模型信息放入到 request 域对象中,
* 把返回的 String 类型数据转化成视图信息。
*
* @param map
* @return
*/
@RequestMapping("/getMap")
public String getMap(Map<String, Object> map, String name) {
map.put("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
map.put("time", new Date());
map.put("school", "东北财经大学");
map.put("user", new User("Tom", 13));
return SUCCESS;
} /**
* 目标的返回值是 ModelAndView, 其中包含视图和模型信息。
* SpringMVC 会把 ModelAndView 中的 model 模型放入到 request 域对象中。
*
* @return
*/
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
String viewName = SUCCESS;
ModelAndView mv = new ModelAndView(viewName);
mv.addObject("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
mv.addObject("time", new Date());
mv.addObject("school", "东北财经大学");
mv.addObject("user", new User("Tom", 13));
System.out.println(mv);
return mv;
}
}

最新文章

  1. form 提交数组的一些trick
  2. CentOS 安装 Dubbo 管理控制台
  3. linux GD库安装
  4. lambda 表达式
  5. VI下删除所有内容
  6. JQuery ajax返回JSON时的处理方式
  7. Codeforces Round #271 (Div. 2)
  8. java 十六进制数的转换
  9. Apache-Tika解析JPEG文档
  10. Quartz(任务调度)- 入门学习
  11. java se 另一博客
  12. 某电商网站线上drbd+heartbeat+nfs配置
  13. day04 JS
  14. 程序通过ReportViewer对象来调用reporting services并导出pdf文件
  15. day 11 装饰器
  16. emwin之基于某个事件或标志创建某个界面的一种方法
  17. DIYer最担心的事来了!CPU降价彻底无望
  18. 拦截$.ajax方法实现登录过期登录
  19. Cracking The Coding Interview 1.1
  20. 如何用JS获取页面上的所有标签

热门文章

  1. Hadoop启动后无法启动NodeManager
  2. laravel读excel
  3. ubuntu 关闭触控板
  4. jmeter上传视频图片附件
  5. Jenkins的pipeline脚本中获取git代码变更用户名和email
  6. Hessian 2.0 序列化协议 - Hessian 2.0 Serialization Protocol 翻译
  7. BZOJ 4276 [ONTAK2015]Bajtman i Okrągły Robin 费用流+线段树优化建图
  8. winform label去背景
  9. 剑指offer:跳台阶
  10. nopcommerce商城系统--安装nopCommerce