Spring MVC 向页面传值-Map、Model和ModelMap https://www.cnblogs.com/caoyc/p/5635878.html,Spring MVC 向页面传值-Map、Model和ModelMap
Spring MVC 向页面传值-Map、Model和ModelMap
除了使用ModelAndView方式外。还可以使用Map、Model和ModelMap来向前台页面创造
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。使用后面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 }
结果:
,除了使用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 }
结果:

更多精彩