File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts: Submit form with Map property Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: Submit form with Map property" Watch "Struts: Submit form with Map property" New topic

Struts: Submit form with Map property

Rick Herrick

Joined: Aug 25, 2004
Posts: 11
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 : 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<br />C#/.NET, Java, Ruby, Agile as hell
Rick Herrick

Joined: Aug 25, 2004
Posts: 11
Here's a followup. I've continued to look for a solution to this and found this: (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

Joined: Aug 25, 2004
Posts: 11
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 ]
I agree. Here's the link:
subject: Struts: Submit form with Map property
jQuery in Action, 3rd edition