Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed0.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Chris Riesbeck Newsgroups: comp.lang.java.programmer Subject: Re: JSTL: getting a map's keys Date: Tue, 20 Mar 2012 12:19:42 -0500 Lines: 51 Message-ID: <9srshfFjo5U1@mid.individual.net> References: <9spbepF1baU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net OoOpzm8DZQJMv8Brwsa05gZZsXnuGb8TLljY43BJbih2wZQW3n Cancel-Lock: sha1:lkU46EFcu+STRF91W3netHE8y0Y= User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:13080 On 3/19/2012 6:59 PM, Daniel Pitts wrote: > 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? >> >> >> >>

rates: ${rates}

>> >>

jstl: ${rates.keys}

>> >>

jsp: <%= ((edu.northwestern.ils.simulator.SimTable) >> pageContext.findAttribute("rates")).getKeys() %>

>> >> Here's the generated HTML output: >> >>

rates: {Bill=100.0, Fred=75.0, Jane=75.0, Mary=50.0, Sam=75.0, >> Sarah=150.0}

>> >>

jstl:

>> >>

jsp: [Bill, Fred, Jane, Mary, Sam, Sarah]

>> > 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. Ah! This would explain why ${rates.class} doesn't work either. Basically, name.key will always be taken as name[key] for a Map, even if key is a property of name. If I'm reading the JSP EL resolver specification correctly, that's just what is happening. > > You *can* use c:forEach to iterate over the entry set (Map.Entry) in a map. Yes, that's what I've been doing instead for looping. > > Hopefully this helps, > Daniel. Indeed. Thanks, Daniel