I was asking a very similar question to the mailing list a few days ago.
This is the conclusion I did draw from the answers I got:


The answers are:
- Struts 1.0 does not support variable indexing
- there exists a workaround employing some JSP code(see below)
- there exists an inofficial implementation of the feature by Dave Hayes on
http://www.husted.com/about/struts/resources.htm#extensions (I did not
employ it, as I want to stick with the official Struts).

The solution to my problem thus looks like that:

...
<html:form name="report" type="ReportForm" scope="session" action="log.do">
<% String accountVal = null; %>
<% String amountVal = null; %>
<% String remarkVal = null; %>
<logic:iterate name="report" property="entries" scope="session" id="entry"
indexId="entryId">
<tr>
        <td>#<bean:write name="entryId"/></td>
        <td>
          <% accountVal = "entry[" + entryId + "].account"; %>
          <html:text property="<%=accountVal%>"/>
        </td>
        <td>
          <% amountVal = "entry[" + entryId + "].amount"; %>
          <html:text property="<%=amountVal%>"/>
        </td>
        <td>
          <% remarkVal = "entry[" + entryId + "].remark"; %>
          <html:text property="<%=remarkVal%>"/>
        </td>
</tr>
</logic:iterate>
...
</html:form>
...


|-------------------------+------------------------|
|        Guido Roth       |        SYSTOR AG       |
|                         |                        |
|                         |                        |
|   Tel. +41 1 405 35 12  |   Fax +41 1 405 31 13  |
|                         |                        |
|                         |                        |
|  [EMAIL PROTECTED]  |      Baslerstr. 60     |
|                         |       8048 Zürich      |
|-------------------------+------------------------|




                                                                                       
                         
                   "Mike Thompson"                                                     
                         
                   <mrt@instanton.       To:  <[EMAIL PROTECTED]>         
                         
                              com>       cc:                                           
                         
                                         Subject:  Re: variable number of inputs       
                         
                                                                                       
                         
                        03.07.2001                                                     
                         
                             16:01                                                     
                         
                    Please respond                                                     
                         
                    to struts-user                                                     
                         
                                                                                       
                         
                                                                                       
                         


Struts does not necessairly create an ActionForm out of the returned data,
it uses the current one and repopulates it via the property names specified
in your <html:form>.  What you need are indexed properties, see archive.
You'll end up having something like:

throw all this in an iterate tag to iterate over all the rows in your form.
You'll need to declare an index variable in your jsp.

   <html:form action="/lookup">
     <table>
        <%
            int index = 0;
        %>

        <!-- iterate tag goes here -->
       <tr>
         <td><html:text property='<%= "person[" + index + "].first"
%>'/></td>
         <td><html:text property='<%= "person[" + index + "].last"
%>'/></td>
         <td><html:text property='<%= "person[" + index + "].phone"
%>'/></td>
       </tr>

        <%
            ++index;
        %>

     </table>
   </html:form>


----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, July 03, 2001 8:21 AM
Subject: variable number of inputs


> Newbie question...
>
> I need to have a table where each row will have multiple input fields
> and these fields make up a single record of information. I do not
> know before I generate the table how many rows it will have. When the
> user modifies this table and sends it back, struts will want to make
> an ActionForm out of the returned data. The question is, how do I
> handle the variable number of rows?
>
> For example, if each row has a first name, last name, and phone number,
> and I happen to have two rows, I might do something like:
>
>   <html:form action="/lookup">
>     <table>
>       <tr>
>         <td><html:text property="first"/></td>
>         <td><html:text property="last"/></td>
>         <td><html:text property="phone"/></td>
>       </tr>
>       <tr>
>         <td><html:text property="first"/></td>
>         <td><html:text property="last"/></td>
>         <td><html:text property="phone"/></td>
>       </tr>
>     </table>
>   </html:form>
>
> (Of course, since the number of rows is variable, I'd actually do that
> in an iterator, but humor me).
>
> So when the setter methods are called in the ActionForm, the setter for
> each of the fields will get called multiple times. If I can trust the
> order in which they're called, I can regroup them as records later. But
> I don't know if that's kosher.
>
> Another option would be to modify the property names to contain a
> unique number such as:
>
>   <html:text property="first2"/>
>
> But then, of course, I can't statically compile the setter methods for
> each case into the ActionForm. And there doesn't appear to be a method
> in ActionForm that's called for unrecognized properties.
>
> This must be a common problem. How is it usually handled?
>
> Devon







Reply via email to