在 Liferay 6.2 中获取当前用户(themeDisplay 始终为 null)

Getting current user in Liferay 6.2 (themeDisplay always null)

我尝试使用 jsp 页面获取当前用户的 ID:

<%@page import="com.liferay.portal.theme.*" %>
<%@page import="com.liferay.portal.kernel.util.WebKeys" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<%
themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
User objUser=themeDisplay.getUser(); 
%>    

但 themeDisplay return 始终为 null !!

我也尝试在我的 struts 操作中使用 :

public class acces extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ThemeDisplay themeDisplay =
(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
Long currentuser = themeDisplay.getUserId();

但仍然 return 为空

我正在使用 liferay 6.2 ce,webmodule struts 1.2 作为 war 文件部署到 liferay 中,使用在 appManager 中安装。

请帮忙,我缺少一些配置?

在 Liferay 中获取当前登录用户的方法不止一种。

1 ] User currentUser = PortalUtil.getUser( request );

2]User currentUser = (User) request.getAttribute(WebKeys.USER);

3 ]

ThemeDisplay td  = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY );
User currentUser = td.getUser();`

4 ]

LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
User currentUser = liferayFacesContext.getUser();

希望其中一位对您有所帮助。

接着是您对我最初问题的回复...

如果你的Struts应用可以添加,说明里面至少有一个portlet.xml文件(不知道Liferay应用管理器有没有帮你添加)...在任何情况下,您都无法以这种方式访问​​上下文。

通过 Struts 创建 portlet 应用程序的最佳方法是使用 Struts Portlet Bridge ;您如何在我提供的 link 中看到它允许您的代码符合 JSR 规范;然后它应该能够与 portlet 容器正确交互...

您可以只尝试一种操作并测试它是否解决了您的问题...从标准 Web 应用程序到 Portlet 应用程序的转换应该尽可能轻松。

看起来您缺少 taglib 指令您可以通过在 jsp 中执行以下操作来获取 Liferay 中的用户对象:

<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>
<theme:defineObjects/> <%-- Gives you the user object. --%>
<%= user.getLogin() %>

https://docs.liferay.com/portal/6.2/javadocs-all/com/liferay/portal/model/User.html