cancel
Showing results for 
Search instead for 
Did you mean: 

Help with rendering ftl from java backed webscript

jamesc5
Champ in-the-making
Champ in-the-making
I'm writing a java backed webscript and need to know the correct way to pass objects to a freemarker template for rendering. Below is my simplified code.

Webscript

public class SampleWebScript extends AbstractWebScript
{

  public void execute(WebScriptRequest req, WebScriptResponse res)
      throws IOException
  {
    try
    {
      Container c = getContainer();
      Writer writer = res.getWriter();

      String bar = "Hello World";
      Object model = new SampleModel(bar);

      String templatePath = "org/alfresco/demo/foo.ftl";
      if(c.getTemplateProcessor().hasTemplate(templatePath))
      {
        c.getTemplateProcessor().process(templatePath,model,writer);
      }

    }

    catch (Exception e)
    {
      throw new WebScriptException(e.getMessage());
    }
  }

private class SampleModel
  {
    private String message;

    private SampleModel(String s)
    {
      this.message = s;
    }

    public String getMessage()
    {
      return message;
    }

    public void setMessage(String message)
    {
      this.message = message;
    }
  }
}


Freemarker Template

<?xml version="1.0" encoding="UTF-8"?>

<workflow-definitions>
   <message>${message}</message>
</workflow-definitions>

I think the issue is with "model" on this line:

c.getTemplateProcessor().process(templatePath,model,writer);

When I hit this page in my browser I get this exception:

Error during processing of the template 'Expression message is undefined on line 4, column 20 in org/alfresco/demo/foo.ftl.'. Please contact your system administrator.
1 REPLY 1

jamesc5
Champ in-the-making
Champ in-the-making
I figured it out. I was over thinking and didn't see the renderTemplate method on AbstractWebScript.

In order to render a freemarker template from a java backed webscript you need:

1. The path to your freemarker template, relative to Data Dictionary/Web Scripts Extensions/
String templatePath = "org/alfresco/demo/foo.ftl";

2. A Map of objects to pass to your template
       The code in my last post attempts, incorrectly, to pass objects to the template by creating a new arbitrary class "SampleModel" with one property and passing it as the model argument. The correct way to do what I was attempting to do in my last post is:

Map<String, Object> model = new HashMap<String, Object>();
model.put("message", "Hello World");

3. The Writer from WebscriptResponse
You get the writer from the response object passed to your webscript

Writer writer = res.getWriter();

The whole thing looks like this:

public class SampleWebScript extends AbstractWebScript {
 
  public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
     
      String templatePath = "org/alfresco/demo/foo.ftl";

      Map<String, Object> model = new HashMap<String, Object>();
      model.put("message", "Hello World");

      Writer writer = res.getWriter();
     
      renderTemplate(templatePath,model,writer);
  }

}

Pretty simple, once understood  :wink: