使用简单的 Java 代码获取当前用户 Liferay [英] Get the current user Liferay using a simple Java code
问题描述
我正在使用:Liferay 6.0.6 和 JBoss 5.1 和 Struts2.
I'm working with : Liferay 6.0.6 with JBoss 5.1 and Struts2.
我的问题是,如何使用 Java 代码在 Liferay 登录后获取当前用户.
My question is, how to get the current user in Liferay once logged in, using a Java code.
推荐答案
在你的 doView/processAction 方法中执行以下操作
In your doView/processAction method do following
User user = (User) request.getAttribute(WebKeys.USER);
或使用 ThemeDisplay 对象.它包含另一个信息,如 companyId、groupId、...
or use the ThemeDisplay object. It contains another information like companyId, groupId, ...
ThemeDisplay td =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
User user = td.getUser();
类 ThemeDisplay、User nad WebKeys 是 portal-service.jar 的一部分.
Classes ThemeDisplay, User nad WebKeys are part of portal-service.jar.
如果你只需要一些 id 来识别当前用户,你也可以使用
If you need just some id to identify current user you can also use
String userId = request.getRemoteUser();
这个解决方案不是 Liferay 特有的,应该可以在 jsr-286 门户之间移植.
This solution is not Liferay specific and should be portable among jsr-286 portals.
这篇关于使用简单的 Java 代码获取当前用户 Liferay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!