You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by "John J. Thomas" <jo...@sbti.com> on 2001/06/08 22:43:43 UTC

Use of HashMaps via logic:iterate and bean:write

We have written XML driven data access components that implement the
Collection interface. A data access object retrieves data from the JDBC
ResultSet then uses the query’s metadata to load the rows into HashMaps.
Each database row is stored in a HashMap. All said and done, we have a
collection of HashMaps.

Now, how do we access the HashMap's values in the logic:iterate tag? The
traditional bean:write tag wants a bean but we have a HashMap? We have
looked through Apache Tablibs but we haven’t found any existing tag to
handle an existing HashMap. If there is no existing way to get to this data,
we can write such a tag.

Thanks,

JT


RE: Use of HashMaps via logic:iterate and bean:write

Posted by "John J. Thomas" <jo...@sbti.com>.
Thanks Martin,

After reading the last few emails, I now realize that this thread is more
appropriate for the Struts' users group.

The trouble is I'm iterating over a collection of HashMaps. The collection
represents the queried data, the HashMap represents a row of data. So, the
iterate tag retrieves the HashMap from the Collection, we then need to write
the appropriate column data stored in the HashMap.

Are you stating that the following will work?

<!-- iterate through the collection, the element is a HashMap -->
<logic:iterate id="hashMap" name="collection">

  <!-- write out the value of the HashMap via the key -->
  <bean:write name="hashMap" property="key">

</logic:iterate>

Thanks for your help, I'm just digging into Struts so I'm rather green with
the plethora of tags it supports. It looks like a cool framework. We had
rolled our own MVC architecture over the last 18 months but we felt that we
needed to go with a framework that had some momentum in the industry. There
where a few things I liked about our architecture over Struts.

First, our "Action" objects had no tie to the HTTP request or response. Our
RequestToAction translator created the appropriate Action then set the
Action's properties. Our Action was nothing more than a context free user
gesture.

Secondly, we had well defined interfaces for our Models (stateless and
stateful). Implement a Stateful model and our app controller would push the
object on the session.

>From what I've read about Struts, it looks like the Action performs the
business logic and "models" are simply the data or the state of the
application. I have a difficult time understanding why you would want to
tightly couple your business logic (Action) to HTTP specific interfaces.
Granted, if you needed to migrate business logic code (Actions) to say a
Swing application, you could create "front end" objects that implement
HttpServletRequest and HttpServletResponse but that seems rather cumbersome.
I don't believe business logic components should have any reference to these
Servlet specific interfaces.

Anyway, thanks again for the time you spent responding to my question.

Regards,

JT


-----Original Message-----
From: Martin Cooper [mailto:martin.cooper@tumbleweed.com]
Sent: Friday, June 08, 2001 9:12 PM
To: struts-dev@jakarta.apache.org
Subject: Re: Use of HashMaps via logic:iterate and bean:write


This is documented in the logic taglib reference. Here's the relevant
section:

-----
Normally, each object exposed by the iterate tag is an element of the
underlying collection you are iterating over. However, if you iterate over
a Map, the exposed object is of type Map.Entry that has two properties:

key - The key under which this item is stored in the underlying Map.
value - The value that corresponds to this key.

So, if you wish to iterate over the values of a Hashtable, you would
implement code like the following:

<logic:iterate id="element" name="myhashtable">
Next element is <bean:write name="element" property="value"/>
</logic:iterate>
-----

Hope this helps.

--
Martin Cooper


At 01:43 PM 6/8/01, John J. Thomas wrote:
>We have written XML driven data access components that implement the
>Collection interface. A data access object retrieves data from the JDBC
>ResultSet then uses the query's metadata to load the rows into HashMaps.
>Each database row is stored in a HashMap. All said and done, we have a
>collection of HashMaps.
>
>Now, how do we access the HashMap's values in the logic:iterate tag? The
>traditional bean:write tag wants a bean but we have a HashMap? We have
>looked through Apache Tablibs but we haven't found any existing tag to
>handle an existing HashMap. If there is no existing way to get to this
data,
>we can write such a tag.
>
>Thanks,
>
>JT



Re: Use of HashMaps via logic:iterate and bean:write

Posted by Martin Cooper <ma...@tumbleweed.com>.
This is documented in the logic taglib reference. Here's the relevant section:

-----
Normally, each object exposed by the iterate tag is an element of the 
underlying collection you are iterating over. However, if you iterate over 
a Map, the exposed object is of type Map.Entry that has two properties:

key - The key under which this item is stored in the underlying Map.
value - The value that corresponds to this key.

So, if you wish to iterate over the values of a Hashtable, you would 
implement code like the following:

<logic:iterate id="element" name="myhashtable">
Next element is <bean:write name="element" property="value"/>
</logic:iterate>
-----

Hope this helps.

--
Martin Cooper


At 01:43 PM 6/8/01, John J. Thomas wrote:
>We have written XML driven data access components that implement the
>Collection interface. A data access object retrieves data from the JDBC
>ResultSet then uses the query's metadata to load the rows into HashMaps.
>Each database row is stored in a HashMap. All said and done, we have a
>collection of HashMaps.
>
>Now, how do we access the HashMap's values in the logic:iterate tag? The
>traditional bean:write tag wants a bean but we have a HashMap? We have
>looked through Apache Tablibs but we haven't found any existing tag to
>handle an existing HashMap. If there is no existing way to get to this data,
>we can write such a tag.
>
>Thanks,
>
>JT