Ben Anderson wrote:
> Hi,
> I'm need to iterate over a map and have access to both the keys and
> values.  I was hoping for something like like:
> 
>         <jx2:forEach items="${pages}" var="p">
>           <page key="${p.key}" value="${p.value}""/>
> 
> This doesn't work.  What I've found is that the variable p actually
> has the value in it.
> 
> So, instead of java.util.Map.entrySet().iterator() being called to do
> the iteration, it seems as if java.util.Map.getValues().iterator() is
> called to do this iteration behind the scenes.  This is in contrast to
> jstl.
> 
> Anyways, does anyone know a way to get the key?

This is how I'd do it:

<jx2:forEach var="key" items="${pages.keySet()}">
        <page key="${key}" value="${pages.get(key)}"/>
</jx2:forEach>

In the case of beans I would think that the the forEach just calls on
the iterator() method found in all classes implementing the
java.util.Collection interface (I'm just guessing here).  The var
attribute just picks up the value from iterator.next().   So using
keySet() you have a collection of all keys found in the Map, iterate
through those and just pick up the mapped values with get().

> 
> Thanks,
> Ben
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


-- 
Paul Focke                                http://outerthought.org/
Outerthought                              Open Source Java & XML
paul at outerthought.org

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to