AnsweredAssumed Answered

Adding custom Java Objects in Freemarker Model

Question asked by cedric_lamalle on Oct 27, 2016
Latest reply on Oct 29, 2016 by cedric_lamalle

Hello,

I'm creating a custom form control and to render it I need to add custom java objects in the FreeMarker Model. From the documentation at Template models | Alfresco Documentation  it seems to be possible to add this objects from a Spring Configuration. I've tried to search through Alfresco Source Code, and found some example of how the default model is added to Freemarker in repository/config/alfresco/template-services-context.xml.

So in my repo project I added:

 

<bean id="jsonToModelTemplateExtension" parent="baseTemplateImplementation" class="x.x.x.x.JsonTemplateExtension">
   <property name="extensionName">
   <value>jsonToModel</value>
   </property>
</bean>

 

The java code is:

 

public class JsonTemplateExtension extends BaseTemplateProcessorExtension implements TemplateMethodModelEx {

   private static final Log LOG = LogFactory.getLog(JsonTemplateExtension.class);

   @Override
   public Object exec(List arguments) throws TemplateModelException {
   LOG.warn(arguments);
   return "Hello World!";
  }
}

 

Then in my share project I've tried to use the Template Model Method like this:

 

<#assign mytext = jsonToModel('Teste')>
${mytext}<br/>

 

And I'm getting this error:

 

Caused by: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> jsonToModel  [in template "org/alfresco/components/form/controls/conteudoSolicitacao.ftl" at line 4, column 26]

 

Any idea of what I am doing wrong?

 

Thanks,

Cedric.

Outcomes