在JSP中循环遍历HashSet和HashMap并打印结果 [英] Loop through HashSet and HashMap in JSP and print the result

查看:95
本文介绍了在JSP中循环遍历HashSet和HashMap并打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从for loop开始在JSP中执行以下操作-我只想循环HashSet和HashMap并打印结果

I want to do the below thing in JSP starting from for loop- I just want to loop HashSet and HashMap and print the result

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

I am working with Spring MVC, so I added these two things in my model

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

在我的JSP页面中,我正在做类似上面的JAVA code的操作,但是这给了我一个例外,那就是我的JSP页面中有问题.

And in my JSP page, I was doing something like this to emulate the above JAVA code but it was giving me an exception that something is wrong in my JSP page.

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

有人可以帮我吗?

推荐答案

您无需使用keySet即可访问HashMap中的values.当使用>遍历HashMap时,您会得到EntrySet,您可以使用它:-EntrySet#getKey()EntrySet#getValue()直接:-

You don't need to make use of keySet to access the values in the HashMap. When you iterate over HashMap using <c:forEach..>, you get back the EntrySet, for which you can use: - EntrySet#getKey() and EntrySet#getValue() directly: -

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

这篇关于在JSP中循环遍历HashSet和HashMap并打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆