爲了複用,記載一些之前寫過的工具類、方法javascript
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; } }
<%-- 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>