Iterate over Hashmap with JSTL

B

Bogi

Hi all!

I've a HashMap construct like this:

HashMap all = new HashMap();
int index=0;

....
while(rs.next()){
ResultSetMetaData rsmd = rs.getMetaData();
HashMap h = new HashMap();

for(int i=1;i<=rsmd.getColumnCount();i++){
String col=rsmd.getColumnName(i);
h.put(col, rs.getString(col));
}

all.put(String.valueOf(index), h);
index++;
}
....


I want to know how to iterate over the rows, so that I can retrieve
the values like

....
<c:forEach var="loop" items="${hashmap}" varStatus="status">
<c:eek:ut value="${loop.columnName1}"/>
<c:eek:ut value="${loop.columnName2}"/>
</c:forEach>
....


I trying for hours.
I am really stuck here.


Thank you in advance.
Marc van den Bogaard
(e-mail address removed)
 
A

Andrea Desole

Bogi said:
...
<c:forEach var="loop" items="${hashmap}" varStatus="status">
<c:eek:ut value="${loop.columnName1}"/>
<c:eek:ut value="${loop.columnName2}"/>
</c:forEach>

I don't think you can do this directly. What you can do is to iterate
over the set of the values (given by the values() method), doing
something like:

<c:forEach var="loop" items="${hashmap.values}" varStatus="status">
<c:eek:ut value="${loop.columnName1}"/>
<c:eek:ut value="${loop.columnName2}"/>
</c:forEach>

of course, because you don't have a getter for values, you will have to
implement a small wrapper, or a derived class.

Also, keep in mind that the values in the map are not sorted, so maybe
this is not really what you want
 
B

Bogi

Hello again,

I finally changed my code to:


HashMap first = new HashMap();
first.put("longname", "Deutsch");
first.put("shortname", "de");

HashMap second = new HashMap();
second.put("longname", "Englisch");
second.put("shortname", "en");

HashMap h = new HashMap();
h.put("0", first);
h.put("1", second);

request.setAttribute("hashmap", h);


But I got the same problem.

I just want to know how to iterate over a nested HashMap and access the
key, e.g. in for my source:

<iterate>
<c:eek:ut hashmap.longname>
<c:eek:ut hashmap.shortname>
</iterate>

Does anyone has an example for me?
It would be great.

Thank you.
 
A

Andrea Desole

Bogi said:
Hello again,

I finally changed my code to:


HashMap first = new HashMap();
first.put("longname", "Deutsch");
first.put("shortname", "de");

HashMap second = new HashMap();
second.put("longname", "Englisch");
second.put("shortname", "en");

HashMap h = new HashMap();
h.put("0", first);
h.put("1", second);

request.setAttribute("hashmap", h);


But I got the same problem.

I just want to know how to iterate over a nested HashMap and access the
key, e.g. in for my source:

<iterate>
<c:eek:ut hashmap.longname>
<c:eek:ut hashmap.shortname>
</iterate>

Does anyone has an example for me?

okay, without writing a complete example I'll try to be a bit clearer.
your variable h shouldn't be a map, it should be a vector:

Vector h = new Vector()
h.add( first );
h.add( second );
request.setAttribute("vector", h);

then you can iterate over your vector, and read your maps:

<c:forEach var="map" items="${request.vector}">
<c:eek:ut value="${map.longname}/>
<c:eek:ut value="${map.shortname}/>
</c:forEach>

clear?
 
M

Marc van den Bogaard

Andrea said:
okay, without writing a complete example I'll try to be a bit clearer.
your variable h shouldn't be a map, it should be a vector:

Vector h = new Vector()
h.add( first );
h.add( second );
request.setAttribute("vector", h);

then you can iterate over your vector, and read your maps:

<c:forEach var="map" items="${request.vector}">
<c:eek:ut value="${map.longname}/>
<c:eek:ut value="${map.shortname}/>
</c:forEach>

clear?


Now I got it!

I used the logic:iterate tag instead of c:forEach and it works with the
example above.

Thank you!
 
R

Roedy Green

HashMap first = new HashMap();
first.put("longname", "Deutsch");
first.put("shortname", "de");

HashMap second = new HashMap();
second.put("longname", "Englisch");
second.put("shortname", "en");

HashMap h = new HashMap();
h.put("0", first);
h.put("1", second);
If this is your actual application, you don't need the complexity of
a nested HashMap or dynamically named fields. You just need a Language
class with three fields:

id, longname, shortname.

you can then build a HashMap on those objects keyed by either id,
longname, or shortname. to get the Language object. From there you can
at all three values.

If you want "00" to be treated as "0" then store you id as a int not a
string.

You can define only one hashCode/equals method for your object. That
limits you to one type of lookup for your objects. There is no
equivalent to Comparator for HashMaps.
 
R

Roedy Green

You can define only one hashCode/equals method for your object. That
limits you to one type of lookup for your objects. There is no
equivalent to Comparator for HashMaps.
cancel cancel! I am drooling here confusing HashSets and HashMaps.

Let me quote from http://mindprod.com/hashcode.html#ONEKEY

The One Key Catch

You can define only one hashCode/equals method for your HashSet
Objects. That limits you to one type of HashSet lookup for your
Objects. There is no equivalent to Comparator for HashSets. You can
look up your Objects by only one key, though that key might contain
several fields. You can't have several HashSets each accessing the
same Objects by different keys. You can of course have several
HashSets each accessing a different subset of the same group of
Objects using the same key.

In contrast, with HashMap you have more freedom. Your Objects don't
have to implement a useful hashCode/equals, but any keys you use do.
Since you can define different hashCode/equals for different types of
key, you can have multiple HashMaps on the same group of Objects
looking up by different keys.
 
Joined
May 13, 2008
Messages
1
Reaction score
0
Iterate over Map using JSTL

I know this is a late post, but it took me a while to figure this out and I didn't find an answer here or elsewhere, so I'm posting this to hopefully save other people time. I found two very similar ways to iterate over a Map using JSTL:

1. Use the var attribute:

Code:
<c:forEach items="${sessionScope.myFileUploadMap}" var="mapEntry">
    <tr>
        <td>${mapEntry.value.filename}</td>
        <td>${mapEntry.value.contentType}</td>
        <td>${mapEntry.value.filesize} bytes</td>
        <td>${mapEntry.value.description}</td>
    </tr>
</c:forEach>

2. Use the varStatus attribute:

Code:
<c:forEach items="${sessionScope.myFileUploadMap}" varStatus="status">
    <tr>
        <td>${status.current.value.filename}</td>
        <td>${status.current.value.contentType}</td>
        <td>${status.current.value.filesize} bytes</td>
        <td>${status.current.value.description}</td>
    </tr>
</c:forEach>

Basically, when you iterate over a Map using JSTL, you are iterating over Map.Entry objects and NOT whatever you have placed in the value field. Therefore you need to call getValue (.value above). You can also call getKey (this would be .key above).

http://java.sun.com/javase/6/docs/api/java/util/Map.Entry.html.
 
Joined
Sep 2, 2009
Messages
1
Reaction score
0
An article which explains JSTL for iterating Map can be viewed at tech-freaks.in/Java-Programming/Rapid-Code/iterate-map-jstl-el.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top