4

我想在 JSP 中做以下事情for loop- 我只想循环 HashSet 和 HashMap 并打印结果

private static HashMap<Long, Long> histogram = new HashMap<Long, Long>();
private static Set<Long> keys = histogram.keySet();

for (Long key : keys) {
    Long value = histogram.get(key);
    System.out.println("MEASUREMENT, HG data, " + key + ":" + value);
}

我正在使用 Spring MVC,所以我在我的model

model.addAttribute("hashSet", (keys));
model.addAttribute("histogram", (histogram));

在我的 JSP 页面中,我正在做类似的事情来模拟上述内容,JAVA code但它给了我一个例外,即我的 JSP 页面有问题。

<fieldset>
    <legend>Performance Testing:</legend>
        <pre>

            <c:forEach items="${hashSet}" var="entry">
            Key = ${entry.key}, value = ${histogram}.get(${entry.key})<br>
            </c:forEach>


        </pre>
        <br />
</fieldset>

我得到的例外 -

Caused by: javax.el.PropertyNotFoundException: Property 'key' not found on type java.lang.Long
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:195)
    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:172)
    at javax.el.BeanELResolver.property(BeanELResolver.java:281)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)

谁能帮我解决这个问题?

4

1 回答 1

4

您不需要使用来keySet访问values. 当您使用>HashMap进行迭代时,您将返回,您可以使用:-并直接:-HashMap<c:forEach..EntrySetEntrySet#getKey()EntrySet#getValue()

<c:forEach items="${histogram}" var="entry">
     Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
于 2013-01-30T20:54:45.830 回答