You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Niall Pemberton <ni...@blueyonder.co.uk> on 2004/10/01 22:58:08 UTC

Re: Help! Iterating through nested hashmap and setting properties

I don't use the nested tags, but the problem is that the way you've code
them its just going to generate a whole load of <input> tags with the same
name...

   <input type="text" name="foo" value="..."/>

(You can verify this easily by looking at the source of the generated html
page)

which means struts will simply try to populate all the values to property
"foo" in your ActionForm - rather than the appropriate "B" bean in your
array of your mapped property A!

The reason this is happening is because you've used the id and name
attributes - so that the nested tags are not doing their *thing* - building
up the nested property name for you. Theoretically if you used something
like the following....

 <nested:iterate name="MyForm" property="A">
     <nested:iterate  property="this/">
          <nested:text property="foo"/>
     </nested:iterate>
 </nested:iterate>


...then it would generate appropriate names for the structure you want,
something like....

   <input type="text" name="A(key1)[0].foo" value="..."/>
   <input type="text" name="A(key1)[1].foo" value="..."/>
   <input type="text" name="A(key2)[0].foo" value="..."/>
   <input type="text" name="A(key2)[1].foo" value="..."/>

However I said theoretically because I don't think this will work - I
believe BeanUtils will NOT cope with the fact that you effectively trying to
index a mapped property and it will "blow up" on that.

I think the only way this is going to work is if you change your structure
to something that BeanUtils can cope with. Maybe your Map property (i.e. A)
should not contain an ArrayList of B, but contain a bean which has a
property which returns an ArraList of B. I think that would require
something like

   <input type="text" name="A(key1).B[0].foo" value="..."/>

which BeanUtils could cope with.

Alternatively you could wrap your Map in a LazyDynaMap - which effectively
changes it from a Map to a DynaBean, use JSTL to iterate over the DynaBean
properties and generate the "name" attributes with the EL expression
language - but maybe thats over cooking the goose.

Niall

----- Original Message ----- 
From: "boukovska" <bo...@yahoo.co.uk>
To: <st...@jakarta.apache.org>
Sent: Thursday, September 30, 2004 5:34 PM
Subject: Help! Iterating through nested hashmap and setting properties


> Hi all
> I have a problem with setting the values on a hierarchy of nested
> properties.
> I have an ActionForm which has a property A of type HashMap. A is
> keyed
> on
> Strings, and each value of A is a Collection (ArrayList). This
> collection
> contains objects of type B.
> I am trying to iterate through the keys A, and for each key, iterate
> through
> the corresponding value (which is an ArrayList of B objects). The I
> want to display a property of
> B
> and let the user change it. I am able to display the properties of
> object B
> on the screen. However, for some reason the <nested:text> tag doesn't
> work -
> the textbox is there, but the input does not populate the form.
> The following is what I am trying to do.
> <nested:iterate id="idForA" property="A">
> <nested:iterate name="idForA" property="value"> <!--this should access
> collection of B objects -->
> <nested:text property="foo"> <!--allow user to enter a
> property of B -->
> </nested>
> </nested>
> </nested>
> The problem is that when I enter a new value for foo, nothing happens;
> the
> value of the nested property is not set. Is it possible to iterate
> through a
> HashMap, and let the user update its values?
> I am quite new to Struts, and urgently need
> this to
> work, but no luck. Any help will be really appreciated!
> Thanks
> Danielle



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