Re: Weird stuff in hashmap

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, June 4, 2010, 5:12:53 PM, gz1 wrote:

>
> Hello,
>
> I'm trying to iterate over my hashmap like this:
>
> (first populate the map in Java):
>
> HashMap<String,String> foo = new HashMap<String,String>();
> foo.put("hey","you");
> foo.put("one","two");
> root.put("stuff", foo); // map passed to template.process()
>
> Now access it in ftl:
>
>                         <#list stuff?keys as key>
>                                 <h3>${key}</h3>
>                         </#list>
>
> I get stuff like this in my output:
>
> put
> remove
> entrySet
> hashCode
> productElement
> clear
> isEmpty
> hey
> values
> empty
> underlying
> productElements
> copy
> getClass
> get
> copy$default$1
> equals
> productPrefix
> class
> canEqual
> keySet
> size
> one
> containsKey
> productArity
> containsValue
> productIterator
> toString
> putAll
>
> What's all this crap?

The ObjectWrapper, which is most certainly a BeansWrapper instance in
your case, was configured incompletely, and thus the methods are the
items of the Map as well (like they are of all POJO-s). You have to
call yourObjectWrapper.setSimpleMapWrapper(true) where you set up
FreeMarker (see also:
freemarker.template.Configuration.set/getObjectWrapper). Note that
this way you won't be able to call the methods of Map-s. Either this
or that...

> My "hey" and "one" are there--and work fine if I
> access them like ${stuff["hey"]}
>
> But I want to iterate over all my keys and don't want all this other stuff,
> which looks like introspected stuff from HashMap.
>
> Any ideas appreciated.  Thanks!
> Greg

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.