首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

SSH 框架分頁查詢:JSP 中使用 EL 表達式遍歷 PageBean 數據

     編輯:程式設計綜合問答

後端數據模型

`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)。

JSP 頁面核心代碼

使用 **JSTL ``** 遍歷 `pageBean.list`,配合 EL 表達式 `${}` 輸出屬性。

<%@ 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>

關鍵點解析

  • `items="${pageBean.list}"`:直接遍歷 List 集合,`var="stu"` 當前項變量名。
  • `varStatus="status"`:提供循環狀態對象,`status.index` (0基), `status.count` (1基), `status.first`, `status.last` 等,用於隔行換色、序號顯示。
  • 分頁導航邏輯完全基于 `PageBean` 的屬性 (`currentPage`, `totalPage`, `hasPreviousPage`, `hasNextPage`),**無需在 JSP 寫任何 Java 代碼 (Scriptlet)**。
  • URL 構建:``,點擊觸發 Action 的 `page` 參數。

Struts2 專用簡寫 (可選)

若 Action 實現 `ModelDriven` 或 `pageBean` 在 ValueStack 栈頂,可直接用 ``,但 `` 性能更好、語法更標準,推薦優先使用 JSTL。

依賴包

<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>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved