4

I have some problems with creating a jsp file. I would like to use JSTL in order to display a collection somehow. First, I have created a java code inside my jsp:

<% for(int i = 0; i < ((List<BookPosition>)request.getAttribute("books")).size(); i+=1) { %>  
        <label>Test</label>
<% } %>

Then, I've create a JSTL snippet.

<c:forEach var="book" items="$(books)">
        <label>Test</label>
</c:forEach>

Unfortunately, the first one outputs correct number of labels (24) and the JSTL version only one label (despite the collection contains exaclty 24 items).

Why?

pwas
  • 3,225
  • 18
  • 40

1 Answers1

2

EL expressions are written like this : ${ myExpression }

<c:forEach var="book" items="${ books }">
        <label>Test</label>
</c:forEach>

Now, if you also want to keep track of the number of printed values, you could use a counter using the varStatus attribute in your JSTL Core Tag:

<c:forEach var="book" items="${ books }" varStatus="counter">
        <label><c:out value="${ counter.count }"/>. Test</label>
</c:forEach>
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89