Using custom services or liferay services in liferay themes (velocity templates)?

纵然是瞬间 提交于 2019-12-19 04:05:10

问题


How to use custom services method in liferay themes in velocity files like init_custom.vm, portal_normal.vm etc.

I see liferay provides a lot of variables of helper utility classes like $portalUtil for PortalUtil, $getterUtil for GetterUtil and so on, inside the init.vm file.

So is it possible to get instance of my custom services like an instance of com.my.custom.service.MyCustomLocalServiceImpl or services of liferay from UserLocalServiceImpl?

Here is some psuedo code, to give an idea of what I need:

// this code calls method from MyCustomLocalServiceImpl class to fetch items
#set ($listOfItems = $myCustomLocalServiceUtil.getAllItems())

// this code calls method from UserLocalServiceImpl class to fetch users
#set ($listOfUsers = $userLocalServiceUtil.getUsers(0, 99))

Environment: Liferay 6.1 CE GA1


回答1:


It is possible.

  1. The following code shows how to get the services:

    // Fetching instance of my custom services
    #set ($myCustomLocalService = $serviceLocator.findService('myCustomServices-portlet', 'com.my.custom.service.MyCustomLocalService'))
    
    // Fetching instance of UserLocalServiceImpl
    #set ($userLocalService = $serviceLocator.findService('com.liferay.portal.service.UserLocalService'))
    
  2. Then simply call the service methods:

    #set ($listOfItems = $myCustomLocalService.getAllItems())
    
    #set ($listOfUsers = $userLocalService.getUsers(0, 99))
    

For Liferay 6.1 CE GA1: I found this class VelocityVariablesImpl (see methods like insertHelperUtilities, insertVariables) which actually makes all the variables and helper utilities available to the velocity templates.




回答2:


You can extend velocity context used in theme with custom variables and services using following hook plugin. Let's say you need to use your custom local service.

  1. create a hook plugin with following liferay-hook.xml definition

    <hook>
        <portal-properties>portal.properties</portal-properties>
    </hook>
    
  2. create portal.properties in main/resources (when you use maven) or in docroot/WEB-INF/src (when you use plugins sdk), place following configuration there

    servlet.service.events.pre=com.my.custom.action.MyCustomPreAction
    
  3. create com.my.custom.action.MyCustomPreAction class in your hook that will extend com.liferay.portal.kernel.events.Action

  4. implement run method

    @Override
    public void run(final HttpServletRequest request, final HttpServletResponse response)
        throws ActionException {
    
        Map<String, Object> vmVariables = (Map<String, Object>) request.getAttribute(WebKeys.VM_VARIABLES);
        if (vmVariables == null) {
          vmVariables = new HashMap<String, Object>(1);
        }
        vmVariables.put("myCustomServiceUtil", com.my.custom.service.MyCustomLocalServiceUtil.class);
        request.setAttribute(WebKeys.VM_VARIABLES, map);
    }
    
  5. after your hook is deployed you can use your custom service in velocity template of your theme

    // this code calls method from MyCustomLocalServiceImpl class to fetch items
    #set ($listOfItems = $myCustomServiceUtil.getAllItems())
    


来源:https://stackoverflow.com/questions/12053910/using-custom-services-or-liferay-services-in-liferay-themes-velocity-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!