开发者

Retrieving the first element of a TreeMap using JSTL/EL/JAVA

I am trying to access the first element of a TreeMap, I have the following HTML in a JSP file:

<c:forEach items="${subscriber.depent}" var="entry" begin="0" end="0" step="1">
    <c:set var="dep" value="${entry.value}" />
</c:forEach>

This code gets me the first element of the TreeMap but this just seems like a 'hack' to me.

I have also tried:

<c:set var="dep" value="${subscriber.depent[0]}" />

But that gives me a exception:

java.lang.Integer i开发者_如何学Goncompatible with java.lang.Long

Any better ways of doing this?

Thanks, Randall.


In order to do this, you need to get into a situation where the "first of" makes sense in the Collection/array/getter context that you have with JSTL. Unfortunately the TreeMap.firstKey is not a getter so you cannot get to it with JSTL-syntax.

If you can subclass the TreeMap you can add a "getFirstKey()" method which just calls firstKey and then refer to it with "subscriber.depent.firstKey".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜