aspose file tools*
The moose likes Struts and the fly likes HashMap mapped property / action form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "HashMap mapped property / action form" Watch "HashMap mapped property / action form" New topic
Author

HashMap mapped property / action form

H Ritwick
Greenhorn

Joined: Mar 15, 2006
Posts: 18
Hello
I have a HashMap of categorgy VO as the key and ArrayList of ItemBean as value. This has to be displayed and the few of the properties of both and key and value can be edited. Can someone please suggest which indexed property or variation of mapped property to use. can mapped propery take a vo as the key.?.any help / code sample is appreciated.
Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Mapped properties only work if the key is a String. You will need to used indexed properties. Using a Hashmap presents a problem for indexed properties, though, because it is not guaranteed to deliver the elements in any particular order, and having a set order is essential when using indexed properties.

I'd suggest you switch to an implementation of the java.util.Map interface that does support retrieval of elements in a fixed order such as LinkedHashMap or TreeMap. You can then put an indexed getter and seter for the map.entries() collection in your ActionForm and use indexed properties within a logic:iterate tag in your JSP.


Merrill
Consultant, Sima Solutions
H Ritwick
Greenhorn

Joined: Mar 15, 2006
Posts: 18
Thanks for your reply.
I converted my map to LinkedHashMap. I have a qn on adding the getter setter in the action form. Is the below getter setter correct.

public Category getStringIndexed(String key) {
return (Category )categoryItemMap.get(key);
}

public void setStringIndexed(String key, String value) {
categoryItemMap.put(key, value);
}

How to use the iterate tag to get the keys and value to display each category and items for that category.

Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When the Struts <logic:iterate> tag iterates over a java.util.Map, each iteration produces and object of type Map.Entry, which has a key and a value property. Your indexed getter will therefore need to retrieve an object of type Map.Entry. It will look something like this:


Note: if you're not using Java SE 5, you will have to modify the code above to eliminate the generics and the Java 5 for loop.

You will notice that the value part of the Map is of type ItemList rather than List. The reason for this is that in order for you to access the items in the list, you will need a wrapper class for it that has an indexed getter and setter for each item. It will look something like this:


Your JSP will look something like this:


The above assumes that the Category bean has a property named "bar" and the ItemBean has a property named "foo".

Also note that the above indexed getters don't include any "lazy initialization" behavior, so this will only work if the ActionForm is in session scope. If you decide you need it to be in request scope, read this link for hints on how to accomplish this.
[ October 03, 2006: Message edited by: Merrill Higginson ]
H Ritwick
Greenhorn

Joined: Mar 15, 2006
Posts: 18
Merrill
Thank you so much for your detailed reply. It worked almost perfect and I was able to resolve all my issues.
Once again your help is greatly appreciated.

notes:
For me since my itemsList was of type List I did not have to write a wrapper.
also in the jsp syntax use a "." after key.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HashMap mapped property / action form