Monday, November 24, 2008

Passing values to your freemarker template in alfresco

This story started when my teammate needed the value of JSESSIONID from alfresco and bla bla bla... So, in short, this is how I managed to pass the session id.

  1. Create a simple model (javabean class) to pass the value to the template, in the class create a method to return a Map containing the values needed, the keys used to store the values are the keys that will be accessed in the template.

  2. public Map getTemplateModel() {
    Map result = new HashMap();

    result.put("sessionId", getSessionId());

    return result;
    }

  3. Register the class as a managed bean in WEB-INF/faces-config-custom.xml (pasting the content here is a bit difficult, until I know how to easily paste an xml content to the post I might not post it). Actually it's not that difficult to register a managed bean, just look at the sample in faces-config-beans.xml.
  4. <managed-bean>
    <description>
    Helper bean providing access to http servlet request's related properties
    and many other things.
    </description>
    <managed-bean-name>SanzModel</managed-bean-name>
    <managed-bean-class>com.sanz.model.TemplateModel</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
  5. Use the bean name as the model passed to the template.
  6. <r:template template="/alfresco/templates/elnusa/elnusa.html.ftl" model="#{Elnusa.templateModel}"/>
  7. Access the value in the template, just like the way default values provided by alfresco are accessed.

That's all, I guess I have to sleep now, already late...


Reference :
  1. Template Guide

1 comment:

Unknown said...

Hi Santoso,

I am new to the alfresco,me using Alfresco 2.1 version. I read u r documentation about How to create a custom dashlet in Alfresco and i tried it i got successfully.

Now i want to shoe some data on the Dashlets.But i don't know how.
Can you give me a tip please.