Spring中将一个对象数据显示在前端的列表中
Java核心代码,从userDao中获取对象users(包含用户名,性别,密码等属性),将这些属性全部显示在前台页面上,
返回值为:ModelAndView
@RequestMapping("/test") public ModelAndView test(){ System.out.println("sucess"); List<User> users=userDao.getAllUsers();//获取users对象 System.err.println(users); String viewName = "sucess";//跳转的页面,sucess.jsp ModelAndView modelAndView = new ModelAndView(viewName); modelAndView.addObject("list", users);//将uesrs放入到list中 return modelAndView; }
jsp前台代码,sucess.jsp,从requestScope从获取list对象后,使用JSTL的for:each循环输出到页面上。<table style="width:200px;height:auto;border:red 2px;"> <tr style="width:200px;height:30px;"> <td>学号</td> <td>姓名</td> <td>密码</td> </tr> <c:forEach items="${requestScope.list}" var="item"> <tr><td>${item.id}</td> <td>${item.username}</td> <td>${item.password}</td></tr> </c:forEach>