为什么大多程序在controller中给jsp传值时使用model.addAttribute()而不使用httpServeletRequest.setAttribute()?
事实上model数据,最终spring也是写到HttpServletRequest属性中,只是用model更符合mvc设计,减少各层间耦合。
@RequestMapping(value="/list",method=RequestMethod.GET) public String list(Model model) { //前台输出必须使用形如${ul.value.属性名 }的形式 model.addAttribute("userList",userList); return "user/userList"; } @RequestMapping(value="/userlist",method=RequestMethod.GET) public String list(HttpServletRequest request) { //前台输出必须使用形如${user.id }的形式 List<User> userList = new ArrayList<User>(); userList.add(new User("1","yonghu1","121","[email protected]")); userList.add(new User("2","yonghu2","122","[email protected]")); userList.add(new User("3","yonghu3","123","[email protected]")); request.setAttribute("userList", userList); return "user/userList"; }
原文地址:https://www.cnblogs.com/cnki/p/9059262.html
时间: 2024-09-30 07:17:55