Liferay.com

Wednesday, November 30, 2011

How to access custom portlet services in velocity template

By default we will have following property which won't allow us to use serviceLocator variable in velocity templates.

    #
    # Input a comma delimited list of variables which are restricted from the
    # context in Velocity based Journal templates.
    #
    journal.template.velocity.restricted.variables=serviceLocator


In order to access you need to change that property as below


journal.template.velocity.restricted.variables=


Default findServce(serviceName)  method (what we normally use) searches @ portal level where as other one with extra parameter as shown here  findService(servletContextName, serviceName) searches @ particular portlet level

TO add more to it here are the method signatures & implementations in ServiceLocator.java

        public Object findService(String serviceName) {
                Object bean = null;

                try {
                        bean = PortalBeanLocatorUtil.locate(_getServiceName(serviceName));
                }
                catch (Exception e) {
                        _log.error(e, e);
                }

                return bean;
        }


        public Object findService(String servletContextName, String serviceName) {
                Object bean = null;

                try {
                        bean = PortletBeanLocatorUtil.locate(
                                servletContextName, _getServiceName(serviceName));
                }
                catch (Exception e) {
                        _log.error(e, e);
                }

                return bean;
        }



HoW tO uSe


Suppose i have an custom Entity named MyEntity(defined through service.xml)


#set ($myEntityService = $serviceLocator.findService("", "com.rnd.common.portlet.service.MyEntityLocalService"))


I have a column named count under MyEntity, i can access as below


#set ($count =$myEntityService.getCount())


$count 


Hope that helps :)


Please feel free to add your comments. Cheers

2 comments:

  1. Nice Blog. This blog is very intersting and you are provide best information for users. I always visited your blog site.Thanks for your wonderful sharing Liferay Portal.

    ReplyDelete