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

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

for all developers who will have the same problem, i will post my results Smiley Happy

I does not get it work to access the Java class with Freemarker Smiley Sad Now i call my Java-Code directly from the jsp-page via JavaServer Faces. For more Information send me a PM.

Regards,
Christian

hansraj
Champ in-the-making
Champ in-the-making
Hi Major,

iam trying to write Java backed Restful API.

i followed the following steps:
1). helloworld.get.desc.xml
2). HelloWorld.java
3). helloworld-scripts-context.xml
4). helloworld.get.html.ftl
5). created HelloWorld.class jar file and placed in
WEB-INF\lib
6). copied helloworld-scripts-context.xml in
shared\classes\alfresco\extension
7). removed alfresco.war
8). restarted Tomcat server
9). created helloworld.get.desc.xml & helloworld.get.html.ftl in DataDictionary/WebScripts-extension space
10). run the Restful API like - http://localhost:8080/alfresco/service/helloworld?name={nameArgument}
11). System.out.print(…) in java file are not getting printed on Tomcat console..
I guess java class is not at all getting invoked. could any body pls answer how to link Java class and FreeMarker Template?

The following are the source files:

1). helloworld.get.dec.xml
<webscript> 
   <shortname> Hello World </shortname>
   <description> Hello World Web Script </description>
   <url> /helloworld?name={nameArgument} </url>
</webscript>


2). HelloWorld.java

package com.helloworld.scripts;

import java.util.Map;
import java.util.HashMap;
import org.alfresco.web.scripts.DeclarativeWebScript;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptStatus;

public class HelloWorld extends org.alfresco.web.scripts.DeclarativeWebScript
{
   String nm="";
   protected Map<String, Object> executeImpl(WebScriptRequest wr, WebScriptStatus ws){
      System.out.println("from executeImpl()****************");
      System.out.println("hello****************");
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("foo", nm);
      return model;
   }

   public void setSomeName(String name){
      nm=name;
   }

}


3). helloworld-scripts-context.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans-2.0.dtd'>

<beans>
   <bean id="webscript.com.scripts.helloworld.get" class="com.helloworld.scripts.HelloWorld" parent="webscript">
      <property name="someName">
         <value> This is a Foo Name </value>
      </property>

   </bean>
</beans>


4). helloworld.get.html.ftl

<html>
   <body>
      <p>   Hello, ${args.name} </p>
         Foo : ${foo}
   </body>
</html>

Regards,
Hans

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

Can you please post your custom GuestTemplateContentServlet.buildModel method code.
I' m haveing the same problem. I want to be able to access pojo's from freemarker in Alfresco.