Discussion:
[FreeMarker-user] Equivalent of jsp:useBean in FTL
iogbole
2013-09-02 20:54:25 UTC
Permalink
Hello,

I'm a newbie in FTL and I was wondering if it's possible to use javabean in
FTL like it's done in JSP:

For example: Given this JavaBean:

/public class FTLquestion {

public String sayHello(){
return "Hello"
}
}
/

In JSP, I will call this bean like:

/<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<jsp:useBean id="ftlq" class="com.ftl.helloworld.FTLquestion"
scope="request"/>
<HTML>
....
${ftlq.sayHello}
....
</HTML>/

So my question is it possible to create javabean handler in Freemarker as
demonstrate with JSP above? Please suggest/help




--
View this message in context: http://freemarker.624813.n4.nabble.com/Equivalent-of-jsp-useBean-in-FTL-tp4654740.html
Sent from the freemarker-user mailing list archive at Nabble.com.
iogbole
2013-09-02 20:29:53 UTC
Permalink
Hello,

I'm a newbie in FTL and I was wondering if it's possible to use javabean in
FTL like it's done in JSP:

For example: Given this JavaBean:

/public class FTLquestion {

public String sayHello(){
return "Hello"
}
}/

In JSP, I will call this bean like:

/<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<jsp:useBean id="ftlq" class="com.ftl.helloworld.FTLquestion"
scope="request"/>
<HTML>
....
${ftlq.sayHello}
....
</HTML>/

So my question is it possible to create javabean handler in Freemarker as
demonstrate with JSP above? Please suggest/help





--
View this message in context: http://freemarker.624813.n4.nabble.com/Equivalent-of-jsp-useBean-in-FTL-tp4654739.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-09-03 21:06:27 UTC
Permalink
Post by iogbole
Hello,
I'm a newbie in FTL and I was wondering if it's possible to use javabean in
/public class FTLquestion {
public String sayHello(){
return "Hello"
}
}/
<jsp:useBean id="ftlq" class="com.ftl.helloworld.FTLquestion"
scope="request"/>>
Post by iogbole
<HTML>
....
${ftlq.sayHello}
....
</HTML>/
So my question is it possible to create javabean handler in Freemarker as
demonstrate with JSP above? Please suggest/help
So then, is it the question if you can create arbitrary new beans from
an FTL? You can easily implement a TemplateModel that creates other
objects, and put that TemplateModel into the data-model or into the
Configuration as shared variable, or into an #import-ed FTL (via
<#assign myFactory = "com.example.MyFactory"?new()>). So you can make
whatever sophisticated object factory available for the templates. (An
example of a such factory TemplateModel is the horrible legacy,
freemarker.template.utility.ObjectConstructor. See its JavaDoc.)

Also, out of the box you can do something like <#assign ftlq =
"com.example.FTLquestion"?new()> and then ${ftlq.sayHello()}, but
there's a restriction that FTLquestion must implement TemplateModel.
Also it's might not be as concise as you want it to be.

But, why's this needed anyway? It's not an usual patten in MVC
applications. If you really just need a "pulled" data-model instead of
a pushed one, consider that the data-model can fill itself on demand,
if you implement a such TemplateHashModel and use it as the
data-model.
--
Thanks,
Daniel Dekany
Loading...