You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kevin Wade <kw...@mindspring.com> on 2007/11/17 05:02:12 UTC

Getting values from iterated objects

I'm having a problem with iterators and getting value updates.  I've  
searched the mailing list for hours but I can't seem to hit on a  
solution.  Perhaps I'm searching for the wrong thing.  Any insights  
would be helpful.  It seems like it ought to be easy.

Let's say I have an object in my action called "report."  And a  
report object has certain fields (such as "title) and also has a Set  
of "author" objects.   And each author object has a "bio" String.  I  
want to display the report title and then each author's bio and allow  
them to be changed via a web page.  Something like this:

<s:textfield value="%{report.title} name="report.title"/>
<s:iterator value="report.authors">
  	<s:textarea value="%{bio}" name="bio" cols="40" rows="4"/>
</s:iterator>

This works fine for displaying the information from the calling  
action: I see the report title and all of the bios for each author  
associated with the report via my jsp output.  No problem.
But, this does NOT work for getting changes made in the bio textarea  
back into my object on post/submit to the next action.

Changes to the report.title textfield work fine.  That works.  It's  
the "bio" textarea that I seem to have a problem with because (I  
assume) it's embedded in the iterator tag.  Simply putting "bio" in  
the textarea's "name" obviously doesn't work.  I've tried about 8 or  
9 different things, but none work. I'm sure it's got to be simple,  
but the solution eludes me.  What should the textarea's "name" be to  
get struts2 to update the specific report.author.bio String?

Thanks,
Kevin

Re: Getting values from iterated objects

Posted by Dave Newton <ne...@yahoo.com>.
The tags support maps, too.

http://struts.apache.org/2.x/docs/type-conversion.html

Dave

--- Kevin Wade <kw...@mindspring.com> wrote:

> Thanks for the tip, Dave.  However, it's not working
> for me.
> 
> I think the problem may be that the "author" objects
> that I'm  
> iterating over is a Set, not a List.  (These objects
> are pojos  
> hydrated by hibernate).  I might be wrong, but I
> didn't think Sets  
> are indexable like Lists.  No?
> 
> I suppose I could always pull the Set of author
> objects out of  
> report, make it a List or something directly on the
> value stack,  
> update that List via the iterator-driven code you've
> given me, and  
> then stuff it all back in as a Set in the final
> action.  Is that the  
> only way to get changes back in to an object in a
> Set?
> 
> Kevin
> 
> On Nov 16, 2007, at Friday, November 16, 2007  -
> 11:21 PM, Dave  
> Newton wrote:
> >
> > One way to create that association is by using
> array
> > notation via <s:iterator...>'s "status" attribute
> [1]
> > which will let you get the list index position;
> you
> > use that to create the name of the
> <s:textarea...>,
> > more or less like this (it's late so my syntax is
> > probably... suspect):
> >
> > <s:iterator value="report.authors" status="stat"
> > id="foo">
> >   <s:textarea
> name="report.authors[%{stat.index}].bio"
> > value="%{#foo}"/>
> > </s:iterator>
> >
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
> 
> 


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


Re: Getting values from iterated objects

Posted by Kevin Wade <kw...@mindspring.com>.
Thanks for the tip, Dave.  However, it's not working for me.

I think the problem may be that the "author" objects that I'm  
iterating over is a Set, not a List.  (These objects are pojos  
hydrated by hibernate).  I might be wrong, but I didn't think Sets  
are indexable like Lists.  No?

I suppose I could always pull the Set of author objects out of  
report, make it a List or something directly on the value stack,  
update that List via the iterator-driven code you've given me, and  
then stuff it all back in as a Set in the final action.  Is that the  
only way to get changes back in to an object in a Set?

Kevin

On Nov 16, 2007, at Friday, November 16, 2007  - 11:21 PM, Dave  
Newton wrote:
>
> One way to create that association is by using array
> notation via <s:iterator...>'s "status" attribute [1]
> which will let you get the list index position; you
> use that to create the name of the <s:textarea...>,
> more or less like this (it's late so my syntax is
> probably... suspect):
>
> <s:iterator value="report.authors" status="stat"
> id="foo">
>   <s:textarea name="report.authors[%{stat.index}].bio"
> value="%{#foo}"/>
> </s:iterator>
>


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


Re: Getting values from iterated objects

Posted by Dave Newton <ne...@yahoo.com>.
--- Kevin Wade <kw...@mindspring.com> wrote:
> What should the textarea's "name" be to get struts2 
> to update the specific report.author.bio String?

The problem is that you have no association between
the "bio" field and a specific report author. 

One way to create that association is by using array
notation via <s:iterator...>'s "status" attribute [1]
which will let you get the list index position; you
use that to create the name of the <s:textarea...>,
more or less like this (it's late so my syntax is
probably... suspect):

<s:iterator value="report.authors" status="stat"
id="foo">
  <s:textarea name="report.authors[%{stat.index}].bio"
value="%{#foo}"/>
</s:iterator>

d.

[1] http://struts.apache.org/2.x/docs/iterator.html


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