在Liferay 6.2速度模板中获取用户的名字和姓氏

时间:2014-03-08 21:10:41

标签: java liferay liferay-6 velocity

我正在尝试显示类似的内容:

  

欢迎John Doe

在Liferay 6.2 GA1 CE上的WebContent中使用的基于Liferay Velocity的模板

到目前为止,我的代码如下:

#set ($userLS = $portal.getClass().forName('com.liferay.portal.service.UserLocalServiceUtil'))
#set ($userId = $getterUtil.getLong($request.get("theme-display").get("user-id")))
#set ($user = $userLS.getUserById($userId))

<div class="user-welcome">
    #if($user.isMale())
        <span class="welcome-msg">Bienvenido</span><br>
    #else
        <span class="welcome-msg">Bienvenida</span><br>
    #end
    <span class="username">$user.getFullName() $user.getLastName()</span>
</div>

我遇到的错误是:

  1. $user.isMale()始终返回false
  2. 在我的span.username中,输出是代码本身。它不会打印该值。
  3. 提前致谢

1 个答案:

答案 0 :(得分:7)

编辑:提前回答你的问题,我将在下面留下其余的解释:

如果您想访问当前用户,可以从$ permissionChecker获取。所以只需用以下内容替换前三行:

 #set( $user = $permissionChecker.getUser() )

以下是我对你问题中使用的技术不起作用的原因的解释:

检查$userLS.getClass().getName():它已经无法正常解析。最有可能的原因是这个设置:

#
# Set a comma delimited list of Java classes the Velocity engine cannot have
# access to.
#
velocity.engine.restricted.classes=\
    java.lang.Class,\
    java.lang.ClassLoader,\
    java.lang.Thread

Check this Wiki article要了解serviceLocator,但要注意它是 - 出于好的理由 - 也无法从模板中获取。

#
# Set a comma delimited list of variables the Velocity engine cannot
# have access to. This will affect Dynamic Data List templates, Journal
# templates, and Portlet Display templates.
#
velocity.engine.restricted.variables=serviceLocator

如果您是唯一一个编写模板的人(或者如果您信任所有人都这样做),可以在portal-ext.properties中更改此配置:将serviceLocator配置为不受限制,然后调用

#set($userLocalService = $serviceLocator.findExceptionSafeService(
    "com.liferay.portal.service.UserLocalService"))

从积极的一面:这将简化错误处理,因为exceptionSafeService将透明地忽略所有异常 - velocity无法处理它们,如果没有这种行为,你将看不到任何结果(并且没有提示错误消息) ,如果发生异常,将会发生。

相关问题