简体   繁体   中英

Getting user's first and last name in Liferay 6.2 velocity template

I'm trying to display something like:

Welcome John Doe

In my Liferay Velocity Based Template which is used in a WebContent on Liferay 6.2 GA1 CE

So far the code i have is the following:

#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>

The errors i'm having are:

  1. $user.isMale() always returns false
  2. in my span.username the output is the code itself. It doesn't print the value.

Thanks in advance

Edit: The answer to your question upfront, I'll leave the rest of the explanation below:

If you want to access the current user, you can get it from $permissionChecker. So just replace your first three lines with this:

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

Here's my old explanation on why the technique used in your question didn't work:

Check $userLS.getClass().getName() : It's already not resolving properly. The reason most likely is this setting:

#
# 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 to learn about serviceLocator, but be aware that it is - for good reasons - also unavailable from templates.

#
# 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

If you're the only one writing templates (or if you trust everyone doing so) it's fine to change this configuration in portal-ext.properties: Configure serviceLocator to not be restricted, then call

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

On the positive side: This will ease error handling, as the exceptionSafeService will transparently ignore all exceptions - velocity can't handle them and without this behaviour you'd see no result (and no hint for the error message) at all, should an exception occur ever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM