使用 JSTL/EL/JAVA 检索 TreeMap 的第一个元素

发布于 2024-09-14 23:10:35 字数 498 浏览 3 评论 0原文

我正在尝试访问 TreeMap 的第一个元素,JSP 文件中有以下 HTML:

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

此代码获取 TreeMap 的第一个元素,但这对我来说似乎是一个“黑客”。

我也尝试过:

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

但这给了我一个例外:

java.lang.Integer 与 java.lang.Long 不兼容

有更好的方法吗?

谢谢,兰德尔。

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 incompatible with java.lang.Long

Any better ways of doing this?

Thanks, Randall.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-09-21 23:10:35

为了做到这一点,您需要进入这样一种情况:“第一个”在 JSTL 的 Collection/array/getter 上下文中有意义。不幸的是,TreeMap.firstKey 不是 getter,因此您无法使用 JSTL 语法获取它。

如果您可以子类化 TreeMap,则可以添加一个“getFirstKey()”方法,该方法仅调用firstKey,然后使用“subscriber.depent.firstKey”引用它。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文