Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Struts and the fly likes Accessing a Map with Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing a Map with Struts" Watch "Accessing a Map with Struts" New topic
Author

Accessing a Map with Struts

Adam Kreiss
Ranch Hand

Joined: Sep 12, 2005
Posts: 35
Can you access individual Map elements in struts JSP tags?

I have a map of employees that need to be displayed on a page. The problem is that these employees cannot be displayed in any order. I have a set order they must be displayed (based on their jobs).

So is there any way (sort of converting the Map to a Collection) that I could access individual elements via the key within the JSP? (Java in the JSP is undesirable as well)

Thanks!
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Have your employee class implement Comparator to sort on the job field and use a TreeMap, then you can just iterate through the map.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, you might consider using a different implementation of the java.util.Map interface. A java.util.LinkedHashMap will retrieve entries in the order they were added just like an ArrayList does. If you want a map that is always in key order, use java.util.TreeMap.

In answer to your question: Yes, you can access maps by keys using Struts tags. example:

HashMap myMap is a property of myForm:

"one", 1
"two", 2
"three", 3

<bean:write name="myForm" property='myMap("one")' />

This expression evaluates to 1.
[ September 01, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Adam Kreiss
Ranch Hand

Joined: Sep 12, 2005
Posts: 35
Thanks for the input. And believe me, I would love to go the Comparator or Linked paths but due to the way the data is coming in, I can't guarantee I'll be putting it in the Map in the correct order nor can I order them manually.

Thanks for the sample, thats what I'll be doing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a Map with Struts
 
Similar Threads
Question on Maps and Iterators
html link with params
Access Custom Resource Bundle?
Quession obout cellection
JSP hiding