1. 程式人生 > 其它 >2.域物件共享資料

2.域物件共享資料

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