package bean;
public class Page {
private int totalPage;//總頁數
private int currentPage;//當前頁數
private int totalRecord;//總記錄條數
private int currentRecord;//目前記錄數
private int pageSize=1;//每頁顯示的記錄數
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int PcurrentRecord,int PpageSize) {
if(currentRecord%pageSize==0)
currentPage=currentRecord/pageSize;
else
currentPage=currentRecord/pageSize+1;
}
public int getCurrentRecord() {
return currentRecord;
}
public void setCurrentRecord(int PcurrentRecord) {
currentRecord=PcurrentRecord;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int PpageSize) {
pageSize=PpageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int PtotalRecord,int PpageSize) {
if(PtotalRecord%PpageSize==0)
totalPage=PtotalRecord/PpageSize;
else
totalPage=PtotalRecord/PpageSize+1;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int PtotalRecord) {
totalRecord=PtotalRecord;
}
}
<%@page import="java.util.List"%>
<%@page import="bean.Guest"%>
<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html; charset=utf-8" language="java"
import="java.sql.*" errorPage=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<jsp:useBean id="db" class="bean.ORMDBUtil" scope="page"></jsp:useBean>
<jsp:useBean id="pager" class="bean.Page" scope="page"></jsp:useBean>
<%
String sql = "select * from guests";
int currentRecord = 0;
ArrayList<Guest> result = db.select(sql);
pager.setTotalRecord(result.size());
pager.setTotalPage(result.size(), pager.getPageSize());
if (request.getParameter("currentRecord") != null) {
currentRecord = Integer.parseInt(request.getParameter("currentRecord"));
pager.setCurrentRecord(currentRecord);
pager.setCurrentPage(currentRecord, pager.getPageSize());
}
//取出當前頁面要顯示的子集
List<Guest> subResult = null;
if (currentRecord == 0);
subResult = result.subList(0, pager.getPageSize());
if (pager.getCurrentRecord() + pager.getPageSize() < result.size())
subResult = result.subList(pager.getCurrentRecord(),
pager.getCurrentRecord() + pager.getPageSize());
else
subResult = result.subList(pager.getCurrentRecord(),
result.size());
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分頁顯示JavaBean使用樣本</title>
</head>
<body>
<font size="+2"><strong>本頁利用JavaBean實現分頁顯示功能。</strong><br />
下面的表格就是運行結果 </font>
<table width="387" border="1" height="47">
<tr>
<td height="41">編號</td>
<td>姓名</td>
<td>電話</td>
<td>email</td>
</tr>
<%
if (subResult.isEmpty() == false) {
for (int i = 0; i < subResult.size(); i++) {
Guest guest = subResult.get(i);
out.print("<tr>");
out.print("<td>" + guest.getId() + "</td>");
out.print("<td>" + guest.getCname() + "</td>");
out.print("<td>" + guest.getPhone() + "</td>");
out.print("<td>" + guest.getEmail() + "</td>");
out.print("</tr>");
}
}
%>
</table>
<span><font size="+2">總<%=pager.getTotalRecord()%>條記錄|總<%=pager.getTotalPage()%>頁|當前<%=pager.getCurrentPage() + 1%>頁|每頁
<%=pager.getPageSize()%>條| <%
if (pager.getCurrentRecord() - pager.getPageSize() < 0)
out.print("首頁|");
else
out.print("<a href='PageQuery.jsp?currentRecord="
+ (pager.getCurrentRecord() - pager.getPageSize())
+ "&pageSize=" + pager.getPageSize() + "'>上一頁,</a>|");
if (pager.getCurrentRecord() + pager.getPageSize() > pager
.getTotalRecord())
out.print("尾頁");
else
out.print("<a href='PageQuery.jsp?currentRecord="
+ (pager.getCurrentRecord() + pager.getPageSize())
+ "&pageSize=" + pager.getPageSize() + "'>下一頁</a>|");
%>
</font></span>
</body>
</html>