You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rick Reumann <st...@reumann.net> on 2004/10/12 17:14:37 UTC

Can lazyList help out for indexed arrays in ActionForm?

I have a situation where I need to use fields on form that are indexed...

tier[0], tier[1], etc. (possibly more)

Is there a way I could use a LazyList implmentation to grow this if the 
  form property is defined as String[]? (I know how to grow a List in 
the reset using LazList but not sure of an easy way to do this with an 
array unless I create my own setters that will grow the array and use 
those different setters in the form ie. tierIndexed[0] etc )

TIA for any help. (If I hardcode a size in the reset for the array 
things are fine, but of course I want to avoid just declaring some 
arbitrary size).

I've looked over Niall's wiki but still stumped on this since that wiki 
is dealing with dyna implementations 
http://wiki.apache.org/struts/StrutsCatalogLazyList

-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Can lazyList help out for indexed arrays in ActionForm?

Posted by Rick Reumann <st...@reumann.net>.
Niall Pemberton wrote the following on 10/13/2004 9:09 AM:

> You should be able to do this in the indexed setter, something like this....
> 
> protected String[] myStringArray;
> 
> public void setMyString(int index, String myString) {
> 
>           // Allocate the array
>           if  (myStringArray == null) {
>                myStringArray = new String[index + 1];
>           }
> 
>           // Grow the array
>           if (index >= myStringArray.length) {
>                String[] newMyStringArray = new String[index + 1];
>                System.arraycopy(myStringArray, 0, newMyStringArray , 0,
> myStringArray.length);
>                myStringArray = newMyStringArray ;
>           }
> 
>           // Set the indexed property
>           myStringArray[index] = myString;
> 
> }

Thanks Niall.. that's what I was hoping to avoid having to code:)

> Alternatively, just use a LazyValidatorForm and define the String[] as a
> property in the struts-config and it will do this for you.

Ok, cool. I'll try that right now. Thanks a million.


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Can lazyList help out for indexed arrays in ActionForm?

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Rick,

You should be able to do this in the indexed setter, something like this....

protected String[] myStringArray;

public void setMyString(int index, String myString) {

          // Allocate the array
          if  (myStringArray == null) {
               myStringArray = new String[index + 1];
          }

          // Grow the array
          if (index >= myStringArray.length) {
               String[] newMyStringArray = new String[index + 1];
               System.arraycopy(myStringArray, 0, newMyStringArray , 0,
myStringArray.length);
               myStringArray = newMyStringArray ;
          }

          // Set the indexed property
          myStringArray[index] = myString;

}

Alternatively, just use a LazyValidatorForm and define the String[] as a
property in the struts-config and it will do this for you.

Niall

----- Original Message ----- 
From: "Rick Reumann" <st...@reumann.net>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, October 12, 2004 4:14 PM
Subject: Can lazyList help out for indexed arrays in ActionForm?


> I have a situation where I need to use fields on form that are indexed...
>
> tier[0], tier[1], etc. (possibly more)
>
> Is there a way I could use a LazyList implmentation to grow this if the
>   form property is defined as String[]? (I know how to grow a List in
> the reset using LazList but not sure of an easy way to do this with an
> array unless I create my own setters that will grow the array and use
> those different setters in the form ie. tierIndexed[0] etc )
>
> TIA for any help. (If I hardcode a size in the reset for the array
> things are fine, but of course I want to avoid just declaring some
> arbitrary size).
>
> I've looked over Niall's wiki but still stumped on this since that wiki
> is dealing with dyna implementations
> http://wiki.apache.org/struts/StrutsCatalogLazyList
>
> -- 
> Rick



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Can lazyList help out for indexed arrays in ActionForm?

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
Not being familiar with lazyList, I thought you didn't have to do 
anything to get it to populate from the request params (or would that be 
too lazy?)

since you're thinking of coding in the reset() anyway, then you should 
do a check on the size of the array you need there and size your array 
accordingly.

Sorry if this is wide of the mark but this is way too early in the 
morning for me.

On 10/13/2004 04:43 AM Rick Reumann wrote:
> Adam Hardy wrote the following on 10/12/2004 5:46 PM:
> 
>> I thought that the servlet container would just pass in the parameter 
>> 'tier' as an array of the appropriate size already, in one hit so to 
>> speak.
>>
>> Or am I totally wrong?
> 
> 
> I'm sure it would if I defined each property in the form like...
> 
> <... property="tier" ../>
> <... property="tier" ../>
> <... property="tier" ../>
> 
> When it submits it would populate them into my array fine. I'm running 
> into trouble because I'm defining them as
> 
> <... property="tier[0]" ../>
> <... property="tier[1]" ../>
> <... property="tier[2]" ../>
> 
> (I'm only doing this because I have to do some funky javascript stuff 
> and it's much easier calling the field I'm concerned with by name (ie 
> tier[0] )
> 
> I'm probably just doing something stupid that I can't get this to work 
> as expected.
> 
> <original post below:>
> ----------------------
> 
> I have a situation where I need to use fields on form that are indexed...
> 
> tier[0], tier[1], etc. (possibly more)
> 
> Is there a way I could use a LazyList implmentation to grow this if the 
>  form property is defined as String[]? (I know how to grow a List in the 
> reset using LazList but not sure of an easy way to do this with an array 
> unless I create my own setters that will grow the array and use those 
> different setters in the form ie. tierIndexed[0] etc )
> 
> TIA for any help. (If I hardcode a size in the reset for the array 
> things are fine, but of course I want to avoid just declaring some 
> arbitrary size).
> 
> I've looked over Niall's wiki but still stumped on this since that wiki 
> is dealing with dyna implementations 
> http://wiki.apache.org/struts/StrutsCatalogLazyList
> 
> 


-- 
struts 1.2 + tomcat 5.0.19 + java 1.4.2
Linux 2.4.20 Debian


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Can lazyList help out for indexed arrays in ActionForm?

Posted by Rick Reumann <st...@reumann.net>.
Adam Hardy wrote the following on 10/12/2004 5:46 PM:

> I thought that the servlet container would just pass in the parameter 
> 'tier' as an array of the appropriate size already, in one hit so to speak.
> 
> Or am I totally wrong?

I'm sure it would if I defined each property in the form like...

<... property="tier" ../>
<... property="tier" ../>
<... property="tier" ../>

When it submits it would populate them into my array fine. I'm running 
into trouble because I'm defining them as

<... property="tier[0]" ../>
<... property="tier[1]" ../>
<... property="tier[2]" ../>

(I'm only doing this because I have to do some funky javascript stuff 
and it's much easier calling the field I'm concerned with by name (ie 
tier[0] )

I'm probably just doing something stupid that I can't get this to work 
as expected.

<original post below:>
----------------------

I have a situation where I need to use fields on form that are indexed...

tier[0], tier[1], etc. (possibly more)

Is there a way I could use a LazyList implmentation to grow this if the 
  form property is defined as String[]? (I know how to grow a List in 
the reset using LazList but not sure of an easy way to do this with an 
array unless I create my own setters that will grow the array and use 
those different setters in the form ie. tierIndexed[0] etc )

TIA for any help. (If I hardcode a size in the reset for the array 
things are fine, but of course I want to avoid just declaring some 
arbitrary size).

I've looked over Niall's wiki but still stumped on this since that wiki 
is dealing with dyna implementations 
http://wiki.apache.org/struts/StrutsCatalogLazyList


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Can lazyList help out for indexed arrays in ActionForm?

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
On 10/12/2004 04:14 PM Rick Reumann wrote:
> I have a situation where I need to use fields on form that are indexed...
> 
> tier[0], tier[1], etc. (possibly more)
> 
> Is there a way I could use a LazyList implmentation to grow this if the 
>  form property is defined as String[]? (I know how to grow a List in the 
> reset using LazList but not sure of an easy way to do this with an array 
> unless I create my own setters that will grow the array and use those 
> different setters in the form ie. tierIndexed[0] etc )
> 
> TIA for any help. (If I hardcode a size in the reset for the array 
> things are fine, but of course I want to avoid just declaring some 
> arbitrary size).

I thought that the servlet container would just pass in the parameter 
'tier' as an array of the appropriate size already, in one hit so to speak.

Or am I totally wrong?

Adam


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org