gpt4 book ai didi

jsp - 如何仅使用 JSTL c :ForEach without javascript or jquery? 进行分页

转载 作者:行者123 更新时间:2023-12-02 21:15:07 24 4
gpt4 key购买 nike

我听说可以使用 JSTL 进行分页。我已经看过并尝试了很多代码示例,但没有给出任何好的结果。我试过这个代码here返回一个空白页。到目前为止,下面的代码是我所拥有的并且它正在工作,但是如何显示下一个和上一个?

<c:forEach var="product" items="${categoryProducts}" begin="1" end="8" varStatus="iter">
<div class="product_container">

<div class="product_img">
<a href="viewProduct?${product.id}"> <img class="product_picture" alt="" src="${initParam.productGalleryImagePath}${product.id} (1).jpg" /></a>
</div>


<div class="product_name">
<a href="viewProduct?${product.id}">${product.name}</a>
</div>


<div class="product_price">

<a href="viewProduct?${product.id}">$${product.price}</a>

</div>

</div>
</c:forEach>

<div class="pagination">
<c:if test="${product.start > 1}">
NEXT
</c:if>
<c:if test="${product.end} < ${fn:length(product)}">
PREVIOUS
</c:if>
</div>

rickz代码:

   <c:set var="product" scope="session" value="${categoryProducts}"/>
<c:set var="totalCount" scope="session" value="${categoryProducts}"/>
<c:set var="perPage" scope="session" value="10"/>
<c:set var="pageStart" value="${param.start}"/>
<c:if test="${empty pageStart or pageStart < 0}">
<c:set var="pageStart" value="0"/>
</c:if>
<c:if test="${totalCount < pageStart}">
<c:set var="pageStart" value="${pageStart - 10}"/>
</c:if>
<a href="category.jsp?start=${pageStart - 10}">Previous</a>${pageStart + 1} - ${pageStart + 10}
<a href="category.jsp?start=${pageStart + 10}">Next</a>

最佳答案

这是一个演示。

<%@ page import="java.util.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
List<String> letters = Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
%>
<c:set var="letters" scope="session" value="<%=letters%>"/>
<c:set var="totalCount" scope="session" value="<%=letters.size()%>"/>
<c:set var="perPage" scope="session" value="10"/>
<c:set var="pageStart" value="${param.start}"/>
<c:if test="${empty pageStart or pageStart < 0}">
<c:set var="pageStart" value="0"/>
</c:if>
<c:if test="${totalCount < pageStart}">
<c:set var="pageStart" value="${pageStart - 10}"/>
</c:if>
<a href="?start=${pageStart - 10}"><<</a>${pageStart + 1} - ${pageStart + 10}
<a href="?start=${pageStart + 10}">>></a>
<h1>Letters</h1>
<c:forEach var="letter" items="${letters}" varStatus="letterCounter"
begin="${pageStart}" end="${pageStart + perPage - 1}">
${letter}
</c:forEach>

编辑:

categoryProducts 是什么类型的对象?
你需要找到它的大小。您可以尝试添加

<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>

并尝试使用

<c:set var="totalCount" scope="session" value="${fn:length(categoryProducts)}"/>

编辑2:

<%@ page import="java.util.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
List<String> letters = Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
%>
<c:set var="letters" scope="session" value="<%=letters%>"/>
<c:set var="totalCount" scope="session" value="<%=letters.size()%>"/>
<c:set var="perPage" scope="session" value="${5}"/>
<c:set var="pageStart" value="${param.start}"/>
<c:if test="${empty pageStart or pageStart < 0}">
<c:set var="pageStart" value="0"/>
</c:if>
<c:if test="${totalCount < pageStart}">
<c:set var="pageStart" value="${pageStart - perPage}"/>
</c:if>
<a href="?start=${pageStart - perPage}"><<</a>${pageStart + 1} - ${pageStart + perPage}
<a href="?start=${pageStart + perPage}">>></a>
<h1>Letters</h1>
<c:forEach var="letter" items="${letters}"
begin="${pageStart}" end="${pageStart + perPage - 1}">
${letter}
</c:forEach>

关于jsp - 如何仅使用 JSTL c :ForEach without javascript or jquery? 进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31223395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com