在JSP中循环遍历HashSet和HashMap并打印结果 [英] Loop through HashSet and HashMap in JSP and print the result
问题描述
我想从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屋!