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. 第19月第2天 cellForItemAtIndexPath 返回空

    1. 动画需要获取当前的 Cell ,下面的调用在 viewDidLoad 中,有时候返回 nil,有时候成功. UICollectionView *cell = (UICollectionView* ...

  2. day2 查看文件目录命令:ls

    查看当前文件夹下面多有的目录文件ls 查看当前目录下面所有的文件,包括隐藏的文件ls -a(或者两个一样ls -all) 显示除"."和".."外的所有文件ls ...

  3. JS堆栈与拷贝

    一.堆栈的定义 1.栈是一种特殊的线性表.其特殊性在于限定插入和删除数据元素的操作只能在线性表的一端进行. 结论:后进先出(Last In First Out),简称为LIFO线性表.栈的应用有:数制 ...

  4. magelinux(0111)

    Web Service 应用层:http, https 实现某类具体应用: 传输层协议:TCP, UDP, SCTP IANA: 0-1023:众所周知,永久地分配给固定的应用使用,特权端口: 102 ...

  5. A - 最大子矩阵 HYSBZ - 1084 (DP)

    题目链接:https://cn.vjudge.net/contest/281963#problem/A 题目大意:中文题目 具体思路:观察到m<=2,所以我们可以对两种情况进行单独讨论,当m== ...

  6. JavaScript练习 - 正反选练习

    正反选练习 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. Android测试技能树

    Android 基础知识 Android 的体系结构 apk 的组成结构 adb 命令的使用 Android 的四大组件 Activity 的生命周期 … 测试/开发环境的准备 JDK 安装 SDK ...

  8. js中,for循环里面放ajax,ajax访问不到变量以及每次循环获取不到数据问题总结

    想在点击"终端控制"的时候能够开启多个窗口对多个终端进行管理: /**提交事件**/ $("#terminalControl").bind("clic ...

  9. linux笔记_day05

    1.bash以及特性 shell:外壳 GUI:KDE,Gnome,Xfce CLI:sh,csh,ksh,bash(born again shell) 进程:在每个进程看来,当前主机上只存在内核和当 ...

  10. ROS 错误之 [rospack] Error: package 'beginner_tutorials' not found

    ubuntu 下面情况处理 $ cd $gedit .bashrc 再后面加入两行 source /opt/ros/indigo/setup.bash source /home/lv/catkin_w ...