cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Focke <p...@outerthought.org>
Subject Re: <jx:forEach items="${[java.util.Map]}"
Date Tue, 14 Feb 2006 16:04:37 GMT
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: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 


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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message