2016-01-21 73 views
1

一個簡單的例子:爲什麼Liferay對象不能在我的Velocity模板中渲染?

/* Get user roles */ 
#set($userId=$request.attributes.get('USER_ID')) 
#set($roleLocalService=$serviceLocator.findService("com.liferay.portal.service.RoleLocalService")) 
$roleLocalService.getUserRoles($userId) 

在頁面上呈現什麼只是沒有數據的文本。

$ roleLocalService.getUserRoles($用戶id)

我缺少什麼?

回答

3
  1. 請確保您允許使用serviceLocatorportal.properties中的default valuevelocity.engine.restricted.variables=serviceLocator,這意味着serviceLocator不適用於模板。將其設置爲「空白」(或至少不包括serviceLocator)。例如,在一個portal-ext.properties文件Liferay的主目錄內將其設置爲

velocity.engine.restricted.variables=

  1. $request.attributes.get將爲您提供userId的字符串值。所以,你需要將其轉換爲長期使用的東西,如:

$roleLocalService.getUserRoles($getterUtil.getLong($userId))