RE: Which classes are available for staticUtil in Freemarker Templates?

thumbnail
Dominik Marks, modified 6 Years ago. Regular Member Posts: 149 Join Date: 8/29/12 Recent Posts

In our project we use staticUtil to access static classes and call methods inside Freemarker Templates (e.g. Web Content Templates) like this

<#assign 
    textFormatter = staticUtil["com.liferay.portal.kernel.util.TextFormatter"]
    sizeFormatted = textFormatter.formatStorageSize(file.size, locale) 
/>

However it seems that not all classes are available for staticUtil. For example I tried to access javax.imageio.ImageIO and got a ClassNotFoundException

Is there any list which classes are available here? Internally it seems that Class.forName() is called. So which Classloader is used here??

From some tests I found that the following classes are available:

  • java.util.Calendar
  • java.util.regex.Pattern
  • com.liferay.portal.kernel.service.ServiceContextThreadLocal
  • com.liferay.portal.kernel.util.TextFormatter

The following classes throw a ClassNotFoundException

  • javax.image.ImageIO
  • com.liferay.portal.kernel.image.ImageToolUtil

Thank's for your help.

Rayappa Hattarwat, modified 6 Years ago. Junior Member Posts: 89 Join Date: 8/11/15 Recent Posts

Hi Dominik,

You can access web content template in free marker bellow code

 

<#assign article = renderer.getArticle() />
<#assign className = renderer.getClassName() />
<#if className == "com.liferay.journal.model.JournalArticle">

<!---

 enter your logic here

-->

</#if>

 

Regards,

Rayappa

thumbnail
Dominik Marks, modified 6 Years ago. Regular Member Posts: 149 Join Date: 8/29/12 Recent Posts

Thanks Rayapp for your answer. 

 

However I specifically asked about the possibilities for staticUtil and which classes are avaible for this. Do you know which classes are available here?

SEBASTIEN PAYEN, modified 5 Years ago. New Member Posts: 7 Join Date: 10/29/19 Recent Posts
Hello Dominik,I have the same problem.Did you find an aswer to your initial question?
Thanks.
thumbnail
Dominik Marks, modified 5 Years ago. Regular Member Posts: 149 Join Date: 8/29/12 Recent Posts
Sorry Sebastien, no I did not find an answer to my initial question yet.
thumbnail
Christoph Rabel, modified 5 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
Hmm.
AFAIKT it is the classloader of the current thread. See getBeansWrapper method in FreeMarkerManager.
https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-template/portal-template-freemarker/src/main/java/com/liferay/portal/template/freemarker/internal/FreeMarkerManager.java
I am not sure what that means though, practically speaking. I guess, the classloader of the current portlet is used. Of course, something could manipulate the thread classloader before calling that method and switch it elsewhere.