JSTL accessing Map

Discussion in 'Java' started by Frederic, Jan 25, 2005.

  1. Frederic

    Frederic Guest

    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
     
    Frederic, Jan 25, 2005
    #1
    1. Advertisements

  2. [I inserted the code you said you were trying to use]
    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>
     
    Chris Riesbeck, Jan 25, 2005
    #2
    1. Advertisements

  3. Frederic

    Frederic Guest

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

    Thanks a lot


     
    Frederic, Jan 26, 2005
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.