Equivalent of jsp:useBean in FTL

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Equivalent of jsp:useBean in FTL

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

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Equivalent of jsp:useBean in FTL

Daniel Dekany
Monday, September 2, 2013, 10:29:53 PM, iogbole wrote:

> 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

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


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user