1. 前端模块
1 <div class='pagelist'> 2 <b>第<span>${pu.curPage}/${pu.totalPages }</span>页 <a 3 href="selectLogs?curPage=${pu.firstPage}&size=${pu.size}" 4 class='first'>首页</a> | 5 <c:if test="${pu.hasPrepage}"> 6 <a href="selectLogs?curPage=${pu.prePage}&size=${pu.size}" 7 class="right-font08">上一页</a> | 8 </c:if> 9 <c:if test="${pu.hasNextpage}"> 10 <a href="selectLogs?curPage=${pu.nextPage}&size=${pu.size}" 11 class="right-font08">下一页</a> | 12 </c:if> 13 <a href="selectLogs?curPage=${pu.lastPage}&size=${pu.size}" 14 class='last'>末页</a> 15 共 ${pu.totalCount}</b> 条 16 </div>
2 后台代码
1 /** 2 * 查询日志 3 */ 4 @RequestMapping("selectLogs") 5 public String selectLogs(HttpServletRequest req,HttpServletResponse resp) { 6 String curPage = req.getParameter("curPage"); 7 String size = req.getParameter("size"); 8 int totalCount = logService.findAll(); 9 PageUtil<User> pu = new PageUtil<>(curPage, size, totalCount); 10 List<Log> selectAll = logService.selectAll(pu.getStart(),pu.getSize()); 11 req.setAttribute("log", selectAll); 12 req.setAttribute("pu", pu); 13 14 return "logList";
3. pageUtil
1 package com.demo.common; 2 3 import java.util.List; 4 5 //分页工具 6 public class PageUtil<T> { 7 private int curPage;//当前页的页码 8 private int size;//每页条目 9 private int totalCount; //总条目数 10 private int totalPages;//总页数 11 private int start;//起始页条目数 12 private boolean hasPrepage;//当前页是否有上一页 13 private boolean hasNextpage;//当前页是否有下一页 14 private int prePage;//上一页 15 private int nextPage;//下一页 16 private int firstPage;//首页 17 private int lastPage;//尾页 18 19 List<T> list; 20 public List<T> getList() { 21 return list; 22 } 23 public void setList(List<T> list) { 24 this.list = list; 25 } 26 /** 27 * 28 * @param curPage 使用String 29 * @param size 30 * @param totalCount 31 */ 32 public PageUtil(String curPage, String size, int totalCount) { 33 //当前页为空 34 this.curPage = curPage==null?1:Integer.parseInt(curPage); 35 this.size = size==null?10:Integer.parseInt(size); 36 this.totalCount = totalCount; 37 this.totalPages=(int)(Math.ceil(this.totalCount*1.0/this.size)); 38 if(this.curPage<1){ 39 this.curPage=1; 40 } 41 if(this.curPage>this.totalPages){ 42 this.curPage=this.totalPages; 43 } 44 //如果当前页>1,当前页有上一页 45 if(this.curPage>1){ 46 this.hasPrepage=true; 47 this.prePage=this.curPage-1; 48 49 } 50 //如果当前页小于总页数 51 if(this.curPage<this.totalPages){ 52 this.hasNextpage=true; 53 this.nextPage=this.curPage+1; 54 } 55 // 56 this.start=(this.curPage-1)*this.size; 57 this.firstPage=1; 58 this.lastPage=this.totalPages; 59 } 60 public int getCurPage() { 61 return curPage; 62 } 63 public void setCurPage(int curPage) { 64 this.curPage = curPage; 65 } 66 public int getSize() { 67 return size; 68 } 69 public void setSize(int size) { 70 this.size = size; 71 } 72 public int getTotalCount() { 73 return totalCount; 74 } 75 public void setTotalCount(int totalCount) { 76 this.totalCount = totalCount; 77 } 78 public int getTotalPages() { 79 return totalPages; 80 } 81 public void setTotalPages(int totalPages) { 82 this.totalPages = totalPages; 83 } 84 public int getStart() { 85 return start; 86 } 87 public void setStart(int start) { 88 this.start = start; 89 } 90 public boolean isHasPrepage() { 91 return hasPrepage; 92 } 93 public void setHasPrepage(boolean hasPrepage) { 94 this.hasPrepage = hasPrepage; 95 } 96 public boolean isHasNextpage() { 97 return hasNextpage; 98 } 99 public void setHasNextpage(boolean hasNextpage) { 100 this.hasNextpage = hasNextpage; 101 } 102 public int getPrePage() { 103 return prePage; 104 } 105 public void setPrePage(int prePage) { 106 this.prePage = prePage; 107 } 108 public int getNextPage() { 109 return nextPage; 110 } 111 public void setNextPage(int nextPage) { 112 this.nextPage = nextPage; 113 } 114 public int getFirstPage() { 115 return firstPage; 116 } 117 public void setFirstPage(int firstPage) { 118 this.firstPage = firstPage; 119 } 120 public int getLastPage() { 121 return lastPage; 122 } 123 public void setLastPage(int lastPage) { 124 this.lastPage = lastPage; 125 } 126 @Override 127 public String toString() { 128 return "PageUtil [curPage=" + curPage + ", size=" + size + ", totalCount=" + totalCount + "]"; 129 } 130 131 132 }