SpringMVC参数返回给页面
springMVC将后台参数返回给前台页面
方法一:利用ModelAndView返回值
1 @RequestMapping("/returnPage.do") 2 public ModelAndView returnPage(){ 3 Person person = new Person(); 4 person.setId(1); 5 person.setName("zs"); 6 person.setGender(2); 7 person.setAge(25); 8 person.setBirthday(new Date()); 9 Map<String,Object> map = new HashMap<String, Object>(); 10 map.put("person", person); 11 return new ModelAndView("returnPage",map); 12 }
方法二:利用方法中的参数
1 @RequestMapping("/returnPage1.do") 2 public String returnPage1(Map<String,Object> map){ 3 Person person = new Person(); 4 person.setId(1); 5 person.setName("zs"); 6 person.setGender(2); 7 person.setAge(25); 8 person.setBirthday(new Date()); 9 map.put("person", person); 10 return "returnPage"; 11 }
方法三:利用Model
1 @RequestMapping("/returnPage2") 2 public String returnPage2(Model model){ 3 Person person = new Person(); 4 person.setId(1); 5 person.setName("zs"); 6 person.setGender(2); 7 person.setAge(25); 8 person.setBirthday(new Date()); 9 model.addAttribute("person", person); 10 return "returnPage"; 11 }
前台页面显示内容
1 ${person.id }<br> 2 ${person.name }<br> 3 ${person.gender }<br> 4 ${person.age }<br> 5 ${person.birthday }<br> 6 <fmt:formatDate value="${person.birthday }" pattern="yyyy-MM-dd"/>