• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts: Submit form with Map property

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been working with Struts for only a very short time now, but I have also scoured everywhere that I can think of to try to find an answer for this problem! Hopefully someone will have an idea here (or at least be able to tell me that there IS no solution!).

I have a form bean that has a property that's a Map (stored internally as a hashtable). There's both a getter and setter for the property. When I initialize the bean, I get a bunch of GIDs and names for items stored in a database, using the GIDs as the Map key and names for the values.

This works fine for the display portion of the process. I have a view that displays these items like this:

<logic : present name="promoForm" property="supportFiles">
  <logic:iterate id="supportFile" name="promoForm" property="supportFiles">
    <bean:write name="supportFile" property="key" />: <bean:write name="supportFile" property="value" />
  </logic:iterate>
</logic : present>

So this checks that there's a value (in this case a non-null Map object) for the property, then iterates and displays the keys and values.

The problem comes when I submit this form back to the server. Because there's no control for the supportFiles property, that basically returns as null, while all the other attributes that had proper html:* controls are initialized properly.

So here's the core of my question: how can the Map object property be represented in my form with html:* controls so that the set method is called and the property is properly initialized with a Map object?

The only solution I've come up with is to create a bunch of controls like this:

<input type="hidden" name="supportFilesKey1" value="xx" />
<input type="hidden" name="supportFilesValue1" value="yy" />

Then, when the form is posted back to the server, I can go through and look for supportFilesKeyX until such time as that returns null, then turn all of those into a Hashtable and set it. But I'd really rather not. Since there's a way to address the key and value properties of a Map in display, there has to be a way to represent them with controls and have them dealt with properly on the way back in!

Any help on this would be greatly appreciated. If any of this is unclear, just let me know and I'll elaborate appropriately! Thanks!

[ August 25, 2004: Message edited by: Rick Herrick ]
[ August 25, 2004: Message edited by: Rick Herrick ]
 
Rick Herrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a followup. I've continued to look for a solution to this and found this:

http://struts.apache.org/faqs/indexedprops.html (search down to Mapped Properties)

Basically, this doesn't work, unless I'm really missing something. I added a property, along with getter and setter, called objectMapped to my form class. As I sort of expected before I even started, Struts didn't like this. I got the message:

No getter method for property objectMapped(xxx) of bean testForm

where xxx is whatever key I'm trying to pass in. So that doesn't work to get the items, and I still have no way to SET the Map keys and values for the submit back to the server and the contruction of the new bean to represent the data from the form.

What it does show is that there's been some attempt to address this in the past, but apparently not adequately enough
 
Rick Herrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, never mind! I've figured it out. It is NOT wildly obvious!

What you do is add two extra get/set methods for each Map property that you have. These methods should look something like this:



Now, in the form that you want to submit (i.e. that you want to have contain controls to hold the data so that it's passed back and included in the newly created bean for the submit handler), do something like this:



In the generated HTML, you'll see something like this:



Now, when you click the Submit button, if you look at the form object passed into the action class that's handling your form, the Map property is properly initialized. Whew. That was rough.

If you're interested in using this, I wrote up a little sample app to test it out. Hope this helps anyone having similar problems!
[ August 25, 2004: Message edited by: Rick Herrick ]
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic