java 你如何在 JSTL/EL 中获取集合中的第一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6755358/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:08:51 来源:igfitidea点击:
How do you get the first element in a collection in JSTL/EL?
提问by Mike Brandywine
My container
contains multiple widgets
.
我的container
包含多个widgets
.
I'm looping over them and the widgetcode
variable is set with the value of the last widget
in the loop.
我遍历他们的widgetcode
变量设置与最后的值widget
在循环。
But what I really want is the first widget
in the collection.
但我真正想要的是widget
收藏中的第一个。
How do I get this without looping?
我如何在不循环的情况下获得这个?
<c:choose>
<c:when test="${fn:length(container.widgets) > 0}">
<c:forEach items="${container.widgets}" var="widgetId" varStatus="status">
<c:set var="widgetcode" value="/widget/<c:out value="${widgetId}"/>"/>
</c:forEach>
</c:when>
<c:otherwise>
<c:set var="widgetcode" value="/widget/000"/>
</c:otherwise>
</c:choose>
回答by highlycaffeinated
<c:set var="widgetcode" value="${container.widgets[0]}"/>