PageInfo.java 存储页数信息
public class PageInfo<T> {
// 当前页需要展示的数据
private List<T> list;
private int totalPage; // 总页数
private int totalCount; // 总记录数
private int size; // 每页数据量
private int currentPage; // 当前页数
public PageInfo() {
}
public PageInfo(List<T> list, int totalPage, int totalCount, int size, int currentPage) {
this.list = list;
this.totalPage = totalPage;
this.totalCount = totalCount;
this.size = size;
this.currentPage = currentPage;
}
}
Handler.java
@RequestMapping("/findAll03.do")
public ModelAndView findAllByPage(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "0") int type,String username,HttpSession session) {
if (type==1){
session.setAttribute("username",username);
}
else {
session.setAttribute("username", null);
}
ModelAndView mv = new ModelAndView();
PageInfo<User> pageInfo = userService.findByPage(currentPage,username);
System.out.println(pageInfo);
mv.setViewName("page/dataList");
mv.addObject("pageInfo", pageInfo);
return mv;
}
userServiceImpl.java
@Override
public PageInfo<User> findByPage(int currentPage,String username){
PageInfo<User> pageInfo = new PageInfo<>();
// 指定每页的数据量
pageInfo.setSize(5);
// 指定总数据量
int totalCount = userDao.getTotal(username);
pageInfo.setTotalCount(totalCount);
// 指定总页数 ceil() 向上取整 floor() 向下取整 round() 四舍五入
int totalPage = (int) Math.ceil(totalCount / (double)pageInfo.getSize());
pageInfo.setTotalPage(totalPage);
// 判断当前页是否合理
if (currentPage < 1) {
pageInfo.setCurrentPage(1);
} else if (currentPage > totalPage) {
pageInfo.setCurrentPage(totalPage);
} else {
pageInfo.setCurrentPage(currentPage);
}
// 指定当前页的数据 指定sql语句中的两个参数
int start = (pageInfo.getCurrentPage() - 1) * pageInfo.getSize();
List<User> list = userDao.findAllByPage(start, pageInfo.getSize(),username);
pageInfo.setList(list);
return pageInfo;
}
dataList.jsp
<table id="dataList" class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right:0px;">
<input id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th class="sorting_asc">ID</th>
<th class="sorting_desc">用户名</th>
<th class="sorting_asc sorting_asc_disabled">密码</th>
<th class="sorting_desc sorting_desc_disabled">年龄</th>
</tr>
</thead>
<tbody>
<%-- c:forEach JSTL 提供的遍历标签 --%>
<c:forEach items="${pageInfo.list}" var="user">
<tr>
<td><input name="ids" type="checkbox" value="${user.id}"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="box-footer">
<div class="pull-left">
<div class="form-group form-inline">总共${pageInfo.totalPage} 页,共${pageInfo.totalCount} 条数据。 每页
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select> 条
</div>
</div>
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=1" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageInfo.currentPage - 1}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="pageNum">
<li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageNum}">${pageNum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageInfo.currentPage + 1}">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageInfo.totalPage}" aria-label="Next">尾页</a></li>
</ul>
</div>
</div>