[英]Loop through HashSet and HashMap in JSP and print the result
我想在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)
任何人都可以幫我解決這個問題嗎?
您不需要使用keySet
來訪問HashMap
的values
。 當您使用<c:forEach..
>迭代HashMap
,您將返回EntrySet
,您可以直接使用: - EntrySet#getKey()
和EntrySet#getValue()
: -
<c:forEach items="${histogram}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.