February 2005
Intermediate to advanced
528 pages
12h 53m
English
Instead of creating a long page to display a large number of tabular items, you want to display a limited fraction of the data on a page, allowing the user to navigate between the pages.
The simplest way to perform paging without resorting to a third-party
tag library is to leverage the arithmetic capabilities of JSTL EL and
the features of JSTL's c:forEach
tag. The JSP page
(paged_data.jsp) of Example 4-10
presents a complete page that supports paging through a Collection.
Example 4-10. Using JSTL for data paging
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix= "bean" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head> <title>Struts Cookbook - Chapter 4 : Paging</title> </head> <body> <jsp:useBean id="pagedData" class="com.oreilly.strutsckbk.ch04. PagedData"/> <bean:size id="listSize" name="pagedData" property="data"/> <c:set var="pageSize" value="10"/> <c:set var="pageBegin" value="${param.pageBegin}"/> <c:set var="pageEnd" value="${pageBegin + pageSize - 1}"/> <c:if test="${(pageBegin - pageSize) ge 0}"> <a href='<c:url value="paged_data.jsp"> <c:param name="pageBegin" value="${pageBegin - pageSize}"/> </c:url>'> Prev </a> </c:if> <c:if test="${(listSize gt pageSize) and (pageEnd lt listSize)}"> <a href='<c:url value="paged_data.jsp"> <c:param name="pageBegin" value="${pageBegin + pageSize}"/> </c:url>'> Next </a> </c:if> <table ...