Java的分頁處理


1. 前端模塊

 1     <div class='pagelist'>
 2                 <b>第<span>${pu.curPage}/${pu.totalPages }</span>頁 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
15                     共&nbsp;&nbsp;${pu.totalCount}</b>&nbsp;&nbsp;條
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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM