Reputation: 8416
I can access a HashMap<String, Object>
easily enough in JSTL but is it possible to access a HashMap<Object, Object>
I only ask because I don't receive any errors (or output) when trying the following:
${myHashMap[anObject]}
It leads me to believe that myHashMap is trying to find my value but it is somehow not evaluating anObject
as the correct key. I can verify that myHashMap has anObject
as a key with a (non-blank/non-null) value that should display.
Upvotes: 2
Views: 1456
Reputation: 1108742
This syntax ought indeed to work. I understand that you didn't get any value by the given object as key although you're confident that the desired object is in there? In that case, the class as represented behind ${anObject}
in your code must have the equals()
(and hashCode()
) methods properly implemented. The Map#get()
namely tests the key by equals()
method. See also the javadoc:
Returns the value to which the specified key is mapped, or
null
if this map contains no mapping for the key.More formally, if this map contains a mapping from a key k to a value v such that
(key==null ? k==null : key.equals(k))
, then this method returns v; otherwise it returnsnull
. (There can be at most one such mapping.)
In other words, if the equals()
of your ${anObject}
returns true
for the map key, then the associated map value will be returned, otherwise null
will be returned and EL will then print nothing.
That it works for String
is simply because that class has the equals()
already properly implemented.
Upvotes: 3