JSTL accessing Map

F

Frederic

Hi all,

I have a problem with JSTL. I have in session an Map with a String as a
key and a collection as value. I would like to iterate throught all key,
and for each key, itearate througt collection value.

Here is a jsp code that work great
<c:forEach var="objectType" items="${QUERYRESULT.typeList}">
<c:eek:ut value="${objectType}"/> -
<logic:iterate id="leo" name="QUERYRESULT" property =
"objects(${objectType})">
<bean:write name="leo" property="name.value"/><br/>
</logic:iterate>
</c:forEach>


with this bean

SortedMap tm = new TreeMap();
public Collection getTypeList() {
return tm.keySet();
}

public Collection getObjects(String objectType) {
return (Collection) tm.get(objectType);
}



I'm trying to use a second foreach tag to access collection value

<c:forEach var="eo" items='${QUERYRESULT.objects[objectType]}'>
${eo.name.value}
</c:forEach>

but it's not working, it's not even calling getObjects method ...


what's wrong ? I tries many solution with name index.

Thanks

Fred
 
C

Chris Riesbeck

Frederic said:
Hi all,

I have a problem with JSTL. I have in session an Map with a String as a
key and a collection as value. I would like to iterate throught all key,
and for each key, itearate througt collection value.

<c:forEach var="objectType" items="${QUERYRESULT.typeList}">
<c:eek:ut value="${objectType}"/> -
<c:forEach var="eo" items='${QUERYRESULT.objects[objectType]}'>
${eo.name.value}
</c:forEach>
</c:forEach>

[I inserted the code you said you were trying to use]
with this bean

SortedMap tm = new TreeMap();
public Collection getTypeList() {
return tm.keySet();
}

public Collection getObjects(String objectType) {
return (Collection) tm.get(objectType);
}

but it's not working, it's not even calling getObjects method ...

getObjects() isn't returning a Map, so objects[objectType]
isn't valid. If QUERYRESULT is the Map, you should be able
to write

${QUERYRESULT[objectType]}

But why not simply use the Map.Entry directly?

<c:forEach var="entry" items="${QUERYRESULT}">
<c:eek:ut value="${entry.key}"/> -
<c:forEach var="eo" items='${entry.value}'>
${eo.name.value}
</c:forEach>
</c:forEach>
 
F

Frederic

Yes great, that's what I was looking for.

Thanks a lot


Chris said:
Hi all,

I have a problem with JSTL. I have in session an Map with a String as a
key and a collection as value. I would like to iterate throught all key,
and for each key, itearate througt collection value.

<c:forEach var="objectType" items="${QUERYRESULT.typeList}">
<c:eek:ut value="${objectType}"/> -
<c:forEach var="eo" items='${QUERYRESULT.objects[objectType]}'>
${eo.name.value}
</c:forEach>
</c:forEach>


[I inserted the code you said you were trying to use]

with this bean

SortedMap tm = new TreeMap();
public Collection getTypeList() {
return tm.keySet();
}

public Collection getObjects(String objectType) {
return (Collection) tm.get(objectType);
}

but it's not working, it's not even calling getObjects method ...


getObjects() isn't returning a Map, so objects[objectType]
isn't valid. If QUERYRESULT is the Map, you should be able
to write

${QUERYRESULT[objectType]}

But why not simply use the Map.Entry directly?

<c:forEach var="entry" items="${QUERYRESULT}">
<c:eek:ut value="${entry.key}"/> -
<c:forEach var="eo" items='${entry.value}'>
${eo.name.value}
</c:forEach>
</c:forEach>
 

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,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top