You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Carr <ja...@gmail.com> on 2008/04/25 00:06:14 UTC

[S2] Populate a List property?

Hi All,

I want to be able to submit a form that can add array elements to one
of my action properties. I have something like this:

public class SomeAction{
     private FooBar foo;
     // getters and setters, execute, etc. here
}

with

public class FooBar{
    private List<Bar> bars;
    // getters and setters
}


how can I submit elements in a way that adds new elements to the list
in FooBar? I can set FooBar fine, but trip up on the array portion. I
need to know how to do this without using the s2 taglib, as I am
sending a json request.

Thanks,
James

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


Re: [S2] Populate a List property?

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
James Carr wrote:
> Hi All,
>
> I want to be able to submit a form that can add array elements to one
> of my action properties. I have something like this:
>
> public class SomeAction{
>      private FooBar foo;
>      // getters and setters, execute, etc. here
> }
>
> with
>
> public class FooBar{
>     private List<Bar> bars;
>     // getters and setters
> }
>
>
> how can I submit elements in a way that adds new elements to the list
> in FooBar? I can set FooBar fine, but trip up on the array portion. I
> need to know how to do this without using the s2 taglib, as I am
> sending a json request.
>
> Thanks,
> James
>
>   

Assuming valueA and valueB are properties of Bar:

/some.action?foo.bars[0].valueA=textA&foo.bars[0].valueB=textB&foo.bars[1].valueA=textC&foo.bars[1].valueB=textD

That reads set valueA of index 0 in the bars list of foo to "textA".  It 
should be URL encoded.

Important:
 The list needs to either be instantiated before the parameter 
interceptor sets values on it, or the list needs to be able to be 
instantiated by the framework.  This is the point that causes most 
people to stumble and the feedback from the framework is poor.
 You'll save yourself some effort if the constructor of FooBar 
initialises bars so the framework sees a non-null concrete list 
implementation.



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