Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Which classes are available for staticUtil in Freemarker Templates?
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.
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
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?
Thanks.
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.
Powered by Liferay™