开发者

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

开发者 https://www.devze.com 2023-03-27 17:18 出处:网络
I have a List of MyBean1 in request scope (th开发者_开发技巧ey have the name tests). MyBean1 has a parameter of type Map<Integer, List<MyBean2>>, accessible via a method call getMap(). Key

I have a List of MyBean1 in request scope (th开发者_开发技巧ey 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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号