Re: Another Idea/Question
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Monday, November 17, 2008, 5:04:13 PM, Luther Baker wrote:
> Thanks so much for your extended answers Daniel. They are really helpful.
>
> So - I have a mechanism that allows a user to define *.ftl files
> that I will load at web application startup. The user does this
> using init parameters in the web.xml.
>
> Now, I'd like the user to be able to ... for instance, make a
> simple Constants instance (or static class) available for use on
> every page. I started to think I should add another parameter to the
> web.xml file that include fully qualified classes - of which I would
> instantiate for the user and place into a shared variable.
>
> But - before trying that, I thought I'd try the <#assign directive ...
>
> So, in one of the *.ftl files (I, as the user, have defined via
> web.xml) I am creating this assign directive:
>
> <#assign constants="com.company.app.web.components.Constants"?new()/>
That's not allowed for security reasons. The ?new built-in can only
instantiate objects whose class implements TemplateModel. By default
at least; AFAIR it was configurable somehow. So if you *really* want
to allow people doing this in the template, then you could just write
a TemplateMethodModel and expose it just like your other
framework-specific stuff. Like, you add a method called "getConstants"
as shared variable (or as part of a #import-able FTL), and then users
can do <#assign constants = getConstants("com.example.Constants")>.
Or... you can do the same with a TemplateHashModel, lets call it
"consts", and then it's used like <#assign constants =
consts["com.example.Constants"]>. And so on. But if you know already
what constants will be needed, then of course you shouldn't want users
to do these #assignement, rather your framework should automatically
populate the data-model with the required constants. (And of course
feel free to use Java Reflection for that.)
> and I get the following when I process a template ...
>
>
> 6657 [12031745@qtp0-1] ERROR freemarker.runtime -
> Class com.company.app.web.components.Constants does not implement freemarker.template.TemplateModel
> The problematic instruction:
> ----------
==>> assignment: constants="com.company.app.web.components.Constants"?new() [on line 2, column 1 in Global.ftl]
> ----------
>
> Java backtrace for programmers:
> ----------
> freemarker.template.TemplateException: Class
> com.company.app.web.components.Constants does not implement freemarker.template.TemplateModel
>
>
>
> does that help? I know I can put POJOS in the root map via java,
> but to clarify, I guess I'm looking for the correct way to expose a
> POJO via an ftl .... my POJOs via #assign don't work.
It's not assign that fails, but ?new, and even that only for security
reasons.
> Similar to the way a JSP can import packages or classes which make
> certain objects visible to the page.
You are not supposed to do low-level technical stuff in the templates.
That's the duty of the Java code that prepares the template execution.
> ... I think :)
>
> -Luther
--
Best regards,
Daniel Dekany
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/