Service 在liferay主题(velocity模板)中使用自定义服务或liferay服务?

Service 在liferay主题(velocity模板)中使用自定义服务或liferay服务?,service,liferay,liferay-velocity,liferay-theme,Service,Liferay,Liferay Velocity,Liferay Theme,如何在velocity文件(如init_custom.vm,portal_normal.vm等)中的liferay主题中使用自定义服务方法 我看到liferay在文件中提供了许多辅助工具类的变量,如$portalUtilfor、$getterUtilfor等等 那么,是否可以从userlocalserviceinpl获取自定义服务的实例,如com.my.custom.service.mycustomlocalserviceinpl的实例或liferay的服务 以下是一些psuedo代码,让我了解

如何在velocity文件(如
init_custom.vm
portal_normal.vm
等)中的liferay主题中使用自定义服务方法

我看到liferay在文件中提供了许多辅助工具类的变量,如
$portalUtil
for、
$getterUtil
for等等

那么,是否可以从
userlocalserviceinpl
获取自定义服务的实例,如
com.my.custom.service.mycustomlocalserviceinpl
的实例或liferay的服务

以下是一些psuedo代码,让我了解我需要什么:

// 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))
环境:Liferay 6.1 CE GA1是可能的

  • 以下代码显示了如何获取服务:

    // 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'))
    
  • 然后只需调用服务方法:

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

  • 对于Liferay 6.1 CE GA1:我发现了这样一个类(请参见,)它实际上使所有变量和助手实用程序都可用于velocity模板。

    您可以使用以下钩子插件扩展主题中使用的velocity上下文和自定义变量和服务。假设您需要使用自定义的本地服务

  • 使用以下liferay-hook.xml定义创建一个钩子插件

    <hook>
        <portal-properties>portal.properties</portal-properties>
    </hook>
    
  • 在钩子中创建
    com.my.custom.action.MyCustomPreAction
    类,该类将扩展
    com.liferay.portal.kernel.events.action

  • 执行
    run
    方法

    @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);
    }
    

  • 谢谢这也是一个很好的方法。
    @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);
    }
    
    // this code calls method from MyCustomLocalServiceImpl class to fetch items
    #set ($listOfItems = $myCustomServiceUtil.getAllItems())