分页复用代码【Page类、JSP显示页面】
前言
为了复用,记载一些以前写过的工具类、方法
page类
import java.util.List; /** * Created by ozc on 2017/3/1. */ public class Page { //保存着分页的数据 private List<Customer> list; //总记录数 private long totalRecord; //每页显示记录数,这里我规定每页显示3条 private int linesize = 3; //总页数 private int totalPageCount; //当前显示的页数 private int currentPageCount; //开始取的记录位置 private int startIndex; //记录JSP页面开始的页数和结束的页数 private int startPage; private int endPage; private String url; public Page() { } public Page(int currentPageCount, long totalRecord) { //将传递进来的currentPageCount初始化 this.currentPageCount = currentPageCount; //总页数 totalPageCount = (int) (totalRecord % linesize == 0 ? totalRecord / linesize : totalRecord / linesize + 1); this.totalRecord = totalRecord; //开始取数据的位置 startIndex = (currentPageCount - 1) * linesize; //如果当前页小于10,那么开始页为1,结束页为10就行了 if (this.currentPageCount <= 10) { this.startPage = 1; this.endPage = 10; } else { this.startPage = this.currentPageCount - 4; this.endPage = this.currentPageCount + 5; //加减后页数越界的情况 if (startPage < 1) { this.startPage = 1; this.endPage = 10; } if (endPage > totalPageCount) { this.startPage = this.currentPageCount - 9; this.endPage = this.totalPageCount; } } } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getStartIndex() { return startIndex; } public void setCurrentPageCount(int currentPageCount) { this.currentPageCount = currentPageCount; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } 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; } public int getTotalPageCount() { return totalPageCount; } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public List<Customer> getList() { return list; } public void setList(List<Customer> list) { this.list = list; } public long getTotalRecord() { return totalRecord; } public void setTotalRecord(long totalRecord) { this.totalRecord = totalRecord; } public int getLinesize() { return linesize; } public void setLinesize(int linesize) { this.linesize = linesize; } public long getCurrentPageCount() { return currentPageCount; } public void setCurrentPageCount(long currentPageCount) { this.currentPageCount = (int) currentPageCount; } }
显示页面JSP
<%-- Created by IntelliJ IDEA. User: ozc Date: 2017/3/1 Time: 21:17 To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--显示当前页数--%>
当前页数是:[${page.currentPageCount}]
<%--如果当前的页码大于1,才显示上一步--%>
<c:if test="${page.currentPageCount>1}">
<%--把传递过去的页码-1就行了--%>
<a href="${page.url}?currentPageCount=${page.currentPageCount-1}">
上一步
</a>
</c:if>
<%--提供页数的界面--%>
<c:forEach var="pageNum" begin="${page.startPage}" end="${page.endPage}">
<a href="${page.url}?currentPageCount=${pageNum}">
[${pageNum}]
</a>
</c:forEach>
<%--如果当前的页码小于总页数,才显示下一步--%>
<c:if test="${page.currentPageCount<page.totalPageCount}">
<%--把传递过去的页码-1就行了--%>
<a href="${page.url}?currentPageCount=${page.currentPageCount+1}">
下一步
</a>
</c:if>
<input type="text" id="currentPageCount">
<input type="button" value="跳转" onclick="goPage()">
总页数是:${page.totalPageCount}
总记录数是:${page.totalRecord}
<script type="text/javascript"> /*既然写上了JavaScript代码了,就顺便验证输入框输入的数据是否合法吧*/ function goPage() { /*获取输入框控件*/ var input = document.getElementById("currentPageCount"); /*获取输入框的数据*/ var value = input.value; if(value==null || value==""){ alert("请输入页码"); return false; } if(!value.match("\\d+")){ alert("请输入数字"); return false; } if(value<1 || value>${page.totalPageCount}){ alert("请输入合法数据"); return false ; } window.location.href="${page.url}?currentPageCount="+value; } </script>
还没有评论,来说两句吧...