-4

Can someone help on how to display a TreeMap<String, Stats> in a JSP using JSTL?

I am trying to display all the records in a table in the JSP Page.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
VBJ
  • 673
  • 5
  • 14
  • 24

4 Answers4

3

something like this

<c:forEach items="${map}" var="entry">
    ${entry.key} ${entry.value.myProperty}
</c:forEach>

The object value for each of map's entry is referenced as "${entry.value}" and java bean properties in this object will be accessed as ${entry.value.myProperty} where by "myProperty" is readable property on that bean and hence must have a getMyProperty() method in that bean class.

Gladwin Burboz
  • 3,519
  • 16
  • 15
RSH
  • 53
  • 1
  • 5
  • Can anyone give me an example on how to get the values from a object...in the page. Like i mentioned in my question values is an object in my case. So I am not finding an example on how to get the values from obj. Thank you! – VBJ Jun 20 '13 at 02:16
  • Added explanation on how to access properties from the object entry value. – Gladwin Burboz Jun 20 '13 at 17:58
1

Something like this:

<%
for (Map.Entry<String,Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object o = entry.getValue();
%>
out.println(o);
<%
}
>%
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
mconlin
  • 8,169
  • 5
  • 31
  • 37
  • 1
    [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/a/3180202/738746) – Bhesh Gurung Jun 20 '13 at 01:57
  • I want use jstl to display. Don't want to use java code in my jsp page. But thanks for help. – VBJ Jun 20 '13 at 01:57
  • 2
    no idea why I got down voted, I am aware of jstl and taglibs but that was not asked in the question. Tough crowd. – mconlin Jun 20 '13 at 01:59
  • Sorry...but let me add that to my question. – VBJ Jun 20 '13 at 02:00
  • Whether requested or not, you should avoid presenting bad practice as an example. – erickson Jun 20 '13 at 03:03
  • True, my answer should have included both the java and at least a mention of tags as a preferred way to do it and a link. However you dont know the posters needs anymore than I do. I am also a believer in doing things the hard way first to learn, and then using preferred ways second. For example the Head First books dont just jump to tags. You build your understanding, its a journey. – mconlin Jun 20 '13 at 13:45
1

You may try something like -

Iterator<String> = treeMap.keySet().iterator();
PrintWriter out; // out has defined somewhere, may be something else

while(iterator.hasNext()) {
     key = iterator.next();
     out.println(key + " --- " + treeMap.get(key));
     // you can add you html code to suit your needs
}

Hope this help

simpletron
  • 739
  • 4
  • 14
  • 1
    I don't think this should get down voted. Stackoverflow is not where you ask question and copy, paste the answer. My answer show how to print key-value pairs of this map. That's what he need. http://stackoverflow.com/questions/17204010/how-to-display-a-treemapstring-object-in-a-jsp-page-using-jstl?noredirect=1#comment24919724_17204010 – simpletron Jun 20 '13 at 02:14
  • I agree. Some aggressive down voting around here. – mconlin Jun 20 '13 at 02:17
0

Okay after some trails this is how we do it.

<c:forEach items="${treemap}" var="treemap">
<tr >
<td>${treemap.key}</td>
<td>${treemap.value.varNamefromyourPOJO}</td>
</tr>
</c:forEach>
VBJ
  • 673
  • 5
  • 14
  • 24
  • Next time, try to form your question completely before you post it. Then the people trying to help you won't waste their time with answers to the wrong question. – erickson Jun 20 '13 at 03:36