資料的分頁處理,資料分頁處理

來源:互聯網
上載者:User

資料的分頁處理,資料分頁處理

當頁面中要顯示的內容過多需要分多頁顯示、或是資料量過大記憶體吃不消時,需要分頁處理。

原理:每次從資料庫中取出一定量的資料,通過jsp頁面顯示

實現:

①寫一個類封裝分頁的頁面

②從資料庫中取出一個頁面的資料,將資訊封裝到分頁頁面對象中

③根據情況,將分頁的頁面對象設定到request對象、session對象或servletContext對象屬性中,供jsp頁面調用

④在jsp頁面中顯示分頁資料、分頁頁碼、上一頁下一頁、跳轉頁面等

下面看具體代碼:

分頁頁面Page類:

package cn.wzbrilliant.domain;import java.util.List;//介面上所有與分頁有關的都找此類要public class Page {    private List records;    private int pagesize = 10;//每頁顯示的記錄條數    private int pagenum;//使用者要看的頁碼即當前頁碼    private int totalpage;//總頁數    private int startIndex;//每頁開始記錄的索引    private int totalrecords;//總記錄條數        //顯示的頁碼    private int startPage;    private int endPage;        public Page(int pagenum,int totalrecords){        this.pagenum = pagenum;        this.totalrecords = totalrecords;                //計算每頁開始記錄的索引        startIndex = (pagenum-1)*pagesize;        //計算總頁數        totalpage = totalrecords%pagesize==0?totalrecords/pagesize:(totalrecords/pagesize+1);                //顯示的頁碼        if(totalpage<=9){            startPage = 1;            endPage = totalpage;        }else{            startPage = pagenum-4;            endPage = pagenum+4;            if(startPage<1){                startPage = 1;                endPage = 9;            }            if(endPage>totalpage){                endPage = totalpage;                startPage = totalpage-8;            }        }    }            public List getRecords() {        return records;    }    public void setRecords(List records) {        this.records = records;    }    public int getPagesize() {        return pagesize;    }    public void setPagesize(int pagesize) {        this.pagesize = pagesize;    }    public int getPagenum() {        return pagenum;    }    public void setPagenum(int pagenum) {        this.pagenum = pagenum;    }    public int getTotalpage() {        return totalpage;    }    public void setTotalpage(int totalpage) {        this.totalpage = totalpage;    }    public int getStartIndex() {        return startIndex;    }    public void setStartIndex(int startIndex) {        this.startIndex = startIndex;    }    public int getTotalrecords() {        return totalrecords;    }    public void setTotalrecords(int totalrecords) {        this.totalrecords = totalrecords;    }    public int getStartPage() {        return startPage;    }    public void setStartPage(int startPage) {        this.startPage = startPage;    }    public int getEndPage() {        return endPage;    }    public void setEndPage(int endPage) {        this.endPage = endPage;    }    }

從資料庫中取出一定條數的記錄,此處以mysql為例,sql語句為select * from 表名 limit ?,?   兩個問號分別為起始位置和結束位置

 

在jsp頁面中顯示分頁頁碼、上一頁下一頁、跳轉頁面等的實現代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!-- 分頁的部分 --> 第${page.pagenum}頁/共${page.totalpage}頁   <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet">首頁</a>   <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${page.pagenum-1==0?1:page.pagenum-1}">上一頁</a>        <c:forEach begin="${page.startPage}" end="${page.endPage}" var="num">            <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${num}">${num }</a>      </c:forEach>                        <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${page.pagenum+1>page.totalpage?page.totalpage:page.pagenum+1}">下一頁</a>      <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${page.totalpage}">尾頁</a>    <select id="s1">          <c:forEach begin="1" end="${page.totalpage}" var="num">                 <option value="${num}" ${page.pagenum==num?'selected="selected"':''}>${num}</option>          </c:forEach>     </select>     <a href="javascript:jump()">跳轉</a> <script type="text/javascript">      function jump(){            var num = document.getElementById("s1").value;            window.location.href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum="+num;      }</script>



部落格園部落格:欠扁的小籃子

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.