manub
manub

Reputation: 4100

how to access inner elements of a Map that contains Lists in JSTL?

I have a List of MyBean1 in request scope (they have the name tests). MyBean1 has a parameter of type Map<Integer, List<MyBean2>>, accessible via a method call getMap(). Keys of this map are numbers from 1 to 6. MyBean2 has a method getValue() which returns a String.

I need the values of each list to be put in the same table cell.

I wrote something like this:

<c:forEach var="test" items="${tests}">
    // some stuff
    <td><c:forEach var="bean" items="S{test.map[1]}">${bean.value} </c:forEach></td>
    // repeat for keys to 2 to 6
</c:forEach>

But this doesn't seem to work. I'm not getting the value fields of the List contained in the Map at key 1.

Am I doing something wrong?

I'm using Spring MVC as MVC framework, and I'm able to get other fields from that test variable.

Thank you.

Upvotes: 1

Views: 1189

Answers (1)

JB Nizet
JB Nizet

Reputation: 691865

See EL access a map value by Integer key for an explanation of why it doesn't work.

If your map is a sorted map or a LinkedHashMap, iterating over its entries might work:

<td><c:forEach var="entry" items="${test.map}">
        <c:forEach var="bean" items="${entry.value}">${bean.value}</c:forEach>
    </c:forEach></td>

Upvotes: 1

Related Questions