怎么在Java項目中實現(xiàn)一個分頁功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供臨海網(wǎng)站建設、臨海做網(wǎng)站、臨海網(wǎng)站設計、臨海網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、臨海企業(yè)網(wǎng)站模板建站服務,十年臨海做網(wǎng)站經驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
分頁對象
public class PageUtils implements Serializable { /** * */ private static final long serialVersionUID = -5247614532234782640L; public final static String PAGE = "page"; public final static String PAGE_NO = "pageno"; public final static String PAGE_SIZE = "pagesize"; private long pageSize=10;//每頁顯示記錄數(shù) private long firstResult=0;//當頁第一條記錄號 private long totalCount;//總記錄數(shù) private long totalPage;//總頁碼 private long pageNo=1;//當前頁碼 private List<?> sumData;//此集合可用來保存 合計數(shù)據(jù) private List<?> data;//查詢結果 public long getPageSize() { return pageSize; } public void setPageSize(long pageSize) { this.pageSize = pageSize; } public long getFirstResult() { if(pageNo>0){ firstResult=pageSize * (pageNo -1); }else{ firstResult = 0; } return firstResult; } public long getNextPageResult(){ if(pageNo>0){ return pageSize*(pageNo-1); }else{ return pageNo; } } public void setFirstResult(long firstResult) { this.firstResult = firstResult; } public long getTotalCount() { return totalCount; } public void setTotalCount(long totalCount) { this.totalCount = totalCount; totalPage = this.totalCount/pageSize; if (totalPage == 0 || totalCount % pageSize != 0) { totalPage++; } } public long getTotalPage() { return totalPage; } public void setTotalPage(long totalPage) { this.totalPage = totalPage; } public long getPageNo() { return pageNo; } public void setPageNo(long pageNo) { this.pageNo = pageNo; } public List<?> getData() { return data; } public void setData(List<?> data) { this.data = data; } /** * 是否第一頁 */ public boolean isFirstPage() { return pageNo <= 1; } /** * 是否最后一頁 */ public boolean isLastPage() { return pageNo >= getTotalPage(); } /** * 下一頁頁碼 */ public long getNextPage() { if (isLastPage()) { return pageNo; } else { return pageNo + 1; } } /** * 上一頁頁碼 */ public long getPrePage() { if (isFirstPage()) { return pageNo; } else { return pageNo - 1; } } public PageUtils(){} public PageUtils(long pageNo){ this.pageNo=pageNo; } public PageUtils(long pageNo,long pageSize){ this.pageNo=pageNo; this.pageSize = pageSize; } public List<?> getSumData() { return sumData; } public void setSumData(List<?> sumData) { this.sumData = sumData; } }
查詢的數(shù)據(jù)實體
在查詢的實體里添加頁碼和每頁顯示條數(shù)參數(shù);
private int pageSize; //每頁顯示的條數(shù) private int pageNo; //當前頁碼 public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; }
控制層Controller
@RequestMapping("/list") public String list(Model model,ChannelValueInfoView input) { // input:傳入的參數(shù)為對象 PageUtils page=new PageUtils(); //如果傳入的當前條數(shù)為0,則賦予值(首次查詢不帶參); if(input.getPageSize()==0){ //當前頁碼第一頁 input.setPageNo(1); //每頁顯示條數(shù),當前每頁顯示10條數(shù)據(jù); input.setPageSize(10); } page.setPageNo(input.getPageNo()); page.setPageSize(input.getPageSize()); //核心分頁代碼 PageHelper p=new PageHelper(); Page<ChannelValueInfoList> l=p.startPage(input.getPageNo(),input.getPageSize()); //緊跟著的第一個select查詢將會被分頁 channelValueService.getChannelValueInfoViewList(input); model.addAttribute("input", input); page.setData(l); page.setTotalCount(l.getTotal()); model.addAttribute("page", page); return "index"; }
頁面處理
//循環(huán)穿過來的PAGE.data數(shù)據(jù) <tr th:each="ts : ${page.data}"> <td th:text="${ts.channelValueName}"></td> ---------- <form id="content_form" action="/channelValue/list" method="post" > <div> 總數(shù):<span id="totalCount" th:text="${page.totalCount}">0</span> </div> <ul class="pagination"> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onFirst()">首頁</a> </li> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onPre()"><</a> </li> <li class="active"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <span id="beginRow" th:text="${page.pageNo}">0</span> </a> </li> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onNext()">></a> </li> <li class="disabled"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="onLast()">尾頁</a> </li> </ul> </for m> ---------- <script> function onFirst() { onList(1); } function onPre() { var beginRow = parseInt($('#beginRow').html()); if (beginRow - 1 > 0) { onList(beginRow - 1); } } function onNext() { var beginRow = parseInt($('#beginRow').html()); var totalCount = parseInt($('#totalCount').html()); var pageSize = parseInt($('#pageSize').val()); if (parseInt(totalCount / pageSize + 1) > beginRow + 1) { onList(beginRow+1); } } function onLast() { var totalCount = parseInt($('#totalCount').html()); var pageSize = parseInt($('#pageSize').val()); onList(parseInt(totalCount / pageSize + 1) - 1); } function onList(pageNo) { if (pageNo == 0) pageNo = 1; $('#pageNo').val(pageNo); $("#content_form").submit(); } </script>
關于怎么在Java項目中實現(xiàn)一個分頁功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。
分享題目:怎么在Java項目中實現(xiàn)一個分頁功能
分享網(wǎng)址:http://jinyejixie.com/article20/ijchjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、動態(tài)網(wǎng)站、做網(wǎng)站、全網(wǎng)營銷推廣、網(wǎng)站制作、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)