Java 在JSP中循环HashSet和HashMap并打印结果
我想在JSP中从循环的Java 在JSP中循环HashSet和HashMap并打印结果,java,jsp,spring-mvc,Java,Jsp,Spring Mvc,我想在JSP中从循环的开始做以下事情-我只想循环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);
开始做以下事情-我只想循环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);
}
在我的JSP页面中,我这样做是为了模拟上面的JAVA代码
,但它给了我一个例外,我的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>
有人能帮我解决这个问题吗?您不需要使用键集来访问HashMap
中的值。当您使用迭代HashMap
时,将返回EntrySet
,您可以直接使用它:-EntrySet#getKey()
和EntrySet#getValue()
:-
Key=${entry.Key},value=${entry.value}
没有人能在不知道异常确切说明的情况下帮助您。“有什么不对劲”的信息不足以继续下去。很抱歉Richard谢谢Rohit的帮助。请仔细看看我的问题。我想我需要从HashMap中提取数据。你的回答中有一部分对我有意义me@TechGeeky.. 问题是,在JSTL中不能调用这样的函数。因此,您不能调用map.get(key)
方法。为此,您必须创建一个自定义标记,该标记在某个TagHandler类中定义一个自定义函数,该函数将返回您的map.get(key)值。这当然是一项更乏味的任务。我明白了。。所以我想不用HashSet,我可以直接迭代HashMap,对吗?@TechGeeky。。确切地看看我发布的for循环。它迭代hashmap,并返回EntrySet。然后可以使用它的getKey()和getValue()方法。
<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)
<c:forEach items="${histogram}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>