輸出模型數據:
- ModelAndView:處理方法返回值類型為 ModelAndView 時 , 其中包含視圖和模型信息。方法體即可通過該對象添加模型數據 , 即 SpringMVC 會把 ModelAndView 中的 model 模型放入到 request 域對象中。
- Map, Model, ModelMap:目標方法的入參中包含 Map, Model, ModelMap 類型的數據, 返回值是 String 類型,Spring MVC 會自動把 Map, Model, ModelMap轉化成模型信息放入到 request 域對象中,把返回的 String 類型數據轉化成視圖信息。
- @SessionAttributes:只能定義在 Class, interface , enum 上。@SessionAttributes 除了可以通過屬性名指定需要放到會話中的屬性外,還可以通過模型屬性的對象類型指定哪些模型屬性需要放到會話中。Spring MVC 將在模型中對應的屬性暫存到 HttpSession 中。
- value:指定的屬性名。可以是 String[] 類型的數據。
- type:模型屬性對應的類型。可以是 Class<?>[] 類型的數據。
- @ModelAttribute:方法入參標注該注解后 , 入參的對象就會放到數據模型中(暫時不太懂)。
1 package com.itdoc.springmvc.entities; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.ui.ModelMap; 6 import org.springframework.web.bind.annotation.*; 7 import org.springframework.web.servlet.ModelAndView; 8 9 import java.util.Arrays; 10 import java.util.Date; 11 import java.util.List; 12 import java.util.Map; 13 14 /** 15 * @BLOG http://www.cnblogs.com/goodcheap 16 * @DESCRIBE 17 * @AUTHOR WángChéngDá 18 * @DATE 2017-03-09 13:55 19 */ 20 @Controller 21 /** 22 * @SessionAttributes 只能定義在 Class, interface , enum 上。 23 * @SessionAttributes 除了可以通過屬性名指定需要放到會話中的屬性外, 24 * 還可以通過模型屬性的對象類型指定哪些模型屬性需要放到會話中。 25 * Spring MVC 將在模型中對應的屬性暫存到 HttpSession 中。 26 * •value:指定的屬性名。可以是 String[] 類型的數據。 27 * •type:模型屬性對應的類型。可以是 Class<?>[] 類型的數據。 28 */ 29 //@SessionAttributes(value = {"time", "names"}, types = {String.class, User.class}) 30 public class TestModelAndView { 31 32 public static final String SUCCESS = "success"; 33 34 @ModelAttribute 35 public void getUser(@RequestParam(value = "id", required = false) Integer id, 36 Map map) { 37 System.out.println("I am TestModelAndView's getUser method..."); 38 if (id != null) { 39 //模擬從數據庫獲取對象 40 User user = new User(1, "Tom", 12, "123456", "a@aaa.com"); 41 System.out.println("從數據庫中獲取一個對象: " + user); 42 map.put("user", user); 43 } 44 } 45 46 47 @RequestMapping(value = "/updateUser", method = RequestMethod.POST) 48 public String updateUser(User user) { 49 System.out.println("修改: " + user); 50 return SUCCESS; 51 } 52 53 @RequestMapping("/getModel") 54 public String getModel(Model model) { 55 model.addAttribute("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark")); 56 model.addAttribute("time", new Date()); 57 model.addAttribute("school", "東北財經大學"); 58 model.addAttribute("user", new User("Tom", 13)); 59 return SUCCESS; 60 } 61 62 @RequestMapping("/getModelMap") 63 public String getModelMap(ModelMap modelMap) { 64 List<String> strings = Arrays.asList("Tom", "Jack", "Lucy", "Mark"); 65 modelMap.put("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark")); 66 modelMap.put("time", new Date()); 67 modelMap.put("school", "東北財經大學"); 68 modelMap.put("user", new User("Tom", 13)); 69 return SUCCESS; 70 } 71 72 /** 73 * 目標方法的入參中包含 Map, Model, ModelMap 類型的數據, 返回值是 String 類型, 74 * Spring MVC 會自動把 Map, Model, ModelMap 轉化成模型信息放入到 request 域對象中, 75 * 把返回的 String 類型數據轉化成視圖信息。 76 * 77 * @param map 78 * @return 79 */ 80 @RequestMapping("/getMap") 81 public String getMap(Map<String, Object> map, String name) { 82 map.put("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark")); 83 map.put("time", new Date()); 84 map.put("school", "東北財經大學"); 85 map.put("user", new User("Tom", 13)); 86 return SUCCESS; 87 } 88 89 /** 90 * 目標的返回值是 ModelAndView, 其中包含視圖和模型信息。 91 * SpringMVC 會把 ModelAndView 中的 model 模型放入到 request 域對象中。 92 * 93 * @return 94 */ 95 @RequestMapping("/testModelAndView") 96 public ModelAndView testModelAndView() { 97 String viewName = SUCCESS; 98 ModelAndView mv = new ModelAndView(viewName); 99 mv.addObject("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark")); 100 mv.addObject("time", new Date()); 101 mv.addObject("school", "東北財經大學"); 102 mv.addObject("user", new User("Tom", 13)); 103 System.out.println(mv); 104 return mv; 105 } 106 }