Friday, May 4, 2012

Access Liferay Service & Util and Custom Portlet Service & Util Class in Velocity Template

Here are the steps to access services and util class of custom portlet in velocity template.

1) Make sure the services and util class which you want to access are available inside tomcat/lib/ext.

2) In order to access services you need to place below property inside portal-ext.


By default this property will be like this


Which means you wont be able to access serviceLocator inside velocity template.
So make sure before you access any service inside velocity template change this


3) Access services using findService Method of Custom Portlet in Velocity.


Normally we use findService(serviceName) method to access Liferay service.
But to access custom portlet service we have to use findService(contextName,ServiceName).

findService(serviceName) will search at the portal level and findService(servletContextName , serviceName) will search at specified portlet level.

4) Access Liferay Services using findService Method in Velocity


5) Access util class using  findUtil Method for inbuilt Util of Liferay


6) Access util Class of Custom Portlet in Velocity refer below link

http://www.liferay.com/web/raymond.auge/blog/-/blogs/2245146

8 comments:

  1. In Liferay Portal 6.2, we enable accessing services in portal-ext.properties using this line of code:

    velocity.engine.restricted.variables=

    I spent several hours to recognize this change.

    ReplyDelete
    Replies
    1. Hello all, i want to call a java method in a velocity code, i found your post about service Locator but i don't understand how to adapt my code in order to call my methode. Could you please help me!

      Thank you in advance.

      Delete
    2. Hi Amit,

      I have tried the access the java class of a portlet from theme as followed by the above step, but still i can't access anything from the java class of portlet. But the above example is working fine with hook, not working with portlet only. Can u please tell me about the problem behind that.

      Delete

nRelate Posts Only