• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting values from a hashmap using JSTL

 
Bai Shen
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I used to have the following code.



But since it's a properties object, which is backed by a hashmap, I can't be assured of the order. So I made a sorted array of the keys. However, I can't see to get the values from the properties object. Everything else is working fine.



I've tried multiple permutations, but I keep getting the following error. "The function value must be used with a prefix when a default namespace is not specified"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To ensure order, I'd copy the properties into a LinkedHashMap which will retain the insertion order. Then you can simply iterate over the map, using the .key and .value properties of the Map.Entry items.
 
Bai Shen
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:To ensure order, I'd copy the properties into a LinkedHashMap which will retain the insertion order. Then you can simply iterate over the map, using the .key and .value properties of the Map.Entry items.


Is there a reason my approach won't work? I was able to get a value by replacing the key object with a constant string.



But the following doesn't work.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My way is cleaner as it involves one collection rather than two. Rule #1 of JSP: "keep it simple".

(OK, that's really Rule #2 -- Rule #1 is "no scriptlets!" )

Bai Shen wrote:But the following doesn't work.

What's props in this case? The Map? If so, props[key] (with a valid value for key) will evaluate to a Map.Entry instance. So props[key].value will evaluate to the entry's value.
 
Bai Shen
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:My way is cleaner as it involves one collection rather than two. Rule #1 of JSP: "keep it simple".

(OK, that's really Rule #2 -- Rule #1 is "no scriptlets!" )


The reason I asked is because I already have an enum of the property keys.


Bai Shen wrote:But the following doesn't work.

What's props in this case? The Map? If so, props[key] (with a valid value for key) will evaluate to a Map.Entry instance. So props[key].value will evaluate to the entry's value.


props is the property object. See the first example in the OP. And adding .value doesn't work. What I don't understand is why using a string constant works, but using my key variable doesn't. The key variable works in the title display, so I know it's not null and has the correct value.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bai Shen wrote:props is the property object.

Not specific enough. What class? java.util.Properties?
 
Bai Shen
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Bai Shen wrote:props is the property object.

Not specific enough. What class? java.util.Properties?


Sorry. Yes.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Properties is extended from Hashtable. This is one of the older of the Java collection classes from prior to Java 1.2. As such, it is unfriendly and cantankerous and isn't as easy to deal with on a JSP as newer classes (such as anything derived from java.util.Map).

I strongly advise stopping banging your head against the wall and copy the data into a LinkedHashMap or other suitable collection prior to sending it off to the JSP page. You page code will be simpler, and your head will hurt less.
 
Bai Shen
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:java.util.Properties is extended from Hashtable. This is one of the older of the Java collection classes from prior to Java 1.2. As such, it is unfriendly and cantankerous and isn't as easy to deal with on a JSP as newer classes (such as anything derived from java.util.Map).

I strongly advise stopping banging your head against the wall and copy the data into a LinkedHashMap or other suitable collection prior to sending it off to the JSP page. You page code will be simpler, and your head will hurt less.


-chuckle- Okay.-grin-

Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic