除了使用ModelAndView方式外。還可以使用Map、Model和ModelMap來向前台頁面創造
使用后面3種方式,都是在方法參數中,指定一個該類型的參數。例如:
Java代碼
1 @RequestMapping("/test") 2 public String test(Map<String,Object> map,Model model,ModelMap modelMap){ 3 4 map.put("names", Arrays.asList("caoyc","zhh","cjx")); 5 model.addAttribute("time", new Date()); 6 modelMap.addAttribute("city", "ChengDu"); 7 modelMap.put("gender", "male"); 8 return "hello"; 9 }
JSP頁面
1 1、time:${requestScope.time} 2 <br/>2、names:${requestScope.names } 3 <br/>3、city:${requestScope.city } 4 <br/>4、gender:${requestScope.gender }
結果:
【推薦】:我們一般使用Map就可以了