开发者

How do you get the first element in a collection in JSTL/EL? [duplicate]

开发者 https://www.devze.com 2023-03-21 06:26 出处:网络
This question already has answers here: 开发者_如何学Go How to select the first element of a set with JSTL?
This question already has answers here: 开发者_如何学Go How to select the first element of a set with JSTL? (10 answers) Closed 7 years ago.

My container contains multiple widgets.

I'm looping over them and the widgetcode variable is set with the value of the last widget in the loop.

But what I really want is the first widget in the collection.

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>


<c:set var="widgetcode" value="${container.widgets[0]}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消