This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Getting values from a hashmap using JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Getting values from a hashmap using JSTL" Watch "Getting values from a hashmap using JSTL" New topic
Author

Getting values from a hashmap using JSTL

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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

Joined: Jan 10, 2002
Posts: 61314
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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

Joined: Jan 10, 2002
Posts: 61314
    
  66

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

Joined: Sep 24, 2008
Posts: 323
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

Joined: Jan 10, 2002
Posts: 61314
    
  66

Bai Shen wrote:props is the property object.

Not specific enough. What class? java.util.Properties?
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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

Joined: Jan 10, 2002
Posts: 61314
    
  66

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

Joined: Sep 24, 2008
Posts: 323
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.
 
GeeCON Prague 2014
 
subject: Getting values from a hashmap using JSTL