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. dense prediction

    Dense prediction  fully convolutional network for sementic segmentation 先用feature extractor 提特征,然后再使 ...

  2. HTML5的学习(二)HTML5标签

    3.按功能排列标签 (注:红色为HTML5不支持的,蓝色为HTML5新增的标签元素.)   3.1基本 标签 描述 HTML4 HTML5 <!--...--> 定义注释. √ √ < ...

  3. Pandas提取数据存入excel

    import pandas as pd import pymysql def connect_db(): MYSQL_HOSTS = '127.0.0.1' MYSQL_USER = 'root' M ...

  4. vue学习一:新建或打开vue项目(vue-cli2)

    vue-cli3的操作参考文章:vue/cli 3.0脚手架搭建,浅谈vue-cli 3 和 vue-cli 2的区别 1.前期准备: node.js环境,安装node npm或者cnpm(npm的淘 ...

  5. NSOperation 代码,阐述NSOperation一般功能和重要功能

    // // ViewController.m // 05-NSOperation // // Created by jerry on 15/9/5. // Copyright (c) 2015年 je ...

  6. C# List分页

    假设你每页10条数据当前是第3页 跳到第4页则:List.Skip((4-1)*10).Take(10) 本文来自SunShine,转载请标明出处: http://do.jhost.cn/sunshi ...

  7. 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令

    使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...

  8. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  9. Libevent源码分析—从使用Libevent库开始

    练习libevent库的使用,主要是几个API的调用顺序.根据event.h的开头注释部分可知,要使用libevent库,主要的几个API及调用顺序为:         event_base()初始化 ...

  10. Linux 入门记录:十二、Linux 权限机制【转】

    转自:https://www.cnblogs.com/mingc/p/7591287.html 一.权限 权限是操作系统用来限制资源访问的机制,权限一般分为读.写.执行. 系统中每个文件都拥有特定的权 ...