cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing HttpServletRequest from inside freemarker template

santoso_n
Champ in-the-making
Champ in-the-making
Hi all,

I'm developing a custom dashlet and I need to access the current session Id, to do that I need to access HttpServletRequest.

Anybody know how to access it from inside freemarker template?

in JSF, I might try to access the session id using

#{facesContext.externalContext.request.requestedSessionId}


Thanks in advance,

Santoso
8 REPLIES 8

santoso_n
Champ in-the-making
Champ in-the-making
Currently I managed to find my way out, that is to pass my custom model to the template

<r:template template="/alfresco/templates/mydashlet.html.ftl" model="#{mycustom.templateModel}"/>

Inside my custom model, I retrieve the session id value and store it in the map (e.g. sessionId), then in my ftl file I access the sessionId.

Anybody else know a better way to access the current session id in the template?


santoso

mrogers
Star Contributor
Star Contributor
Your solution has a lot going for it.

If you are following the MVC pattern (which is a good idea) then freemarker is the view tier and should not contain control logic.   This becomes clear when you consider cases where when you want to have different representations of your model,  lets say for example you want a version of your custom dashlet that renders xml rather than html or has other view tier features.

santoso_n
Champ in-the-making
Champ in-the-making
Actually there is no control logic in my freemarker template, maybe I'm not stating it clearly.
I just need to pass the session id to another application which in turn will call my webscript later.

Could you explain more on having a different presentation for my model that might fit my need?

Thanks

t_broyer
Champ in-the-making
Champ in-the-making
Hi all,

I'm developing a custom dashlet and I need to access the current session Id, to do that I need to access HttpServletRequest.

Anybody know how to access it from inside freemarker template?

How about creating a TemplateProcessorExtension? say:

${santoso.sessionId}

santoso_n
Champ in-the-making
Champ in-the-making
Thanks, TemplateProcessorExtension seems to be a great idea.
However I couldn't find the documentation of it, could you pass me the link to the documentation?

t_broyer
Champ in-the-making
Champ in-the-making
Thanks, TemplateProcessorExtension seems to be a great idea.
However I couldn't find the documentation of it, could you pass me the link to the documentation?

I cannot find any doc on the wiki (I thought there was but am unable to find it back), so…

Here's the JavaDoc:
http://dev.alfresco.com/resource/docs/java/repository/org/alfresco/service/cmr/repository/TemplatePr...
http://dev.alfresco.com/resource/docs/java/repository/org/alfresco/repo/template/BaseTemplateProcess...

Inherit the BaseTemplateProcessorExtension and configure a bean in a Spring context file. For examples or such configuration, search for one of the classes inheriting BaseTemplateProcessorExtension (DateCompareMethod or Session for example) in the WEB-INF/classes/alfresco/*-context.xml.
For an example of how to code such an extension, search for the same classes' source code in the Alfresco SDK or the public SVN.

santoso_n
Champ in-the-making
Champ in-the-making
Thanks a lot for the info, I will try it Smiley Happy

veronika_zenz
Champ in-the-making
Champ in-the-making
I just had the same requirement of adding additional functionality to freemarker templates by adding a new root element.

Using this Forum and the template guide wiki page, section custom root objects, this worked out fine.

In short what I did to have a new root objects with custom functionality in freemarker, with some example code:
1) Create a java class that extends BaseTemplateProcessorExtension and defines the methods I want to access from freemarker templates

public class BibtexTemplate extends BaseTemplateProcessorExtension
{

   private BibtexService bibtexService;
   public String NodeToBibtexString(String nodeId)
   {
      StoreRef storeRef = new StoreRef("workspace://SpacesStore");
      NodeRef nodeRef = new NodeRef(storeRef, nodeId);   
      BibtexEntry entry = bibtexService.NodeToBibtex(nodeRef);
      return BibtexUtil.BibtexToString(entry);
   }
   

   
   public void setBibtexService(BibtexService bibtexService)
   {
      this.bibtexService = bibtexService;
   }
   
}
2) Spring configuration (file "[whatever-you-like]-context.xml")

<bean id="bibtexTemplateExtension" parent="baseTemplateImplementation" class="com.matrixware.irss.bibtex.BibtexTemplate">
       <property name="extensionName">
           <value>bibtexTempl</value>
       </property>
      <property name="bibtexService">
         <ref bean ="BibtexService"/>
     </property>
   </bean>
3) Use the new root object in freemarker template

<#list rows as row>
${bibtexTempl.NodeToBibtexString(row.nodeId)}
</#list>