Data paging and data Paging

Source: Internet
Author: User

Data paging and data Paging

When too much content needs to be displayed on the page, multiple pages need to be displayed, or the amount of data is too large for memory to eat up, it needs to be processed by page.

Principle: each time a certain amount of data is retrieved from the database, it is displayed on the jsp page.

Implementation:

① Write a page with a class encapsulation page

② Retrieve the data on a page from the database and encapsulate the information in the page object.

③ Set the pagination Page Object to the request object, session object, or servletContext object attribute for the jsp page to call.

④ Display paging data, page number, previous page, and jump page on the jsp page

The Code is as follows:

Pagination Page class:

Package cn. wzbrilliant. domain; import java. util. list; // This class is required for all paging-related objects on the interface. public class Page {private List records; private int pagesize = 10; // The number of records displayed per page: private int pagenum; // the page number to be viewed by the user, that is, the current page number: private int totalpage; // the total number of pages: private int startIndex; // The index private int totalrecords of records starting from each Page; // The total number of records // The displayed Page number private int startPage; private int endPage; public Page (int pagenum, int totalrecords) {this. pagenum = pagenum; t His. totalrecords = totalrecords; // calculate the index of the Start record per page startIndex = (pagenum-1) * pagesize; // calculate the total number of pages 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 ;}}

Take a certain number of records from the database. mysql is used as an example. The SQL statement is select * from table name limit ?,? The two question marks are the start position and end position respectively.

 

Code for displaying the page number, previous page, and jump page on the jsp page:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! -- Part of the page --> $ {page. pagenum} page/total $ {page. totalpage page <a href = "$ {pageContext. request. contextPath}/servlet/ShowAllMessageServlet "> homepage </a> <a href =" $ {pageContext. request. contextPath}/servlet/ShowAllMessageServlet? Pagenum =$ {page. pagenum-1 = 0? 1: page. pagenum-1} "> previous page </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} "> next page </a> <a href =" $ {pageContext. request. contextPath}/servlet/ShowAllMessageServlet? Pagenum =$ {page. totalpage} "> last page </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 () "> 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>



Blog blog: A small basket that is not flushed

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.