Yes - yes, and again, yes. Thanks, Ted.
To further expound (in the chance that it might help someone else), when one
needs to iterate through the key/value pairs of a hashmap, instantiate the
object as type Map:
Map aMap = new HasMap();
and place it in the request. When you need to retrieve the elements, you
can save time by using the struts logic:iterate tag as follows:
<logic:iterate name="map" id="mapEntry" scope="request">
<bean:write name="mapEntry" property="key"/>
<bean:write name="mapEntry" property="value"/>
</logic:iterate>
This works b/c the element that gets exposed as the iterator is actually a
Map.Entry object, which contains the key value pairs as properties (although
this utility is only available during the life of the iteration).
Very useful! It reduced my code from a natty 11 lines (see below) down to 4.
Feeling *much* better now,
Melissa
-----Original Message-----
From: Ted Husted [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 24, 2001 1:35 PM
To: [EMAIL PROTECTED]
Subject: Re: Another question about the logic:iterate tag
id is the name you want to give each item in the map as it iterates.
For more see, <
http://jakarta.apache.org/struts/struts-logic.html#iterate >
We are about to add more indexing support, which may help you further.
See the Indexed Tablibs item at
< http://husted.com/about/struts/resources.htm#taglibs >
for an early release.
Melissa wrote:
>
> I have just tried this, but the "id" attribute is apparently required. My
> guess is that this can just be an arbitrary variable... However, the
bigger
> issue seems to be that the iterate tag isn't really suited to the
retrieval
> of more than one oject (i.e. the hashmap's key/value pairs)...
>
> here's the old code I was hoping to "convert":
> <!--% HashMap jmap = (HashMap)request.getAttribute("hmap");
> for (Iterator i = jmap.keySet().iterator(); i.hasNext(); )
{
> String key = (String)i.next();
> Object obj = jmap.get(key);
> %>
> <tr>
> <td><font size=1><%= key
%></font></td>
> <td><font size=1><%=
obj.toString()%></font></td>
> </tr-->
> <%
> }
> %>
>
> Is there a way to integrate this into a Struts iterate tag, or should i
just
> stick with the original code?
>
> Thanks so much for your help.
> Melissa
> (Strutting better every day)
>
> -----Original Message-----
> From: Ted Husted [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, July 24, 2001 11:03 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Another question about the logic:iterate tag
>
> The docs say "JSP bean", but the meaning there is broad. So, yes, you
> can do this:
>
> request.setAttribute("hashmap",hashmap);
>
> ...
>
> <logic:iterate name="hashmap">
>
> To use an actual bean, you would also specify the property
>
> <logic:iterate name="javaBean" property="hashmap">
>
> which resolves to javaBean.getHashmap()
>
> Melissa wrote:
> >
> > Hi again. I am wondering whether the logic:iterate tag requires you to
> use
> > a bean (or bean property) as the collection to iterate over. I would
> > actually like to iterate over a hashmap that i placed in the request in
my
> > action class, but I have not determined through the user docs whether
this
> > is possible or not.
> >
> > Any help would be greatly appreciated!
> >
> > Melissa
>
> -- Ted Husted, Husted dot Com, Fairport NY USA.
> -- Custom Software ~ Technical Services.
> -- Tel 716 737-3463.
> -- http://www.husted.com/about/struts/
-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/