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.
So I’am working on a portlet and i want it to display the users first name, this is the view.jsp file:
<%@ 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.
Another alternative would be to use the Liferay ThemeDisplay like so:
Although I’d advise against using JSP Scriptlets in your code, and do this in Java in your portlet class code.
~~ 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:
~~ EDIT 2: To do this in Java code ~~
See my Gist here: https://gist.github.com/4060650
it can be further simplified with using JSTL to get rid of all scriptlets.