Community Wiki

Print Properties
Access to Liferay services in Velocity

In your themes you sometimes want to invoke Liferay services. Liferay provides a utility to obtain references to services; $serviceLocator.

FindService

For example, suppose you need layoutLocalService:

#set($layoutLocalService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))

#set($xyz = $layoutLocalService.getFriendlyURLLayout($layout.getOwnerId(), "/home")) 


The services obtained from the findService method are the raw services and sometimes they can be difficult to work with because Velocity does not provide an exception handling mechanism.


FindExceptionSafeService

To handle this issue, more recent versions of Liferay (5.1.0+) provide a second method, findExceptionSafeService which wraps the underlying service with a proxy which catches any exceptions and simply returns null.

For example, suppose you want to see if some userId refers to an actual user:

#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService"))
The following method would typically crash the VM being processed, because it throws a NoSuchUserException if the user cannot be located.
#set($user = $userLocalService.getUserById($getterUtil.getLong("12345"))


Service Locator doesn't work for me???

ServiceLocator is a feature which unlocks a lot of power. This power can inadvertently fall into the wrong hands if for example Journal Template creation is granted to less trusted users. Therefore to prevent abuse, or simply to protect less aware portal admins, the feature is disabled by default for all Journal VM contexts.

To enable it, edit the value of journal.template.velocity.restricted.variables in portal.properties. See Journal Portlet section of the Portal Properties area.


6361 Views, 0 Attachments 0 Attachments

  • Comments
Threaded Replies Author Date
Doesn't work for me in 5.2.2 My... Thomas Kellerer 4/14/09 3:49 AM
I haven't gotten this to work either, but then... Dave Mosher 6/22/09 11:41 AM

Doesn't work for me in 5.2.2

My portal-ext.properties contains the following line:

journal.template.velocity.restricted.variables=

But still I can't use the service inside a Velocity template.

But I assume this is more caused by the fact that the $layout variable is not working in my template. If I simply put $layout.getOwnerId() into my VM template, it does not retrieve the ID, but displays that text literally (which usually means that the Variable is not defined for Liferay)

Posted on 4/14/09 3:49 AM.

Top Top
I haven't gotten this to work either, but then again I don't have server-side access. Yet.

Idea: Have you tried toggling the value to "true" or "false"? E.g.
journal.template.velocity.restricted.variables=true

Posted on 6/22/09 11:41 AM in reply to Thomas Kellerer.

Top Top