1.处理数据模型

  SpringMVC提供了几种途径出书模型数据

  

二:ModelAndView

1.介绍

  

2.index

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
测试
<br>
<a href="helloworld7/">ModelAndViewTest</a>
<br><br><br>
测试Servlet API
<br>
<a href="helloworld6/">test servlet API</a>
<br>
</body>
</html>

3.controller

 package com.spring.it;

 import java.util.Date;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class ModelAndViewTest {
@RequestMapping("/helloworld7")
public ModelAndView hello() {
String viewName="success";
ModelAndView modelAndView=new ModelAndView(viewName);
//添加模型数据
modelAndView.addObject("time", new Date());
return modelAndView;
}
}

4.success

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
success page
<br>
time:${requestScope.time}
</body>
</html>

5.效果

  

6.ps

  SpringMVC会把ModelAndView的model中的数据方法放到request 域对象中。

三:Model和Map

1.介绍

  一般传入map就可以了

  实际上,可以Model类型或者ModelMap类型。

  

2.controller

  map放在方法参数里,如果新建一个map是不会传入进参数的。

 package com.spring.it;

 import java.util.Arrays;
import java.util.Date;
import java.util.HashMap; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class ModelAndViewTest {
@RequestMapping("/helloworld8")
public String hello1(HashMap<String,Object> map) {
// HashMap<String,Object> map=new HashMap();
map.put("names", Arrays.asList("Tom","Jack","Bob"));
return "success";
} @RequestMapping("/helloworld7")
public ModelAndView hello2() {
String viewName="success";
ModelAndView modelAndView=new ModelAndView(viewName);
//添加模型数据
modelAndView.addObject("time", new Date());
return modelAndView;
}
}

3.index

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
测试MAP
<br>
<a href="helloworld8">MAP Test</a>
<br><br><br>
测试ModelAndViewTest
<br>
<a href="helloworld7">ModelAndViewTest</a>
<br><br><br>
测试Servlet API
<br>
<a href="helloworld6/">test servlet API</a>
<br>
</body>
</html>

4.success

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
success page
<br><br>
time:${requestScope.time}
<br><br>
names:${requestScope.names}
</body>
</html>

5.效果

  

四:@SessionAttributes之value

1.index

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
测试MAP
<br>
<a href="helloworld8">MAP Test</a>
<br><br><br>
测试ModelAndViewTest
<br>
<a href="helloworld7">ModelAndViewTest</a>
<br><br><br>
测试Servlet API
<br>
<a href="helloworld6/">test servlet API</a>
<br>
</body>
</html>

2.controller

  注解只能放到类上。

 package com.spring.it;

 import java.util.Arrays;
import java.util.Date;
import java.util.HashMap; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView; import com.spring.bean.Person; @SessionAttributes({"person"})
@Controller
public class ModelAndViewTest {
@RequestMapping("/helloworld8")
public String hello1(HashMap<String,Object> map) {
map.put("person", new Person("Tom", "12345", "tom#123.com", 18));
return "success";
}
}

3.success

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
success page
<br><br>
request person:${requestScope.person}
<br><br>
session person:${sessionScope.person}
</body>
</html>

4.效果

  

五:@SessionAttributes之types

1.介绍

  

2.insex

  不变

3.controller

 package com.spring.it;

 import java.util.Arrays;
import java.util.Date;
import java.util.HashMap; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView; import com.spring.bean.Person; @SessionAttributes(value={"person"},types= {String.class})
@Controller
public class ModelAndViewTest {
@RequestMapping("/helloworld8")
public String hello1(HashMap<String,Object> map) {
map.put("person", new Person("Tom", "12345", "tom#123.com", 18));
//说明types
map.put("school", "school value");
return "success";
}
}

4.seccess

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
success page
<br><br>
request person:${requestScope.person}
<br><br>
session person:${sessionScope.person}
<br><br>
request school:${requestScope.school}
<br><br>
session school:${sessionScope.school}
</body>
</html>

5.效果

  

010 处理模型数据(ModelAndView,Map Model,@SessionAttributes)的更多相关文章

  1. 5、处理模型数据ModelAndView、Map、Model以及@SessionAttributes注解

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

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

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

  3. [Spring MVC] 取控制器返回的ModelAndView/Map/Model/Request的对象

    ${key }页面可取, <input value="${key}"> 或者<%=request.getParameter("key")%&g ...

  4. SpringMVC 学习笔记(四) 处理模型数据

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

  5. Spring MVC—模型数据,转发重定向,静态资源处理方式

    Spring MVC处理模型数据 添加模型数据的方法 ModelAndView Map及Model SessionAttribute ModelAttribute Spring MVC转发和重定向 S ...

  6. SpringMVC学习 -- ModelAndView , Model , ModelMap , Map 及 @SessionAttributes 的使用

    输出模型数据: ModelAndView:处理方法返回值类型为 ModelAndView 时 , 其中包含视图和模型信息.方法体即可通过该对象添加模型数据 , 即 SpringMVC 会把 Model ...

  7. SpringMVC(二)--处理数据模型、ModelAndView、Model、Map、重定向、@ModelAttribute、

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

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

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

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

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

随机推荐

  1. Java SE之 Eclipse错误: 找不到或无法加载主类或项目无法编译10种解决大法!【摘抄】

    声明一下:此BUG确实经常困扰我许久,今日遇到很强大的一套解决办法,又怕原博主的网页以后查找不到,故此摘抄copy一份,望得各方侵权一事而谅解. 传送门先行送上:http://blog.csdn.ne ...

  2. JavaSE之概述

    作此篇是鉴于个人Java学习之需要,也便于日后进一步归纳与复习.  规定:      1 Java全面概述[囊括 Java工作原理,JVM方面知识,关键字(final,static,public,pr ...

  3. day1 查看当前目录命令:pwd

    用到查看当前目录的完整路径使用:pwd 物理路径和连接路径什么鬼?没明白暂时借鉴别人的记录下 显示当前目录的物理路径 pwd –P 1: [root@DB-Server init.d]# cd /et ...

  4. js 获取当前日期或者前、后N天yyyy-MM-dd的方法

    //js获取当前日期.当前日期前.后N天的标准年月日 //day=0为当前天,day=7为前7天,day=-7为当前日期的后7天 function getstartdate(day) {        ...

  5. 【转】scapy 构造以太网注入帧

    1. 描述 使用scapy进行以太网帧的注入,相对于RAW_SOCKET还是比较简单的.在讲述packet注入之前,先了解一下scapy伪造以太网帧的相关知识.下图为以太网帧格式和scapy对应的封装 ...

  6. Linux的7个运行级别

    0:关机 1:单用户(找回丢失密码)此模式下所有用户不需要密码即可登录,可用于重置密码 2:多用户状态没有网络服务 3:多用户状态有网络服务 ★ 4:系统未使用保留给用户 5:图形界面 ★ 6:系统重 ...

  7. Simulink--MATLAB中的一种可视化仿真工具

     Simulink是MATLAB中的一种可视化仿真工具, 是一种基于MATLAB的框图设计环境,是实现动态系统建模.仿真和分析的一个软件包,被广泛应用于线性系统.非线性系统.数字控制及数字信号处理的建 ...

  8. pt-online-schema-change VS oak-online-alter-table

    前言 在上篇文章中提到了MySQL 5.6 Online DDL,如果是MySQL 5.5的版本在DDL方面是要付出代价的,虽然已经有了Fast index Creation,但是在添加字段还是会锁表 ...

  9. Linux 查看CPU信息,机器型号,内存等信息

  10. redis拾趣(客户端连接,keys命令,数据备份,缓存有效期等)

    1.客户端连接 为了安全保护,redis支持绑定IP跟端口,这个通过conf配置文件中的bind跟port来设置. 绑定后登录client控制台时就需要写明ip(或者hostname)跟端口了,如: ...