Java 如何使用 JSP c:forEach 和 c:if 过滤最后一个条目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16262842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to filter last entries with JSP c:forEach and c:if?
提问by jsfrocha
I'm trying to develop a Spring MVC application with JSP pages and I've run into a problem. It's more of a creativity problem than a code problem, but here goes:
我正在尝试使用 JSP 页面开发 Spring MVC 应用程序,但遇到了问题。这更像是一个创造力问题而不是代码问题,但这里是:
So the application basically recieves a recipe (fields Name, Problem Description, Problem Solution, etc) and slaps an ID on it as it is created.
因此,应用程序基本上会收到一个配方(字段名称、问题描述、问题解决方案等)并在创建时在其上打上一个 ID。
What I want is to show on the front page the last3 recipes created. I've come up with a code that apparently shows the first3 recipes created:
我想要的是在首页上显示最近创建的 3 个食谱。我想出了一个代码,显然显示了创建的前3 个食谱:
<c:forEach var="recipe" items='${recipes}'>
<c:if test="${recipe.id < 4}
<div class="span4">
<h3<c:out value="${recipe.inputDescProb}"></c:out></h3>
<p><c:out value="${recipe.inputDescSol}"></c:out></p>
<p><a class="btn" href="/recipes/${recipe.id}">Details »</a></p>
</div>
</c:if>
</c:forEach>
Any ideas on how to show the last3 recipes created instead?
关于如何显示创建的最后3 个食谱的任何想法?
采纳答案by Ravi Thapliyal
Use the fn:length()
EL function to calculate the total number of recipes. Before we use any EL function
we need to import the necessary tag library
as well.
使用fn:length()
EL 函数计算配方总数。在我们使用任何之前,我们还EL function
需要导入必要的tag library
。
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Then we use <c:set>
to set the total as a page-scoped attribute.
然后我们使用<c:set>
将总数设置为页面范围的属性。
<c:set var="totalRecipes" value="${fn:length(recipes)}" />
<c:forEach>
allows you to get a loop counter using its varStatus
attribute. The counter's scope is local to the loop and it increments automatically for you. This loop counter
starts counting from 1.
<c:forEach>
允许您使用其varStatus
属性获取循环计数器。计数器的范围是循环的本地范围,它会自动为您增加。这loop counter
从 1 开始计数。
<c:forEach var="recipe" items='${recipes}' varStatus="recipeCounter">
<c:if test="${recipeCounter.count > (totalRecipes - 3)}">
<div class="span4">
<h3<c:out value="${recipe.inputDescProb}"></c:out></h3>
<p><c:out value="${recipe.inputDescSol}"></c:out></p>
<p><a class="btn" href="/recipes/${recipe.id}">Details »</a></p>
</div>
</c:if>
</c:forEach>
EDIT: Use the count
property of LoopTagStatus
class to access the current value of the iteration counter in your EL as ${varStatusVar.count}
.
编辑:使用class的count
属性LoopTagStatus
访问 EL 中迭代计数器的当前值 as ${varStatusVar.count}
。
回答by Reimeus
You could compare the current count against the total collection size using ${fn:length(recipes)}
:
您可以使用以下方法将当前计数与总集合大小进行比较${fn:length(recipes)}
:
<c:set var="total" value="${fn:length(recipes)}"/>
<c:forEach var="recipe" items='${recipes}' varStatus="status">
<c:if test="${status.count > total - 3}">
<div class="span4">
<h3<c:out value="${recipe.inputDescProb}"></c:out></h3>
<p><c:out value="${recipe.inputDescSol}"></c:out></p>
<p><a class="btn" href="/recipes/${recipe.id}">Details »</a></p>
</div>
</c:if>
</c:forEach>
Edit:
编辑:
You will need to import fn
first to make the JSTL fn
available for use:
您需要先导入fn
以使 JSTLfn
可用:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
回答by dmlek
No need to check length, just use the .last
property of the varStatus
variable.
无需检查长度,只需使用变量的.last
属性即可varStatus
。
<c:forEach var="recipe" items="${recipes}" varStatus="status">
<c:if test="${not status.last}">
Last Item
</c:if>
<c:forEach>
Side note, you can also get the .first
and .count
旁注,您还可以获得.first
和.count