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. 二、主目录 Makefile 分析(2)

    2.7 编译选项---config.mk 代码 163 164 行 # load other configuration include $(TOPDIR)/config.mk 此段就是包含顶层目录下 ...

  2. 汕头市队赛 SRM 07 D 天才麻将少女kpm

    这道题放了很久还是回来补了 D 天才麻将少女KPM SRM 07 背景&&描述 天才麻将少女KPM立志要在日麻界闯出一番名堂.     KPM上周叒打了n场麻将,但她这次又没控分,而且 ...

  3. SAP笔记---非-现存任务/请求XXX上的请求锁定

    不管在SAP中的哪个系统在点击修改程序时都有可能出现以下图中的报错: 已找到解决办法,步骤如下: 1,se11中查看tlock表找到以上提到的请求号记录: 2,进入se16n,输入请求号,在事务代码输 ...

  4. POJ2516 Minimum Cost【最小费用最大流】

    题意: 有N个客户,M个仓库,和K种货物.已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用.判断所有的仓库能否满足所有客户的需求,如果可以,求出最少 ...

  5. MySQL5.7主从复制配置

    1 my.cnf文件 配置 binlog_format = ROW log_bin_trust_function_creators=1 log-error = /usr/local/mysql/dat ...

  6. mysql select 时间戳转标准时间写法

    select  FROM_UNIXTIME(create_time, '%Y-%m-%d %H:%i:%S') as create_time from tabName

  7. mysql原理~undo管理

    一 简介:undo管理 二 各版本说明 1 5.5     undo位置:默认ibdata1中,不支持独立表空间   缺点:大事务可能造成ibdata1暴涨,只能dump导出导入或者从新搭建  参数: ...

  8. JSP验证码。

    package com; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.aw ...

  9. python3字符串与文本处理

    每个程序都回涉及到文本处理,如拆分字符串.搜索.替换.词法分析等.许多任务都可以通过内建的字符串方法来轻松解决,但更复杂的操作就需要正则表达式来解决. 1.针对任意多的分隔符拆分字符串 In [1]: ...

  10. MHL技术剖析,比HDMI更强【转】

    转自:http://blog.chinaunix.net/uid-22030783-id-3294750.html MHL这个只是经常听说,没有见过的东西,现在已经非常火热了,我们才刚刚开始做,人家三 ...