Re: How to process a bean and hash at the same time?

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Saturday, May 15, 2010, 5:23:30 AM, ipolevoy wrote:

> Hello, I'm working on a framework and considering using Freemarker
> for UI layer. In order to achieve this, I will need the engine to
> recognize objects as beans and maps at the same time. In other words, I would have a class:
> public class Person extends HashMap {
>     private String name;
>     public Person(String name) {name = name;}
>     public String getName() {return name;}
> }
> Merging code: 
> Map root = new HashMap();
> Person p = new Person("John");
> p.put("age", 39);
> root.put("da_bean", p);
> template.process(root, out);
> ...and the template: 
> Map: ${da_bean.age}
> <br>
> Bean: ${da_bean.name}
> The class Person is a Map and is also a bean (has a "name"
> property). I looked at the documentation, API and source code, and
> it seems that I will have to write a custom wrapper and a template
> model, but without a good understanding of the internal
> architecture, this might be a whole new project.

I believe you have to write a custom wrapper (but then I might don't
know BeansWrapper enough), but that's considered to be normal in
FreeMarker. The whole point of object wrappers is that framework
authors (and maybe application authors) can write their own wrappers.
I'm not saying it's the easiest thing on the world, but if you are a
frameworks author, you often have to dig deeper than an application
programmer, so... Try to extend or copy-and-patch BeansWrapper (and
the related classes).

> Is there a way to achieve this easier? Am I going in the wrong direction?
> Thanks much for all the help! 
> igor 
> View this message in context: How to process a bean and hash at the same time?
> Sent from the freemarker-user mailing list archive at Nabble.com.

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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.