处理模型数据(ModelAndView,Map Model,@SessionAttributes)

1.处理数据模型

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

  

 

二:ModelAndView

1.介绍

  

 

2.index

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     测试
11     <br>
12     <a href="helloworld7/">ModelAndViewTest</a>
13     <br><br><br>
14     测试Servlet API
15     <br>
16     <a href="helloworld6/">test servlet API</a>
17     <br>
18 </body>
19 </html>

 

3.controller

 1 package com.spring.it;
 2 
 3 import java.util.Date;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.servlet.ModelAndView;
 8 
 9 @Controller
10 public class ModelAndViewTest {
11     @RequestMapping("/helloworld7")
12     public ModelAndView hello() {
13         String viewName="success";
14         ModelAndView modelAndView=new ModelAndView(viewName);
15         //添加模型数据
16         modelAndView.addObject("time", new Date());
17         return modelAndView;
18     }
19 }

 

4.success

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     success page
11     <br>
12     time:${requestScope.time}
13 </body>
14 </html>

 

5.效果

  

 

6.ps

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

 

三:Model和Map

1.介绍

  一般传入map就可以了

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

  

 

2.controller

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

 1 package com.spring.it;
 2 
 3 import java.util.Arrays;
 4 import java.util.Date;
 5 import java.util.HashMap;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.servlet.ModelAndView;
10 
11 @Controller
12 public class ModelAndViewTest {
13     @RequestMapping("/helloworld8")
14     public String hello1(HashMap<String,Object> map) {
15 //        HashMap<String,Object> map=new HashMap();
16         map.put("names", Arrays.asList("Tom","Jack","Bob"));
17         return "success";
18     }
19     
20     @RequestMapping("/helloworld7")
21     public ModelAndView hello2() {
22         String viewName="success";
23         ModelAndView modelAndView=new ModelAndView(viewName);
24         //添加模型数据
25         modelAndView.addObject("time", new Date());
26         return modelAndView;
27     }
28 }

 

3.index

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     测试MAP
11     <br>
12     <a href="helloworld8">MAP Test</a>
13     <br><br><br>
14     测试ModelAndViewTest
15     <br>
16     <a href="helloworld7">ModelAndViewTest</a>
17     <br><br><br>
18     测试Servlet API
19     <br>
20     <a href="helloworld6/">test servlet API</a>
21     <br>
22 </body>
23 </html>

 

4.success

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     success page
11     <br><br>
12     time:${requestScope.time}
13     <br><br>
14     names:${requestScope.names}
15 </body>
16 </html>

 

5.效果

  

 

四:@SessionAttributes之value

1.index

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     测试MAP
11     <br>
12     <a href="helloworld8">MAP Test</a>
13     <br><br><br>
14     测试ModelAndViewTest
15     <br>
16     <a href="helloworld7">ModelAndViewTest</a>
17     <br><br><br>
18     测试Servlet API
19     <br>
20     <a href="helloworld6/">test servlet API</a>
21     <br>
22 </body>
23 </html>

 

2.controller

  注解只能放到类上。

 1 package com.spring.it;
 2 
 3 import java.util.Arrays;
 4 import java.util.Date;
 5 import java.util.HashMap;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.SessionAttributes;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import com.spring.bean.Person;
13 
14 @SessionAttributes({"person"})
15 @Controller
16 public class ModelAndViewTest {
17     @RequestMapping("/helloworld8")
18     public String hello1(HashMap<String,Object> map) {
19         map.put("person", new Person("Tom", "12345", "tom#123.com", 18));
20         return "success";
21     }
22 }

 

3.success

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     success page
11     <br><br>
12     request person:${requestScope.person}
13     <br><br>
14     session person:${sessionScope.person}
15 </body>
16 </html>

 

4.效果、

  

 

五:@SessionAttributes之types

1.介绍

   

 

2.insex

  不变

 

3.controller

 1 package com.spring.it;
 2 
 3 import java.util.Arrays;
 4 import java.util.Date;
 5 import java.util.HashMap;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.SessionAttributes;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import com.spring.bean.Person;
13 
14 @SessionAttributes(value={"person"},types= {String.class})
15 @Controller
16 public class ModelAndViewTest {
17     @RequestMapping("/helloworld8")
18     public String hello1(HashMap<String,Object> map) {
19         map.put("person", new Person("Tom", "12345", "tom#123.com", 18));
20         //说明types
21         map.put("school", "school value");
22         return "success";
23     }
24 }

 

4.seccess

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     success page
11     <br><br>
12     request person:${requestScope.person}
13     <br><br>
14     session person:${sessionScope.person}
15     <br><br>
16     request school:${requestScope.school}
17     <br><br>
18     session school:${sessionScope.school}
19 </body>
20 </html>

 

5.效果

  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值