First:dao层不变。
//查询全部
List<User> findAll();
Second:service层添加形参
List<User> findAll(Integer page,Integer size);
@Override
public List<User> findAll(Integer page,Integer size) {
PageHelper.startPage(page,size);
return userDao.findAll();
}
Third:Controller实现
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "5") Integer size){
//调用service中的方法,得到查询结果
List<User> userList = userService.findAll(page,size);
PageInfo pageInfo = new PageInfo(userList);
ModelAndView mv = new ModelAndView();
mv.addObject("pageInfo",pageInfo);
mv.setViewName("user-list");
return mv;
}
Fourth:修改jsp参数
<c:forEach var="user" items="${pageInfo.list}">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="${pageContext.request.contextPath}/user/updateCheckedById.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
<a href="${pageContext.request.contextPath}/user/delUserById.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<a href="#" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=5" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum-1}&size=5">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageNum}&size=5">${pageNum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum+1}&size=5">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageSize}" aria-label="Next">尾页</a></li>
</ul>
Finally:调试页面
Tips:如果出现500提示内部服务器错误:
- PageInfo和PageHepler有没有弄错
- ${pageInfo.list}要加list方法