I have the same problem - form fields embedded in an iterate tag.

Would it be possible to change tags such as CheckboxTag and BaseFieldTag so
that they detect when they are embedded in the "iterate" tag and format the
name attribute appropriately.

I tried changing these two tags replacing the following line in the the
doStartTag() method:

    results.append(property);

With the following code and it worked well, automatically populating my
collection objects:

    results.append(decideName());

    /**
     * Determine the name attribute
     */
    private String decideName() {

      // Check if this Tag is embedded in an Iterator Tag
      Tag tag = findAncestorWithClass(this, IterateTag.class);

      // No iterator, use the property as the name
      if (tag == null)
        return this.property;

      IterateTag iterator = (IterateTag)tag;
      int index = iterator.getLengthCount()-1;

      return iterator.getProperty()+"["+index+"]."+this.property;

    }

Niall

P.S. thanks for Struts - great framework.

> -----Original Message-----
> From: Uwe Pleyer [mailto:[EMAIL PROTECTED]]
> Sent: 25 March 2001 07:24
> To: [EMAIL PROTECTED]
> Subject: Re: form question
>
>
> Hey,
>
> I had some hard nights fighting with exactly this problem and got
> a solution
> wich is not very elegant but works...
>
>
> <html:form action="/formAction.do">
> <table border="1" width="100%">
> // some headings for the table
>   <% int i = 0; %>
>   <logic:iterate name="Bean" property="beanItems" id="anything">
>       <tr>
>         <td>
>           <html:text name="Bean" property='<%= "beanItems[" + i +
> "].amount"
> %>'/>
>         </td>
>         <td>
>         $<bean:write name="Bean" property='<%= "beanItems[" + i +
> "].total"
> %>' filter="true" />
>         </td>
>         <td>
>         <html:checkbox name="Bean" property='<%= "beanItems[" + i +
> "].checkbox" %>'/>
>         </td>
>         <td>
>      </tr>
>      <% i++; %>
>   </logic:iterate>
>   </table>
>    <html:submit/>
> </html:form>
>
> As you see, you use the collektion of your formbean directly. The
> Bean needs
> properties, getter and setter Methods according to the Javabean-Spec whats
> the reason to change BeanItems to beanItems!
>
> private Collection beanItems;
> public Item getBeanItems(int index)
> public void setBeanItems(int index, Item item)
> public Item[] getBeanItems()
> public void setBeanItems(Item[] items)
>
> It's also important to configure your formbean with scope="session" in
> Struts-config and never to destroy your Collection in the
> reset-Method! The
> HTML for the input field will show like this: <input type="text"
> name="beanItems[0].amount" value="123">
>
> After submit Struts uses the following setter Method:
> Bean.getBeanItems(0).setAmount("123"); As struts use the getBeanItems(i)
> Method to get yor Item-Objects, there is no chance to create them in the
> request-scope. There must be held from the moment you establish your
> beanItems-Collection for output till the submit of the user in
> session-scope.
>
> Hope that helps
>
> Uwe
>
>
> JOEL VOGT schrieb:
>
> > Hi all,
> >
> > I have a html form on a jsp page. This form has a table generated by the
> > logic iterate tag.
> > In each table row, there are two fields, one a checkbox and the other a
> > text field.
> > What I want is when the user clicks 'submit' all the values are sent to
> > an action form for storing and then to my servlet for processing.
> > How do I make a form that will automatically be populated by all these
> > values? In other words I'm stuffed please help ;)
> >
> > Thanks, Joel.
> >
> > Sample jsp:
> >
> > <html:form action="/formAction.do">
> > <table border="1" width="100%">
> > // some headings for the table
> >   <logic:iterate name="Bean" property="BeanItems" id="bean">
> >       <tr>
> >         <td>
> >           <html:text name="bean" property="amount"/>
> >         </td>
> >         <td>
> >         $<bean:write name="bean" property="total" filter="true" />
> >         </td>
> >         <td>
> >         <html:checkbox name="bean" property="checkbox"/>
> >         </td>
> >         <td>
> >      </tr>
> >   </logic:iterate>
> >   </table>
> >    <html:submit/>
> > </html:form>
> >
> > How do I get these values into a form then servlet then database?
>
>

Reply via email to