Re: Can't get hashes to work
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, June 17, 2009, 9:31:37 PM, Brian Pontarelli wrote:
> I went back through our previous thread and found that using the
> ${bean.coolMap(key)} syntax works. This is systemic based on how FM handles the [] notation right?
The original problem is that, as I said, someHash[key] expects a
*string* as key. Because, the hash type of FTL, by definition, maps
*string* keys to arbitrary values. It's not the same as Java's Map.
(Note that to further complicate the matter, in FTL
someSequenceOrString[index] expects an integer index. So, the [] thing
is used for that too.) Now someBeanWrappedMap(key) has technically
nothing to do with all the []-s, it's just a method call, so it
accepts all kind of keys. If you have a Map with non-string keys, you
must use that.
> -bp
>
>
>
> On Wed, Jun 17, 2009 at 1:19 PM, Brian Pontarelli <[email protected]> wrote:
> So, I have been working on converting everything over to use
> BeansWrapper, but now I can't seem to get my hashes to work at all,
> even with DefaultObjectWrapper. I'm assuming this is something
> simple, but I can't figure it out. Here is my simple bean:
>
> public class Bean {
> public Map<Integer, String> coolMap = new HashMap<Integer, String>();
> private int age;
>
> public int getAge() {
> return age;
> }
>
> public void setAge(int age) {
> this.age = age;
> }
> }
>
>
> Here's the code that I'm using to execute the template:
>
> BeansWrapper ow = new BeansWrapper();
> ow.setExposeFields(true);
> ow.setSimpleMapWrapper(true);
>
> Bean bean = new Bean();
> bean.coolMap.put(1, "test");
> bean.setAge(42);
>
> Map<String, Object> context = new HashMap<String, Object>();
> context.put("bean", bean);
>
> Template template =
> freeMarkerConfiguration.getTemplate(templateName, locale);
> template.setObjectWrapper(ow);
> template.process(root, writer);
>
>
> The template is pretty simple:
>
> [#ftl/]
> Bean [#list bean.coolMap?keys as key]${key} ${bean.coolMap[key]} [/#list] ${bean.age}
>
>
> The error I'm getting is just the standard "Expected number,
> sequence, or string. bean.coolMap evaluated instead to
> freemarker.ext.beans.SimpleMapModel". It is failing at the
> ${bean.coolMap[key]} statement. However, that should work fine, right?
>
> -bp
>
>
>
>
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects