DJ180
DJ180

Reputation: 19854

JSTL: Does c:forEach guarantee List ordering in JSP?

My JSP contains the following snippet:

<c:forEach items="${rulesForm.rules}" var="rule" varStatus="counter">
        <tr id="rules${counter.index}" name="rules[${counter.index}]">

"rules" itself is a List<Rule>.

When I pass my ModelAndView object back from my Spring MVC Controller, I can see that my List is in the correct order.

However, when it is then rendered on screen, the ordering is somewhat random. I also have JavaScript that performs some modifications on the DOM, but I don't see this doing any reordering. Therefore, I am wondering if c:forEach is the culprit?

Upvotes: 2

Views: 6125

Answers (2)

Varun Achar
Varun Achar

Reputation: 15109

Iteration will depend on the type of List passed. So you'll have to check the docs of that type of List.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340763

I bet <c:forEach/> uses an Iterator in most implementations, therefore it relies on underlying collection order. Thus if you pass List, the order will be preserved, which is not true for Sets.

Although the documentation does not state that:

items [...] Collection of items to iterate over.

Think about it - if the order of ordered collection was not preserved, any server-side sorting of results wouldn't have sense.

Upvotes: 3

Related Questions