1.pager类
package po; import java.util.List; public class Pager<T> { private int pageSize; private int currentPage; private int totalRecord; private int totalPage; private List<T> dataList; public Pager(int pageNum,int pageSize,List<T> sourceList) { if(sourceList == null || sourceList.isEmpty()){ return; } // 总记录条数 this.totalRecord = sourceList.size(); // 每页显示多少条记录 this.pageSize = pageSize; //获取总页数 this.totalPage = this.totalRecord / this.pageSize; if(this.totalRecord % this.pageSize !=0){ this.totalPage = this.totalPage + 1; } // 当前第几页数据 this.currentPage = this.totalPage < pageNum ? this.totalPage : pageNum; // 起始索引 int fromIndex = this.pageSize * (this.currentPage -1); // 结束索引 int toIndex = this.pageSize * this.currentPage > this.totalRecord ? this.totalRecord : this.pageSize * this.currentPage; this.dataList = sourceList.subList(fromIndex, toIndex); } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<T> getDataList() { return dataList; } public void setDataList(List<T> dataList) { this.dataList = dataList; } }
2.service
public Pager<Datebase> findCommodityOrder( List<Datebase> allDatebaseList, int pageNum, int pageSize) throws SQLException { DatebaseDAO dao = new DatebaseDAO(); Pager<Datebase> pager = new Pager<Datebase>(pageNum, pageSize, allDatebaseList); return pager; }
3.servlet
String pageNumStr = request.getParameter("pageNum"); int pageNum = 1; //显示第几页数据 if(pageNumStr!=null && !"".equals(pageNumStr.trim())){ pageNum = Integer.parseInt(pageNumStr); } int pageSize = 5; // 每页显示多少条记录 String pageSizeStr = request.getParameter("pageSize"); if(pageSizeStr!=null && !"".equals(pageSizeStr.trim())){ pageSize = Integer.parseInt(pageSizeStr); } Pager<Datebase> result; try { result = service.findCommodityOrder(service.selectall(), pageNum, pageSize); HttpSession session=request.getSession(false); if(session == null) { session=request.getSession(true); } session.setAttribute("result", result); response.sendRedirect("5.jsp"); } catch (SQLException e) { e.printStackTrace(); }
4.jsp
<script type="text/javascript"> // 当前第几页数据 var currentPage = ${result.currentPage}; // 总页数 var totalPage = ${result.totalPage}; function submitForm(actionUrl){ window.location.href=actionUrl; } // 第一页 function firstPage(){ if(currentPage == 1){ alert("已经是第一页数据"); return false; }else{ submitForm("datebaseServlet?flag=5&pageNum=1"); return true; } } // 下一页 function nextPage(){ if(currentPage == totalPage){ alert("已经是最后一页数据"); return false; }else{ submitForm("datebaseServlet?flag=5&pageNum=" + (currentPage+1)); return true; } } // 上一页 function previousPage(){ if(currentPage == 1){ alert("已经是第一页数据"); return false; }else{ submitForm("datebaseServlet?flag=5&pageNum=" + (currentPage-1)); return true; } } // 尾页 function lastPage(){ if(currentPage == totalPage){ alert("已经是最后一页数据"); return false; }else{ submitForm("datebaseServlet?flag=5&pageNum="+totalPage); return true; } } </script>
获取数据 ${result.dataList }
<br> 共${result.totalRecord }条记录共${result.totalPage }页 当前第${result.currentPage }页 <a href="#" onclick="firstPage();">首页</a> <a href="#" onclick="nextPage();">下一页</a> <a href="#" onclick="previousPage();">上一页</a> <a href="#" onclick="lastPage();">尾页</a>