cancel
Showing results for 
Search instead for 
Did you mean: 

Access Java Class with Freemarker Template

major_king
Champ in-the-making
Champ in-the-making
Hey everybody,

is there a possibility to run Java code from an Freemarker template? Does anybody has an example or tutorial for this or is there another workaround to access java classes?

Regards,
Christian
12 REPLIES 12

samuel_penn
Champ in-the-making
Champ in-the-making
Not sure about Freemarker (I haven't used it very much), but it's definitely possible to have Java code run as XSLT extensions if you're using XSLT as the templating language. At least, I've had in working with web forms.

Sam.

nancyg
Champ in-the-making
Champ in-the-making
Freemarker uses the MVC (Model View Controller) pattern, you can use Java to prepare the data and then use Freemarker to display it. You can read more at the Freemarker site, freemarker.org.

Nancy

major_king
Champ in-the-making
Champ in-the-making
Hello,

thanks for fast answering. Now I spend some time on browsing some forums and guids and i found out the Freemarker Beanwrapper. With this it should be possible to call Java-code. Has anybody already done this? I'm struggling with the syntax atm Smiley Sad

Thanks for help!
Christian

poptarts
Champ in-the-making
Champ in-the-making
what helped me the most when trying to wrap my head around the issue was to find somethign that Alfresco already does and try to replicate it. For instance I needed to put a new item on the Sidebar.

So I looked at the existing Sidebar jsps, which are navigator, shelf and opensearch.jsp. Now look at examples under the dashlets jsps, some of which call templates for their rendering.

For me it was more helpful to use the existing stuff and modifying it to see what it would do, than try to read a wiki page to understand it all.

hope that helps a little.

nyronian
Champ in-the-making
Champ in-the-making
It is really quite powerfull and easy to do once  you have it setup.

There are a few items you must be aware of.  Here's how I did it.

* Create the javabean according to java standards.
* Add the javabean to the freemarker model.  I did this by creating my own servlet the extended GuestTemplateContentServlet and extending the buildModel method.  This method is what is used by alfresco add the alfresco java beans to the root model. 
* I then added my own javabeans that did whatever I needed them to.

So for example you had a Javabean with a getTotal() method and you added it to the HashMap with an id of "calc".  In the template you would simply say:  ${calc.total} once the javabean is added to the root Hashmap.  Take a look at the org.alfresco.web.app.servlet.GuestTemplateContentServlet.  Turn on it's debugging in you wish and you will see how it works.

The total method can return most any java.lang data type as well as List or Map interfaces.

nyronian
Champ in-the-making
Champ in-the-making
Samual….how do you do this in XSLT.  I researched this about a year ago and found NOTHING on it.  I posted on the Alfresco forums before and did not get many answers of how to expose java objects to the XSLT engine, so at the time I went all Freemarker because it talks about it natively.

http://forums.alfresco.com/viewtopic.php?f=3&t=6340

Can you point me to the documentation on how to do this in XSLT?

samuel_penn
Champ in-the-making
Champ in-the-making
The problem I had was trying to get the XSLT to pick up the Java class. In the end, the only way I found to do that was to stick the class in the Tomcat shared classes folder - placing the class in the web project itself (even if in a WEB-INF) didn't work. Once that has been done, then it's just a standard Xalan extension, which is documented on the Apache Xalan site.

I've got a simple example on my laptop which I'll try and dig out and put up later today.

Sam.

samuel_penn
Champ in-the-making
Champ in-the-making
I think this version of things works. It just uses a Java class to perform a simple arithmetical operation based on the form data (what I actually needed to do was display logs and square roots of some data, which XSLT doesn't do easily, but my implementation of that has been put on hold) to prove that the concept worked.

http://www.glendale.org.uk/~sam/alfresco/plugin.xsd
http://www.glendale.org.uk/~sam/alfresco/plugin.xsl
http://www.glendale.org.uk/~sam/alfresco/PluginTest.java

I haven't had a chance to test these today, but I think they're the versions which worked. At the very least, it should give an idea of what you're supposed to do (the Java class can be run outside Alfresco to test the XSLT side). The class for PluginTest needs to be placed into the Tomcat classpath, and the xsd/xsl used to define a web form.

Sam.

major_king
Champ in-the-making
Champ in-the-making
Thanks a lot for all these answers. I will try them out Smiley Very Happy

Regards,
christian