SpringMVC学习(7):处理模型数据
来源:互联网 发布:淘宝收到法院传票 编辑:程序博客网 时间:2024/06/06 09:42
SpringMVC提供了1.ModelAndView作为返回值添加模型数据2.Map作为传入参数,在处理方法返回时将Map中的数据添加到模型中
一、ModelAndView
ModelAndView中既包含了视图信息也包含了模型数据,SpringMVC会把ModelAndView的model中数据放入到request 域对象中。
在处理方法中:
package springmvc;import java.util.Date;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import springmvc.User;@Controller@RequestMapping("/springmvc")public class HelloWorld {private static final String SUCCESS = "success";@RequestMapping("/testModelAndView")public ModelAndView testModelAndView() {String viewName = SUCCESS;ModelAndView modelAndView = new ModelAndView(viewName);//添加模型数据到ModelAndView中modelAndView.addObject("time", new Date());return modelAndView;}}在index.jsp中
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>SpringMVC</title></head><body><a href="springmvc/testModelAndView">Test ModelAndView</a></body></html>
在success,jsp中
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>SpringMVC</title></head><body><h4>success page</h4>time:${requestScope.time}<br><br></body></html>运行起来可以看到效果
二、Map
目标方法可以添加Map类型(实际上也可以是Model类型或者ModelMap类型)的参数
处理方法文件:
package springmvc;import java.util.Arrays;import java.util.Date;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import springmvc.User;@Controller@RequestMapping("/springmvc")public class HelloWorld {private static final String SUCCESS = "success";@RequestMapping("/testMap")public String testMap(Map<String, Object> map) {System.out.println(map.getClass().getName());map.put("names", Arrays.asList("Tom", "Jerry", "Mike"));return SUCCESS;}}index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>SpringMVC</title></head><body><a href="springmvc/testMap">Test Map</a></body></html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>SpringMVC</title></head><body><h4>success page</h4>names:${requestScope.names}<br><br></body></html>运行一下可以看到效果:
0 0
- SpringMVC学习(7):处理模型数据
- SpringMVC学习(四)-处理模型数据
- SpringMVC学习笔记(7):处理模型数据(2)
- springmvc-7,处理模型数据
- springMVC(7)------springMVC处理模型数据
- SpringMVC学习笔记(6):处理模型数据(1)
- SpringMVC 学习笔记(四) 处理模型数据
- 学习SpringMVC(七)之处理模型数据
- SpringMVC 学习笔记(四) 处理模型数据
- SpringMVC处理模型数据
- springmvc 处理模型数据
- springmvc 处理模型数据
- SpringMVC-处理模型数据
- SpringMVC处理模型数据
- SpringMVC--处理模型数据
- SpringMVC处理模型数据
- SpringMVC处理模型数据
- SpringMVC处理模型数据
- hdu1540线段shu
- RSA加密解密及RSA签名和验证
- Gvim的字体、颜色、背景设置
- 基于C语言Win32API的可视化界面的qaac【1】
- gvim关闭菜单栏和工具栏
- SpringMVC学习(7):处理模型数据
- Vim插件之YankRing
- 算法课第九周作业 | Arithmetic Slices
- 一个简单的弹出提示框的依赖库
- git配置路上的那些坑
- 排序算法
- 阿里巴巴 JAVA 开发手册
- python not 关键字
- 机器学习、深度学习、计算机视觉、自然语言处理及应用案例——干货分享(持续更新......)