简体   繁体   English

如何在Liferay上获得用户的名字?

[英]how do I get the user's first name on Liferay?

I'm new to both Liferay and Java and I've been struggling for a day on how to get the user firstname, the documentation on this product is really poor so any help would be highly appreciated. 我是Liferay和Java的新手,我一直在努力争取如何获得用户名字,这个产品的文档真的很差,所以任何帮助都会受到高度赞赏。 So I'am working on a portlet and i want it to display the users first name, this is the view.jsp file: 所以我正在开发一个portlet,我希望它显示用户的名字,这是view.jsp文件:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="com.liferay.portal.model.UserModel " %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<portlet:defineObjects />


<liferay-ui:success key="success" message="Greeting saved
       successfully!" />

<%
PortletPreferences prefs = renderRequest.getPreferences();
String greeting = (String)prefs.getValue(
"greeting", "Hello! Welcome to our portal.");
%>
<%

%>
<p><%= greeting %></p>
<portlet:renderURL var="editGreetingURL">
<portlet:param name="mvcPath" value="/edit.jsp" />
<portlet:param name="userName" value="Test" />

</portlet:renderURL>

<p><a href="<%= editGreetingURL %>">Edit greeting</a></p>

I already played with the usermodel class or interface, I actually found the method :getFirstName() but I just cant get it to work. 我已经使用了usermodel类或接口,我实际上找到了方法:getFirstName()但我不能让它工作。

Another alternative would be to use the Liferay ThemeDisplay like so: 另一种选择是使用Liferay ThemeDisplay,如下所示:

<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<liferay-theme:defineObjects />

<%= themeDisplay.getUser().getFirstName() %>

Although I'd advise against using JSP Scriptlets in your code, and do this in Java in your portlet class code. 虽然我建议不要在你的代码中使用JSP Scriptlets,并在你的portlet类代码中用Java做这件事。

~~ EDIT: Adding alternative to use User more than once ~~ ~~编辑:添加替代用户不止一次~~

An example of useing a mixture of Tony's and my approaches would be like so: 使用Tony和我的方法混合的一个例子是这样的:

<%= com.liferay.portal.model.User user = themeDisplay.getUser(); %>

~~ EDIT 2: To do this in Java code ~~ ~~编辑2:用Java代码做~~

See my Gist here: https://gist.github.com/4060650 请在此处查看我的要点: https//gist.github.com/4060650

it can be further simplified with using JSTL to get rid of all scriptlets. 使用JSTL去除所有scriptlet可以进一步简化它。

Try adding to the jsp: 尝试添加到jsp:

<% 
com.liferay.portal.model.User user = 
      com.liferay.portal.util.PortalUtil.getUser(request);
String firstName = user.getFirstName();
%>
<p>User First Name=<%= firstName %></p>

I added the full package for ease of understanding. 为了便于理解,我添加了完整的软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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