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

Isa*_*lez 0 java velocity liferay liferay-6

我试图显示如下内容:

欢迎John Doe

在我的Liferay基于速度的模板中,用于WebContentLiferay 6.2 GA1 CE

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

#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>
Run Code Online (Sandbox Code Playgroud)

我遇到的错误是:

  1. $user.isMale() 总是返回false
  2. 在我span.username的输出中是代码本身.它不打印该值.

提前致谢

Ola*_*ock 7

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

如果要访问当前用户,可以从$ permissionChecker获取.所以只需用以下代码替换前三行:

 #set( $user = $permissionChecker.getUser() )
Run Code Online (Sandbox Code Playgroud)

这是我对你问题中使用的技术为什么不起作用的旧解释:

检查$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
Run Code Online (Sandbox Code Playgroud)

查看这篇Wiki文章以了解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
Run Code Online (Sandbox Code Playgroud)

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

#set($userLocalService = $serviceLocator.findExceptionSafeService(
    "com.liferay.portal.service.UserLocalService"))
Run Code Online (Sandbox Code Playgroud)

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