The base class of pagination
import java.util.List;/** * The standard class for pagination display is to first give - the total number of data strips in the current page - the number of pieces displayed on each page, * Then, initialize the class to get the total number of pages, the start number and the end number, * Then the database pagination uses the start number and the end number, and then assign it to the list attribute of the class after obtaining the data set, * * Then send the class to the jsp page for access* @author admin * * @param <T> */public class PageBean<T> {private int pageIndex;//current pages private int pageSize;// total number of pages private int count;//number of data strips private int pageCount;//Number of data strips per page private int start;//First data position private int end;//End private List<T> list=null;public void init(){/*Root count and pageCount calculate page count pageSize */int pageSize_x=(int)count/pageCount;if(count>=pageCount){this.pageSize=count%pageCount==0?pageSize_x:pageSize_x+1;} else{this.pageSize=1;}//Judge the number of pages and the current number of pages if(pageIndex>pageSize){pageIndex=pageSize;}if(pageIndex<1){pageIndex=1;}//Calculate the start and end entries based on the current page this.start=(pageIndex-1)*pageCount+1;this.end=pageIndex*pageCount;}public PageBean(int pageIndex, int count, int pageCount) {super();this.pageIndex = pageIndex;this.count = count;this.pageCount = pageCount;}public PageBean(int pageIndex, int count, int pageCount, List<T> list) {super();this.pageIndex = pageIndex;this.count = count;this.pageCount = pageCount;this.list = list;}public PageBean() {super();// TODO Auto-generated constructor stub}@Override public String toString() {return "PageBean [count=" + count + ", end=" + end + ", list=" + list + ", pageCount=" + pageCount + ", pageIndex=" + pageIndex + ", pageSize=" + pageSize + ", start=" + start + "]";}public int getPageIndex() {return pageIndex;}public void setPageIndex(int pageIndex) {this.pageIndex = pageIndex;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public int getPageCount() {return pageCount;}public void setPageCount(int pageCount) {this.pageCount = pageCount;}public int getStart() {return start;}public void setStart(int start) {this.start = start;}public int getEnd() {return end;}public void setEnd(int end) {this.end = end;}public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}}servlet call
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.dao.MessageDao;import com.dao.impl.MessageDaoImpl;import com.vo.Message;import com.vo.PageBean;public class ShowMessageServlet extends HttpServlet{/** * */private static final long serialVersionUID = 6646899131087204214L;@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=utf-8");int pageIndex=0;MessageDao md=new MessageDaoImpl();String pageIndexStr=req.getParameter("pageIndex");if(pageIndexStr!=null){try{pageIndex=Integer.parseint(pageIndexStr);}catch (Exception e) {}}PageBean<Message> pb=new PageBean<Message>(pageIndex,md.getMessageCount(),10);pb.init();pb.setList(md.getMessageListOfPage(pb.getStart(), pb.getEnd()));req.setAttribute("pagebean", pb);req.getRequestDispatcher("index.jsp").forward(req, resp);}}The display call of the jsp page
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <c:if test="${empty pagebean}"> <jsp:forward page="showmessage"></jsp:forward> </c:if> <body> <c:forEach var="message" items="${pagebean.list}"> ${message.title } ${message.editdate }<br/> </c:forEach> <a href="showmessage?pageIndex=${pagebean.pageIndex+1}" rel="external nofollow" >Next</a>[${pagebean.pageIndex }<span>/</span>${pagebean.pageSize}] </body></html>Summarize
The above is all the detailed explanation of a general Java pagination base class code in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other Java-related topics on this website. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!