Attaching the answer John had given me for indexed properties.
Hope this would help...


----- Original Message ----- From: "Ciaran Hanley" <[EMAIL PROTECTED]>
To: "'Struts User Mailing List'" <struts-user@jakarta.apache.org>
Sent: Thursday, June 23, 2005 3:01 PM
Subject: How to create form rows dynamically


Can somebody help me or propose a solution to the following please.



I wish to create a form dynamically. Depending on the business logic there
could be 0 to N rows in the form. I tried to use a form with an array of
strings and use the indexed="true" setting in the html:text boxes but as I
was not following any example I ran into problems and didn't have any guide
as to where I was going wrong.



I also need to iterate over a bean containing information which corresponds
to the form as the form boxes are being displayed.



Say if there is 3 rows required, it should ok like the following.



Payment     Old Amnt    New Amnt          Date

1           35          [45]              [12/12/04]

2           35          [45]              [12/01/05]

3           35          [45]              [12/02/05]



Where [] represents a html:text box.



I am also unsure as to how to access these values when I get to the
form/action classes.



Any help would be much appreciated!




--- Begin Message ---
In the struts-config.xml:

    <form-bean name="ManageAccountsForm" type="application.EditUsersForm">
        <form-property name="users" type="application.UserBean[]">
    </form-bean>


    <action-mappings>
    <action
      path="/application/EditUsers"
      type="application.EditUsersAction"
      name="EditUsersForm"
      scope="session"
    >
      <forward
        name="success"
        path="editUsers.jsp"
        redirect="false"
      />
    <action-mappings>


In EditUsersAction.java execute method

    // get collection of users from the database
    Collection users = getUserBeans ();

    // put collection into form as an array for editing
    form.set ( "users", users.toArray ( new UserBean[0] ) );

In editUsers.jsp

    <logic:iterate id="users" name="EditUsersForm" property="users">
        <html:text name="users" property="name" indexed="true">
    </logic:iterate>

In the produced HTML:

    <input type="text" name="users[0].name" />

If you need to client side validation, you'll probably need to write your
own JSP to deal with the element above.

As for using validate.xml to validate on the server side. I've never tried
it with arrays, I just iterate over them in the validate (...) method of the
form, like so:

    UserBean users[] = (UserBean[]) form.get ( "users" );
    for ( int i = 0; i < users.length; i++ ) {
        // check on the attributes of UserBean users[i]
    }

Hope that example clears it up for you.

John



On 20050603 5:05 AM, "Nitesh" <[EMAIL PROTECTED]> wrote:

> Thanks for the answer John...
> 
> Could you give me an example as to how we pre populate the array?
> 
> Regards,
> Nitesh
> ----- Original Message -----
> From: "John Fitzpatrick" <[EMAIL PROTECTED]>
> To: "Struts Users Mailing List" <user@struts.apache.org>
> Sent: Thursday, June 02, 2005 6:00 PM
> Subject: Re: Problem using indexed properties and validator framework
> 
> 
>> 
>> For an Array in a DynaForm property, you can either set the size in the
>> form-property descriptor or, in the case of a session form, pre-populate
>> the
>> Array in your action with the number of elements you desire.
>> 
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



--- End Message ---
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to