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

I managed to do this with the following code, but there should be an easier way.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <c:if test="${fn:length(attachments) > 0}"> <c:forEach var="attachment" items="${attachments}" varStatus="loopCount"> <c:if test="${loopCount.count eq 1}"> attachment.id </c:if> </c:forEach> </c:if> 
+46
java-ee jstl
Jun 16 '09 at 11:25
source share
9 answers

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

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

This will work for arrays and lists. It will not work for cards and sets. In this case, you must put the element key in brackets.

+78
Jun 17 '09 at 7:02
source share

Sets the order, but if you still want to get the first element, you can use the following:

 <c:forEach var="attachment" items="${attachments}" end="0"> <c:out value="${attachment.id} /> </c:forEach> 
+35
May 26 '11 at 21:53
source share

Work only for arrays and lists, not for dialing.

+8
Aug 18 '09 at 16:28
source share

Since I have only one element in my Set, the order is not important. Therefore, I can access the first element like this:

 ${ attachments.iterator().next().id } 
+4
Jun 23 '15 at 16:01
source share

Look here for a description of the statusVar variable. You can do something like below, where the "status" variable contains the current iteration status. This is very useful if you need special annotations for the first and last iterators. In the following case, I want to skip the comma after the last tag. Of course, you can replace status.last with status.first to do something special in the first iteration:

 <td> <c:forEach var="tag" items="${idea.tags}" varStatus="status"> <span>${tag.name not status.last ? ', ' : ''}</span> </c:forEach> </td> 

The options are: current, index, count, first, last, begin, step, and end

+3
Dec 11 '11 at 15:45
source share

If you only need the first element in the set (and you are sure that there is at least one element), you can do the following:

 <c:choose> <c:when test="${dealership.administeredBy.size() == 1}"> Hello ${dealership.administeredBy.iterator().next().firstName},<br/> </c:when> <c:when test="${dealership.administeredBy.size() > 1}"> Hello Administrators,<br/> </c:when> <c:otherwise> </c:otherwise> </c:choose> 
+2
Aug 07 '13 at 20:29
source share

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

I do not think that this is possible without at least one iteration of the forEach loop.

0
07 Oct '09 at 11:33
source share

Using the beginning and the end seemed to me useful to select a range of elements. This gives me three separate lists. The first list with items 1โ€“9, the second list with items 10โ€“18, and the last list with items 11โ€“25.

  <ul> <c:forEach items="${actionBean.top25Teams}" begin="0" end="8" var="team" varStatus="counter"> <li>${team.name}</li> </c:forEach> </ul> <ul> <c:forEach items="${actionBean.top25Teams}" begin="9" end="17" var="team" varStatus="counter"> <li>${team.name}</li> </c:forEach> </ul> <ul> <c:forEach items="${actionBean.top25Teams}" begin="18" end="25" var="team" varStatus="counter"> <li>${team.name}</li> </c:forEach> </ul> 
0
Feb 20 '14 at 16:11
source share

You can use the EL 3.0 Stream API.

 <div>${attachments.stream().findFirst().get()}</div> 

Be careful! The EL 3.0 Stream API has been modified to the Java 8 Stream API , and it is different from this. They cannot sunbathe both apis because they violate backward compatibility.

0
Sep 20 '17 at 11:37
source share



All Articles