1、自动注入Model参数,对model变量添加属性。
@Controller @RequestMapping("user") public class UserController { @RequestMapping(method=RequestMethod.GET) public String allUser(Model model){ List<String> list = new ArrayList<String>(); list.add("zhangsan"); list.add("lisi"); model.addAttribute("list", list); return "list"; } }
在页面中读出
<body> <ul> <c:forEach items="${list}" var="item"> <li>${item}</li> </c:forEach> </ul> </body>
2、新建一个ModelAndView
@RequestMapping(method=RequestMethod.GET) public ModelAndView allUsers(){ ModelAndView mav = new ModelAndView(); mav.setViewName("list"); List<String> list = new ArrayList<String>(); list.add("zhangsan"); list.add("lisi"); mav.addObject("list", list); return mav; }
时间: 2024-10-19 17:24:59