servlet如何實現分頁技術
阿新 • • 發佈:2018-11-10
Java中常見的分頁類別有物理分頁和邏輯分頁。對於一般的model專案可以使用邏輯分頁,對於資料比較多的,建議使用物理分頁。
- 物理分頁
只從資料庫中查詢出當前頁面的資料,不佔用很多記憶體,但效率比較低。 - 邏輯分頁
從資料庫中找到所有的資料, 儲存到記憶體當中。展示的頁面資料直接從記憶體中間讀取,效率高,但是佔用很大記憶體。
實現程式碼
物理分頁的實現
1.使用jdbc完成,使用滾動結果集,可以跨資料庫,但是效能低。
2.使用資料庫本身的分頁操作。也就是限定返回條數。
MySQL:limit
sqlservlet:top
oracle:rownum
使用以上資料庫程式碼編寫,例如limit
select * from 表 limit m,n;
m:從第幾行開始。
n:查詢幾條。
例如,每頁顯示6條,查詢第二頁的資料
select * from 表 limit (頁碼-1)*6,6;
-
分頁分析
1,頁碼自定義,預設第一頁
2,每頁條數,自定義
3,總條數,count(*)查詢
4,總頁數, 總頁數=總條數/每頁條數的向上取整
(總條數%每頁條數==0?總條數/每頁條數:總條數/每頁條數+1)
5,當前頁面資料,
程式碼分析
jsp:
<a href="${pageContext.request.contextPath}/findAllByPage">檢視所有客戶資訊(分頁展示)</a><br
servlet完成分頁
可以先建立一個bean來儲存分頁所要顯示的資料,包括頁面資料,頁碼,總頁數等。可以建立一個bean來封裝這些資料。
public class PageBean {
private int pageNum; // 頁碼
private int currentPage; // 每頁條數
private int totalPage; // 總頁數
private int totalCount; // 總條數
private List<Customer> cs; // 每頁資料
// 分頁操作
// pageNum 頁碼
// currentPage 每頁條數
public PageBean findByPage(int pageNum, int currentPage)
throws SQLException {
PageBean pb = new PageBean();
List<Customer> cs = dao.findByPage(pageNum, currentPage);
// 查詢總條數:
int totalCount = dao.findAllCount();
// 得到總頁數
int totalPage = (int) Math.ceil(totalCount * 1.0 / currentPage);
pb.setTotalCount(totalCount); // 封裝總條數
pb.setTotalPage(totalPage);// 封裝總頁數
pb.setCs(cs);// 封裝當前頁資料.
pb.setCurrentPage(currentPage); // 封裝每頁條數
pb.setPageNum(pageNum);// 封裝當前頁碼
return pb;
}
顯示頁面的jsp
<body>
<c:if test="${empty pb.cs}">
無客戶資訊
</c:if>
<c:if test="${not empty pb.cs}">
<table border="1" align="center" width="85%">
<tr>
<td>客戶編號</td>
<td>客戶姓名</td>
<td>客戶性別</td>
<td>客戶生日</td>
<td>客戶電話</td>
<td>客戶郵箱</td>
<td>客戶愛好</td>
<td>客戶型別</td>
<td>客戶備註</td>
</tr>
<c:forEach items="${pb.cs}" var="c">
<tr>
<td>${c.id }</td>
<td>${c.name}</td>
<td>${c.gender }</td>
<td>${c.birthday }</td>
<td>${c.cellphone }</td>
<td>${c.email }</td>
<td>${c.preference }</td>
<td>${c.type }</td>
<td>${c.description }</td>
</tr>
</c:forEach>
<tr>
<td colspan="9" align="center"><a
href="/findAllByPage?pageNum=1¤tPage=${pb.currentPage}">首頁</a>
<c:if test="${pb.pageNum==1}">
上一頁
</c:if> <c:if test="${pb.pageNum!=1}">
<a
href="/findAllByPage?pageNum=${pb.pageNum-1}¤tPage=${pb.currentPage}">上一頁</a>
</c:if> <c:if test="${pb.pageNum==pb.totalPage}">
下一頁
</c:if> <c:if test="${pb.pageNum!=pb.totalPage}">
<a
href="/findAllByPage?pageNum=${pb.pageNum+1 }¤tPage=${pb.currentPage}">下一頁</a>
</c:if> <a
href="/findAllByPage?pageNum=${pb.totalPage }¤tPage=${pb.currentPage}">尾頁</a>
<select name="currentPage"
onchange="changeCurrentPage(this.value);">
<option>--請選擇每頁條數--</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>
</td>
</tr>
<tr>
<td colspan="9" align="center"><c:forEach begin="1"
end="${pb.totalPage}" var="n" step="1">
<c:if test="${n==pb.pageNum}">
<a
href="/findAllByPage?pageNum=${n}¤tPage=${pb.currentPage}"><font
color='red'>第${n}頁</font> </a>
</c:if>
<c:if test="${n!=pb.pageNum}">
<a
href="/findAllByPage?pageNum=${n}¤tPage=${pb.currentPage}">第${n}頁</a>
</c:if>
</c:forEach>
</td>
</tr>
<tr>
<td colspan="9" align="center"><my:page pb="${pb}" />
</td>
</tr>
</table>
</c:if>
</body>