I'm trying to use <logic:iterate> within a form that contains a Collection of OrderItems to create the appropriate input fields.
 
Example Screen:
 
Order Number: ----------------
List of Items:
Item Number     Name    Quantity
------------------    ------------   ---------
------------------    ------------   ---------
------------------    ------------   ---------
------------------    ------------   ---------
------------------    ------------   ---------
 
(where ------- are form input fields)
The bean the form uses is the OrderForm. It contains an ArrayList of OrderItems. An OrderItem is an object with 3 fields ItemNumber, Name, Quantity.
 
Using <logic:iterate> to iterate over the ArrayList of OrderItems,  and the struts form tags I am trying to generate the correct input lines for each OrderItem.
 
Using
<logic:iterate id="orderlist" name="orderform"  property="items" length="2">
<form:text name="orderlist" property="itemNumber" />
</logic:iterate>
 
... does not work as required. It generate several lines with the same name for itemNumber for each item in the OrderItems ArrayList. When the form is submitted the struts framework cannot get the right values to set the OrderItems ArrayList with the correct values.
 
Setting the OrderItems collection within the OrderForm bean is where the problem is... Getting values can be worked around by using
<form:text name="orderlist" property="itemNumber"
            value="<%= ((OrderItem)orderlist).getItemNumber() %>" />
 
(which I had showed below)....
 
Hope that helps....
 
   -Chandan.
   
 
 
-----Original Message-----
From: Ted Husted <[EMAIL PROTECTED]>
To: Struts List <[EMAIL PROTECTED]>
Date: Wednesday, December 27, 2000 9:50 AM
Subject: Re: Help: <logic:iterate> and form input fields

A clear statement of what you need to accomplish, without posing a particular implementation, might be helpful. I'm not exactly sure what it is you need to do. My first guess is that the part about "count" should be encapsulated in a bean, and not exposed to the JSP. But I'm not sure if I understand the business or application logic behind your question well enough.

*********** REPLY SEPARATOR ***********

On 12/27/2000 at 8:51 AM Chandan Kulkarni wrote:
Actually OrderItem is an object with several fields. I trimmed it down to just one field to simplify the problem. So "items" is an ArrayList of OrderItems.
 
I couldn't find the right combination of nested/indexed property names for the form:text tag to generate the correct input tag. Here's what I tried...
 
<form:text name="orderItems" property="<%= "orderitems[" + count + "]" %>"
            value="<%= ((OrderItem)orderlist).getItemNumber() %>" />
When it can't find the correct property name in the bean it gives a compilation error. I used a run-time expression to create items[0].itemName. In this case the form:text tag is just ignored and is left in the html generated just as is...
 
It seems the only way to use an indexed/nested property name is to use the <input> tag itself.  
 
I am using the struts 12/15 build.... 
 
Thanks,
 
    -Chandan  
-----Original Message-----
From: Ted Husted <[EMAIL PROTECTED]>
To: Struts List <[EMAIL PROTECTED]>
Date: Tuesday, December 26, 2000 4:32 PM
Subject: Re: Help: <logic:iterate> and form input fields

If I understand the situation, you might be able to do this with an array of orderItem beans (orderItems), with getName and getValue methods.
 
<logic:iterate name="orderItems" id="orderItem" >
 <tr>
 <TD> 
   <form:text name="orderItem" />
  </td> 
  </TR>
</logic:iterate>
 
form:text should then call getName and getValue for the name and value of each text field.
 
getName might use and internal counter to give the text fields different names.

*********** REPLY SEPARATOR ***********

On 12/26/2000 at 3:50 PM Chandan Kulkarni wrote:

I am trying to use <logic:iterate> to iterate through a list of order items.
I cannot get it to work. I have seen several emails about patches for handling nested and indexed properties...
 
OrderForm consists of an ArrayList of items.
 
I have added indexed getters
   ArrayList getItems()
 and
   OrderItem getItems(int index)
 
I can't see how an indexed setter would help for setting the properties of OrderItems.
  
Here are parts of my orderform.jsp
==============================================================
 
<form:form action="orderform.do" name="orderform" type="OrderForm" >
 

<TABLE>
<TR>
<TD> Order Number : </TD>
<TD> <form:text name="orderform" property="orderNumber" />  </TD>
</TR>
      
...
<% int count=0; %>
<logic:iterate id="orderlist" name="orderform"  property="items" length="2">
 <tr>
 <TD> 
   <form:text name="orderlist" property="itemNumber"
            value="<%= ((OrderItem)orderlist).getItemNumber() %>" />
  </td>
 
  </TR>
<% count++; %>
</logic:iterate>
 
<form:submit/>
</form:form>
==============================================================
 
Unfortunately this generates input tags with identical names for each iteration. This is a problem when you try to set the properties in the items.
The value= part of the <form:text> tag is a hack to get the values from the Bean.
 

Since the name generated needs to be unique I replaced the form:text lines with input tags like this:
 
    <input TYPE="TEXT" name="<%= "items.itemName[" + count++ + "]" %>" value="<%= ((OrderItem)orderlist).getItemName() %>"  />
 
the run-time expression used above cannot be used in the form:text property value, so you have to use the input tag.
 
This still does not allow for setting the values in the OrderItem...
 
Also what do you think of the name used above...
should it be
   items[0].itemName (more semantically correct - I think)
 
or should it be
   items.itemName[0]...
 
I have tried both but that doesn't work...
 
Does anyone have any pointers/suggestions to get this to work...?
 
Thanks in advance....
 
   -Chandan

Reply via email to