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. EntityFramework用法探索(八)事务处理

    使用 前文中描述的Retail示例 ,在Customer对象的Mapping中设置Name属性:我们构造一个有效的Customer对象,再构造一个无效的Name属性为空的对象. DomainModel ...

  2. Anaconda的安装和更新

    下载地址官网:https://www.anaconda.com/distribution/ 一.安装 二.下载安装完成后我们来检验一下是否安装成功 点击“开始” —— “Anaconda3(64-bi ...

  3. Git操作学习笔记

    根据廖雪峰老师git教程学习整理 这里需要辨析一下概念.Github是代码托管平台,是协作的工具;而Git是版本控制工具.Git不需要联网,在本机就可以使用 集中式版本控制系统与分布式版本控制系统 S ...

  4. F - 回转寿司 (权值线段树)

    题目链接:https://cn.vjudge.net/contest/281960#problem/F 题目大意:中文题目 具体思路:权值线段树,我们每次寻找的是满足 (i<j)   L< ...

  5. Adroid反编译资料收集

    Android反编译神器jadx的使用 https://blog.csdn.net/Fisher_3/article/details/78654450 Android 反编译利器,jadx 的高级技巧 ...

  6. Java的IO流——(七)

    目录结构:

  7. linux下mysql 5.7.22 安装

    二进制安装 1.下载https://dev.mysql.com/downloads/mysql/5.6.html#downloads 2.官方文档https://dev.mysql.com/doc/r ...

  8. 改变checkbox的默认样式

    针对于CheckBox默认样式的改变,和选中状态的改变 <label class="checkBox"><input type="checkbox&qu ...

  9. Linux Samba服务主配文件smb.conf中文详解【转】

    转自:https://blog.csdn.net/maotianwang/article/details/52524732 从网上找到描述比较详细的smb.conf中文解释: 服务名:smb 配置目录 ...

  10. win7下出现读不到移动硬盘的解决办法

    很多电脑会出现移动硬盘读不到,或者 读到部分盘的情况,那么下面我就为大家来一一解决这些情况: 方法一:    最常见的是硬盘供电不足导致 ,要么换一个硬盘盒子,要么给硬盘一个外加电源即可方法二:    ...