On Tue, 16 Jul 2002, Rick Reumann wrote:

> Date: Tue, 16 Jul 2002 22:04:54 -0400
> From: Rick Reumann <[EMAIL PROTECTED]>
> To: Craig R. McClanahan <[EMAIL PROTECTED]>
> Cc: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: Re[2]: getting nested tags to work with DynaActionForm???
>
> On Tuesday, July 16, 2002, 9:04:04 PM, Craig R. McClanahan wrote:
>
> CRM> Setting stuff like this up in the reset() method is the standard approach.
> CRM> Arrays have to exist already for either standard JavaBean-based
> CRM> ActionForms, as well as DynaActionForms.
>
>      I'm still a bit confused by this. When I use a standard
>      ActionForm I don't have to do anything special with my ArrayList
>      in the ActionForm. A page that uses this ArrayList works fine.
>      However as soon as I try to use this ArrayList as property in a
>      DynaActionForm I run into problems trying to submit a jsp page
>      that was populated with the ArrayList info (the display works
>      fine, it's just upon submission).
>

If you're using request scope beans, a new instance gets created on every
request.  And I will bet that you probably have an initialization of this
array happening in your constructor, or in an initialization expression,
right?

For DynaActionForm instances, the default initialization of all
non-primitives in null.  That's why you still need to initialize in
reset(), or use the new "initial" property described below.

> CRM> In recent nightly builds, we added support for an additional mechanism --
> CRM> you can declare an intiialization expression for arrays in the
> CRM> <form-property> for a DynaActionForm bean, using the "initial" attribute.
> CRM> The syntax is basically like what you use in Java to initialize an array
> CRM> to a set of values in a variable declaration -- for example:
>
> CRM>   <form-bean name="myform"
> CRM>              type="org.apache.struts.action.DynaActionForm">
>
> CRM>     <form-property name="intArray" type="int[]"
> CRM>                 initial="{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }"/>
>
> CRM>   </form-bean>
>
>        What if the information in an ArrayList of beans that you want in a
>        DynaActionForm is to first be populated by some database info.
>        Do you need to first initialize it like a above to a bunch of
>        nulls? If so what if the list size fluctuates (hence use of
>        ArrayList) how do you know how many to initialize the ArrayList
>        with?
>

That's definitely a place where loading the arrays in the reset() method
makes sense.

Having an "intArray" property of type "int[]" on a DynaBean is very much
like having the following method signatures on a standard JavaBean:

  public int[] getIntArray();
  public void setIntArray(int intArray[]);

so you don't have to pre-initialze the array to nulls or anything.  Just
set up the array you want as a local variable (of any desired
length), populate its values, and call:

  int intArray[] = ...;
  dynaform.set("intArray", intArray);

One really common scenario is that you don't know ahead of time how many
items you're going to read from the database.  An approach I use a lot is
to use an ArrayList to accumulate the values, then convert them to an
array.  Something like this (assuming you have a "labels" property of
type "java.lang.String[]"):

  ArrayList temp = new ArrayList();
  Connection conn = ...;
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("select label from customer_types");
  while (rs.next()) {
    temp.add(rs.getString(1));
  }
  String labels[] = (String[]) temp.toArray(new String[temp.size()]);
  dynaFormBean.set("labels", labels);

Alternatively, you could set your property type to java.util.List instead
-- all the Struts tags that support indexed access against arrays work
perfectly well against a List as well.

>        Thanks for any more thoughts.
>
> --
>
> Rick
>
> mailto:[EMAIL PROTECTED]
>
>

Craig



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

Reply via email to