Black Horse day13 pagination idea & Realization

Source: Internet
Author: User

The overall idea of paging:

What to include in pagination:

1. Current page, number of records displayed per page, total number of records, total page number, collection list is JavaBean, home, last, previous page, next page

Parameters passed: The current page, the number of records displayed per page. These two are supposed to be there.

Query in the database: the total number of records , the collection list is stored JavaBean. These two are queried from the database.

Computed by known attributes: Home, last, Prev, Next, total page numbers these are calculated by calculation.

So JavaBean page can be written as:

Package Com.itheima.domain;import Java.util.list;public class Page {private int thispage;//current page private int rowperpage;// Number of records displayed per page private int countrow;//total number of records private int countpage;//total pages private int firstpage;//home private int lastpage;// Last private int prepage;//prev Private int nextpage;//next private list<customer> list;public int getthispage () {return Thispage;} public void setthispage (int thispage) {this.thispage = Thispage;} public int getrowperpage () {return rowperpage;} public void setrowperpage (int rowperpage) {this.rowperpage = Rowperpage;} public int Getcountrow () {return countrow;} public void Setcountrow (int countrow) {this.countrow = Countrow;} public int getcountpage () {return countpage;} public void setcountpage (int countpage) {this.countpage = Countpage;} public int getfirstpage () {return firstpage;} public void setfirstpage (int firstpage) {this.firstpage = FirstPage;} public int getlastpage () {return lastpage;} public void setlastpage (int lastpage) {this.lastpage = LastPage;} Public inT Getprepage () {return prepage;} public void setprepage (int prepage) {this.prepage = Prepage;} public int getnextpage () {return nextpage;} public void setnextpage (int nextpage) {this.nextpage = nextpage;} Public list<customer> getList () {return List;} public void setlist (list<customer> list) {this.list = list;}}
2. First paging needs to be done in the Cn.itheima.web package to write a paging servlet,pagecustomerservlet

The main steps of this class are:

(1). The parameter that gets passed is the home page by default

(2). The number of records displayed per page, where the default is 5

(3). A function that invokes paging in service returns the page

(4). Place the page in the request domain

(5). Redirect to the JSP that you want to implement paging page paging

Package Com.itheima.web;import Java.io.ioexception;import Java.util.list;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.itheima.domain.customer;import Com.itheima.domain.Page;import Com.itheima.factory.basicfactory;import Com.itheima.service.customerservice;public class PageCustomerServlet Extends HttpServlet {customerservice service =basicfactory.getfactory (). getinstance (customerservice.class);p ublic void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//1. Gets the record that is displayed per page of the current page int thispage=integer.parseint (request.getparameter ("Thispage")); int ROWPERPAGE=5;//2. Method of invoking the service page page=service.findcustomerbypage (thispage,rowperpage); Request.setattribute ("page", page);//3. The request is forwarded to PageList.jsprequest.getRequestDispatcher ("/pagelist.jsp"). Forward (request, response); public void DoPost (HttpServletRequest request, HttpservleTresponse response) throws Servletexception, IOException {doget (request, Response);}} 
3.service implements the main functions of the class code:

Calculates the properties in the page, set to page

Public page findcustomerbypage (int thispage, int rowperpage) {page page=new page ();//Set Current page page.setthispage (thispage); /per page The number of records displayed by default is 5 page.setrowperpage (rowperpage);//Query database A total of how many lines int countrow=dao.getcount ();p Age.setcountrow (countrow )////Total number of pages int countpage= (countrow-1)/rowperpage+1;page.setcountpage (countpage);//Set Home page.setfirstpage (1);// Set last Page.setlastpage (countpage);//Previous Page page.setprepage (thispage==1?1:thispage-1);//Next page page.setnextpage (thispage= =countpage?countpage:thispage+1);//list<customer> list=dao.getcustomerbypage (thispage-1) *rowperpage, Rowperpage);p age.setlist (list); return page;}
4. Query using the LIMIT keyword in the database

Public list<customer> Getcustomerbypage (int. from, int count) {String sql= ' select * from Customer limit?,? "; Queryrunner runner=new Queryrunner (Datasourceutil.getsource ()); try {return runner.query (SQL, new beanlisthandler< Customer> (Customer.class), from,count);} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();}}
The 5.jsp page displays:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ taglib uri= "http://java.sun.com/jsp /jstl/core "prefix=" C "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Pagination Pseudo-code:


6. Pagination Display:





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

Black Horse day13 pagination idea & Realization

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.