`PageBean` 封裝分頁信息:
public class PageBean {
private List list; // 當前頁數據列表
private int allRow; // 總記錄數
private int totalPage; // 總頁數
private int currentPage; // 當前頁碼
private int pageSize; // 每頁條數
// ... isFirstPage, isLastPage, hasPreviousPage, hasNextPage 等屬性及 getter/setter
}
Action 返回 `pageBean` 放入 `request` 或 `valueStack` (Struts2)。
使用 **JSTL `
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %> <!-- Struts2 標簽庫 (可選) -->
<table class="data-grid">
<thead>
<tr><th>ID</th><th>姓名</th><th>年齡</th><th>操作</th></tr>
</thead>
<tbody>
<c:forEach items="${pageBean.list}" var="stu" varStatus="status">
<tr class="${status.index % 2 == 0 ? 'even' : 'odd'}">
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>
<a href="edit?id=${stu.id}">編輯</a> |
<a href="delete?id=${stu.id}" onclick="return confirm('確定刪除?')">刪除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 分頁導航欄 -->
<div class="pagination">
<c:if test="${pageBean.hasPreviousPage}">
<a href="list?page=${pageBean.currentPage - 1}">上一頁</a>
</c:if>
<c:if test="${!pageBean.hasPreviousPage}">
<span class="disabled">上一頁</span>
</c:if>
<c:forEach begin="1" end="${pageBean.totalPage}" var="p">
<c:choose>
<c:when test="${p == pageBean.currentPage}">
<span class="current">${p}</span>
</c:when>
<c:otherwise>
<a href="list?page=${p}">${p}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pageBean.hasNextPage}">
<a href="list?page=${pageBean.currentPage + 1}">下一頁</a>
</c:if>
<c:if test="${!pageBean.hasNextPage}">
<span class="disabled">下一頁</span>
</c:if>
<span class="info">
第 ${pageBean.currentPage} / ${pageBean.totalPage} 頁 共 ${pageBean.allRow} 條
</span>
</div>
若 Action 實現 `ModelDriven
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>