Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取用户';Liferay 6.2 velocity模板中的姓名_Java_Liferay_Liferay 6_Velocity - Fatal编程技术网

Java 获取用户';Liferay 6.2 velocity模板中的姓名

Java 获取用户';Liferay 6.2 velocity模板中的姓名,java,liferay,liferay-6,velocity,Java,Liferay,Liferay 6,Velocity,我正试图展示如下内容: 欢迎约翰·多伊 在我的Liferay Velocity模板中,该模板用于Liferay 6.2 GA1 CE上的WebContent 到目前为止,我掌握的代码如下: #set ($userLS = $portal.getClass().forName('com.liferay.portal.service.UserLocalServiceUtil')) #set ($userId = $getterUtil.getLong($request.get("theme-disp

我正试图展示如下内容:

欢迎约翰·多伊

在我的Liferay Velocity模板中,该模板用于Liferay 6.2 GA1 CE上的
WebContent

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

#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>
#set($userLS=$portal.getClass().forName('com.liferay.portal.service.UserLocalServiceUtil'))
#set($userId=$getterUtil.getLong($request.get(“主题显示”).get(“用户id”))
#set($user=$userLS.getUserById($userId))
#如果($user.isMale())
比恩维多
#否则 比恩维达
#结束 $user.getFullName()$user.getLastName()
我犯的错误有:

  • $user.isMale()
    始终返回false
  • 在my
    span.username
    中,输出是代码本身。它不会打印值

  • 提前感谢编辑:关于你问题的答案,我将在下面留下剩下的解释:

    如果要访问当前用户,可以从$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
    
    了解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无法处理它们,如果没有这种行为,即使发生异常,您也不会看到任何结果(以及错误消息的提示)。

    非常感谢,我已经创建了一个自定义portlet来显示用户名。我会把答案留在这里,以防有人觉得有用。