The <logic:iterate> tag has a 'type' attribute, which can be used to specify
the type to be used for the element exposed through the 'id' attribute. This
should allow you to get rid of the "cast" like this:
<logic:iterate name="invoiceForm"
property="invoice.invoiceLineItems"
id="lineItem"
type="InvoiceLineItem" >
<bean:write name="lineItem" property="aLineItemProperty"/>
</p>
</logic:iterate>
--
Martin Cooper
----- Original Message -----
From: "Ali Ozoren" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, September 22, 2001 10:52 PM
Subject: Postmortem (iterate, wrap, hashtable, nested)
> Hi all-
>
> I finally solved my iterate problem. It was a learning experience, and
very
> time consuming. Hence here is the distilled version briefly.
>
> We have an "Invoice" business object (what a fancy name for a plain
"bean")
> with a member "invoiceLineItems" of type Hashtable. It has static methods
to
> instantiate a "Invoice" object from db and public method to save itself to
> db. It has no knowledge of it's "container" which is a servlet engine this
> time.
>
> We have an "invoiceForm" view object of type "InvoiceForm", which in
return
> is an ActionForm, with a member property "invoice" of type "Invoice". In
the
> default constructor of "InvoiceForm", an "Invoice" is instantiated and
> assigned to "invoice" member.
>
> We have a "invoice.jsp" that refers to "InvoiceForm"'s elemets as follows:
>
> <html:text property="invoice.aProperty" size="16" maxlength="16"/>
>
> The individual "InvoiceLineItem"s stored in
> invoiceForm->invoice->invoiceLineItems are referred as follows:
>
> <logic:iterate name="invoiceForm" property="invoice.invoiceLineItems"
> id="lineItemAsEntry"> //this only returns an Map.Entry, where's
> introspection?
> <bean:define id="lineItem" name="lineItemAsEntry" property="value"
> type="InvoiceLineItem"/> //now we cast
> <bean:write name="lineItem" property="aLineItemProperty"/> //finally
we
> have an InvoiceLineItem!
> </p>
> </logic:iterate>
>
> So, I had to cast the individual Hashtable entries to type InvoiceLineItem
> to be able to access its properties. Even after so, there is one problem
> remained. I got back the elements in the wrong order. It iterated like
> 3-1-2, 1 being the first element I put into the hashtable.
>
> I hope I shed some light into the infamous "iteration" problem. I thank
Adam
> for the generous help which helped me tackle it from a different angle.
>
> --a
>
> PS. Also, don't forget to put <%@ taglib uri="/WEB-INF/struts-logic.tld"
> prefix="logic" %> at the beginning of the page.
>