• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing a Map with Struts

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic