index.html
1 <!DOCTYPE html> 2 <html lang="en" xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <h1>首页</h1><br> 9 <a th:href="@{/testRequestByServletAPI}">通过ServletAPI向request域共享数据</a><br><br><br> 10 11 <a th:href="@{/testRequestByModelAndView}">通过ModelAndView向request域共享数据</a><br><br><br> 12 13 14 <a th:href="@{/testRequestByModel}">通过Model向request域共享数据</a><br><br><br> 15 16 17 <a th:href="@{/testRequestByMap}">通过Map向request域共享数据</a><br><br><br> 18 19 <a th:href="@{/testRequestByModelMap}">通过ModelMap向request域共享数据</a><br><br><br> 20 </body> 21 </html>
Controller
1 package com.sunnny.Controller; 2 3 4 import org.springframework.stereotype.Controller; 5 import org.springframework.ui.Model; 6 import org.springframework.ui.ModelMap; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import javax.servlet.http.HttpServletRequest; 11 import java.util.Map; 12 13 @Controller 14 public class ScopeController { 15 16 17 18 19 //1.使用ServletAPI向request域对象共享数据 20 @RequestMapping("/testRequestByServletAPI") 21 public String testRequestByServletAPI(HttpServletRequest request){ 22 23 request.setAttribute("testRequestScope","hello,servletAPI"); 24 return "success"; 25 } 26 27 28 29 30 31 32 //2.使用ModelAndView向request域对象共享数据 33 @RequestMapping("/testRequestByModelAndView") 34 public ModelAndView testRequestByModelAndView(){ 35 /* 36 ModelAndView有Model和View的功能 37 Model主要用于向请求域共享数据 38 View主要用于设置视图,实现页面跳转 39 40 */ 41 ModelAndView mav=new ModelAndView(); 42 43 //向请求域共享数据 44 mav.addObject("testRequestScope","hello,ModelAndView"); 45 46 System.out.println("++++++++++"+mav.getClass().getName()); 47 //设置视图,实现页面跳转 48 mav.setViewName("success"); 49 50 return mav; 51 } 52 53 54 //3.使用Model向request域对象共享数据 55 @RequestMapping("/testRequestByModel") 56 public String testModel(Model model){ 57 58 model.addAttribute("testRequestScope","hello, Model"); 59 System.out.println("++++++++++"+model.getClass().getName()); 60 return "success"; 61 } 62 63 //4.使用map向request域对象共享数据 64 @RequestMapping("/testRequestByMap") 65 public String testMap(Map<String,Object> map){ 66 map.put("testRequestScope","hello Map"); 67 System.out.println("++++++++++"+map.getClass().getName()); 68 return "success"; 69 } 70 71 72 //5.使用ModelMap向request域对象共享数据 73 @RequestMapping("/testRequestByModelMap") 74 public String testModelMap(ModelMap modelMap){ 75 modelMap.addAttribute("testRequestScope","hello ModelMap"); 76 System.out.println("++++++++++"+modelMap.getClass().getName()); 77 return "success"; 78 } 79 80 81 82 83 84 }
success.html
1 <!DOCTYPE html> 2 <html lang="en" xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <h1>成功!!!</h1><br> 10 <p th:text="${testRequestScope}"></p><br><br><br> 11 12 </body> 13 </html>
1. 分别点击由上到下的连接:
(1)
(2)
(3)
(4)
(5)
2.分别打印每个对象的实现类的名字
可以看到,这几个对象使用了同一个实现类-来实例化的---BindingAwareModelMap