1、model对象封装的数据 @GetMapping("/test") public String findAyUser(Model model){ model.addAttribute("message", "This is test for @Controller"); System.out.println("封装的model="+model); return "hello"; }
2、ModelAndView 对象封装的对象
ModelAndView modelAndView = new ModelAndView();
@GetMapping("/findAll") public ModelAndView findAll(Model model){ List<User> list = null; try { list = userService.findAll(); } catch (Exception e) { e.printStackTrace(); } for(User user:list){ System.out.println("id="+user.getId()); System.out.println("username="+user.getUsername()); System.out.println("password="+user.getPassword()); } modelAndView.addObject("list", list); modelAndView.setViewName("result"); return modelAndView; }
获取方式都是通过JSTL表达式获取,需要在JSP页面引入JSTL表达式
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
取值时如果是字符串直接通过${message} 获取
<body> ${message} </body>
如果是list集合就需要使用foreach遍历list结合数据,具体如下:
<c:forEach items="${list}" var="item"> <tr> <td>${item.id}</td> <td>${item.username}</td> <td>${item.password}</td> <td><a href="${pageContext.request.contextPath}/User/findAll?id=${item.id}">操作</a></td> </tr> </c:forEach>