Pagination of data

Source: Internet
Author: User

Paging is required when there is too much content in the page to display, or if the amount of data is too large to be overloaded with memory.

Principle: Each time a certain amount of data is taken from the database, the JSP page is displayed

Realize:

① write a page with a class wrapper for pagination

② a page from the database and encapsulates the information into a paged Page object

③ the paging Page object to the Request object, session object, or ServletContext object property, as appropriate, for the JSP page to invoke

④ display paging data, pagination page number, previous page next page, jump page, etc. in JSP page

Here's a look at the specific code:

The package Cn.wzbrilliant.domain;import java.util.list;//interface for all paging related to the public class page {private List records; private int pagesize = 10;//number of record bars displayed per page private int pagenum;//The page number that the user wants to see is the current page number private int totalpage;//total pages private int    startindex;//the index of the start record for each page private int totalrecords;//total record number//display page number private int startpage;        private int endpage;        Public Page (int pagenum,int totalrecords) {this.pagenum = Pagenum;                This.totalrecords = totalrecords;        Calculates the index of the start record per page StartIndex = (pagenum-1) *pagesize;                Total pages Calculated totalpage = totalrecords%pagesize==0?totalrecords/pagesize: (totalrecords/pagesize+1);            The displayed page number 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; }    }

Remove a certain number of records from the database, here in MySQL, for example, the SQL statement is a select * from table name limit?,? two question marks starting and ending positions, respectively

Display the implementation code for page numbering, previous page next page, jump page, and so on in a JSP page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><!--pagination section---${page.pagenum} page/Total ${ Page.totalpage} page    <a href= "${pagecontext.request.contextpath}/servlet/showallmessageservlet" > Home </a>   <a href= "${pagecontext.request.contextpath}/servlet/showallmessageservlet?pagenum=${ PAGE.PAGENUM-1==0?1:PAGE.PAGENUM-1} "> Previous </a>   <c:foreach begin=" ${page.startpage} "end=" ${ Page.endpage} "var=" num "> <a href=" ${pagecontext.request.contextpath}/servlet/showallmessageservlet?pagen Um=${num} ">${num}</a> </c:forEach>    <a href=" ${pagecontext.request . Contextpath}/servlet/showallmessageservlet?pagenum=${page.pagenum+1>page.totalpage?page.totalpage: Page.pagenum+1} "> Next </a>   <a href=" ${pagecontext.request.contextpath}/servlet/    Showallmessageservlet?pagenum=${page.totalpage} "> Last </a> <select id=" S1 ">      <c:foreach begin= "1" end= "${page.totalpage}" var= "num" > <option value= "${num}" ${page.pagen Um==num? ' Selected= "Selected" ': '}>${num}</option> </c:forEach> </select> <a href= ' Javascri Pt:jump () "> Jumps </a> <script type=" Text/javascript "> Function jump () {var num = Document.getel            Ementbyid ("S1"). Value;      window.location.href= "${pagecontext.request.contextpath}/servlet/showallcustomersservlet?pagenum=" +num; }</script>



Blog Park Blog: Small basket under the flat

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Pagination of data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.