File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Iterate over a HashMap where the value is a bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Iterate over a HashMap where the value is a bean" Watch "Iterate over a HashMap where the value is a bean" New topic
Author

Iterate over a HashMap where the value is a bean

Steve Dambrosio
Greenhorn

Joined: Apr 02, 2009
Posts: 28
I can iterate over HashMap *values* no problem when the value is a String. However when the value is a bean I don't know how to get at the properties (the getter methods) of the bean! It seems that struts allows only property="value" or property="key". I need something like property="value, property in value object=fieldname". Can anyone help? Thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

It'd help if you included how you were trying to access the values, and potentially which version of Struts.
Steve Dambrosio
Greenhorn

Joined: Apr 02, 2009
Posts: 28
Struts version is 1.1

I'm using the iterate tag as follows.



Remember, the *value* object is a bean. An interesting side-bar: bean:write actually renders the contents of the bean! This is because the bean has overridden the toString method with one that returns a string containing formatted contents of the instance variables with labels. However, I want just one value, not the whole bean! Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why not set a value with the bean value, then access that? I don't even know how to do that, what with S1.1 being like 10 years old or something... when functionality overlaps with JSTL, you should really be using JSTL. <bean:define>, maybe?
Steve Dambrosio
Greenhorn

Joined: Apr 02, 2009
Posts: 28
Thanks for the prompt reply! Your post is somewhat unclear to me but I think you're saying that I might be able to define a bean, "extract" the HashMap value into the bean and then get at the fields (well, the setter methods). Sounds intriguing given that this would happen within an interate loop. I'll give it some more thought. In the mean time I decided I can change the collection in my ActionClass to be an ArrayList and populate the ArrayList from the HashMap using hashMap.values(). Still there should be an easy way to iterate through the values of a HashMap when the value is a bean! P.S. sorry about the S1.1 but it's a big (er... gigantic) corporation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterate over a HashMap where the value is a bean