數據封裝:javascript
public class Page {
private int pageNum;//當前頁碼 傳入
private int pageSize=5;//每頁顯示的記錄條數
private int totalPage;//總頁數 計算
private int startIndex; //當前頁碼開始的索引 計算
private List records;//每頁顯示的記錄 dao
private int totalRecords;//總記錄條數 dao
private String servletUrl;
public Page(int pageNum,int totalRecords){
this.pageNum=pageNum;
this.totalRecords=totalRecords;
totalPage=totalRecords%pageSize==0?totalRecords/pageSize:totalRecords/pageSize+1;
startIndex=(pageNum-1)*pageSize;
}
public int getPageNum() {
return pageNum;
}
public String getServletUrl() {
return servletUrl;
}
public void setServletUrl(String servletUrl) {
this.servletUrl = servletUrl;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
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 List getRecords() {
return records;
}
public void setRecords(List records) {
this.records = records;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
}java
公用頁面:jsp
page.jsp:this
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
第${page.pageNum}頁 共${page.totalPage}頁
<a href="${pageContext.request.contextPath }/servlet/Controller?op=showAllCustomers&num=1">首頁</a>
<a href="${pageContext.request.contextPath }/servlet/Controller?op=showAllCustomers&num=${page.pageNum-1<1?1:page.pageNum-1}">上一頁</a>
<a href="${pageContext.request.contextPath }/servlet/Controller?op=showAllCustomers&num=${page.pageNum+1>page.totalPage?page.totalPage:page.pageNum+1}">下一頁</a>
<a href="${pageContext.request.contextPath }/servlet/Controller?op=showAllCustomers&num=${page.totalPage}">尾頁</a>
<input type="text" name="num" id="num" value="${page.pageNum}" />
<a href="javascript:jump()">跳轉</a>
<!-- 下拉方式跳轉頁碼
<select name="num" id="id" onchange="jump(this)">
<c:forEach begin="1" end="${page.totalPage}" var="n">
<option value="${n}">${n}</option>
</c:forEach>
</select>
<script type="text/javascript">
function jump(selectObj){
window.location.href="${pageContext.request.contextPath }/servlet/Controller?op=showAllCustomers&num="+selectObj.value;
}
</script>
-->
<script type="text/javascript">
function jump(){
var num=document.getElementById("num").value;
var regObj=new RegExp("[1-9][0-9]*");
if(!regObj.test(num)){
alert("請輸入天然整數");
return;
}
if(num>${page.totalPage}){
alert("超出範圍");
return;
}
window.location.href="${pageContext.request.contextPath }/servlet/Controller?op=showAllCustomers&num="+num;
}
</script>
索引