Scroll through HashSet and HashMap in JSP and print the result

I want to do the following in JSP starting with for loop - I just need to loop the 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); } 

I work with Spring MVC, so I added these two things to my model

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

And on my JSP page, I did something like this to emulate the above JAVA code , but that gave me the exception that something was wrong on 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> 

I got an exception -

 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) 

Can anyone help me with this?

+4
source share
1 answer

You do not need to use keySet to access values in a HashMap . When you HashMap over a HashMap using <c:forEach.. >, you return an 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> 
+4
source

All Articles