• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a Map with Struts

 
Adam Kreiss
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adam Kreiss
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic