How to select the first element of a set with JSTL?

前端 未结 9 1272
逝去的感伤
逝去的感伤 2020-12-02 17:53

I managed to do it with the next code but there must be an easier way.

<%@ taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\" %>
<%@ tag         


        
相关标签:
9条回答
  • 2020-12-02 18:49

    You can access individual elements with the array [] operator:

    <c:out value="${attachments[0].id}" />
    

    This will work for arrays and lists. It won't work for maps and sets. In that case you must put the key of the element inside the brackets.

    0 讨论(0)
  • 2020-12-02 18:56

    Using ${mySet.toArray[0]} does not work.

    I do not think it is possible without having forEach loop at least one iteration.

    0 讨论(0)
  • 2020-12-02 18:59

    Since i have have just one element in my Set the order is not important So I can access to the first element like this :

    ${ attachments.iterator().next().id }
    
    0 讨论(0)
提交回复
热议问题