留言板

how to get the userId in action class

Kiran Kumar Boyini,修改在11 年前。

how to get the userId in action class

Expert 帖子: 287 加入日期: 11-6-2 最近的帖子
Hi All,

I am using liferay 6.0.6.
I want to get the userId of user. for this I am using the following code.
long uId=new ThemeDisplay().getUserId();
System.out.println("userId "+uId);

But it is showing the following exception .
Current URL /web/guest/test? p_auth=pHYT34GE&p_p_id=EmployeePortlet_WAR_EmployeePortletportlet_INSTANCE_i0XI&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-2&p_p_col_count=1 generates exception: null
12:14:20,796 ERROR [jsp:996] java.lang.NullPointerException
java.lang.NullPointerException
at com.liferay.portal.theme.ThemeDisplay.getUserId(ThemeDisplay.java:159)


Thanks,
Kiran
thumbnail
Juhi Kumari,修改在11 年前。

RE: how to get the userId in action class

Expert 帖子: 347 加入日期: 11-12-12 最近的帖子
Hi Kiran,

Use these code to get userId in action class.
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
long userId = themeDisplay.getUserId();


Regards
Juhi
Kiran Kumar Boyini,修改在11 年前。

RE: how to get the userId in action class

Expert 帖子: 287 加入日期: 11-6-2 最近的帖子
Thank you,

But I am getting same exception as I specified above
Siby Mathew,修改在11 年前。

RE: how to get the userId in action class

Expert 帖子: 268 加入日期: 11-3-4 最近的帖子
Hi Kiran,
The previous error is because you tried to instantiate the ThemeDisplay class .
Did you get it from the request object this time ?
Can you post your code.

Thanks,
Siby
Kiran Kumar Boyini,修改在11 年前。

RE: how to get the userId in action class

Expert 帖子: 287 加入日期: 11-6-2 最近的帖子
My code as follows


------------------------------------

public class EmployeePortlet extends MVCPortlet{

@Override
public void processAction( ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long userId = themeDisplay.getUserId();
System.out.println("######LoginAction class########"+userId);

}
}



--------------------------------------------------

Thanks ,

Kiran
thumbnail
Apoorva Prakash,修改在11 年前。

RE: how to get the userId in action class

Liferay Master 帖子: 658 加入日期: 10-6-15 最近的帖子
Kiran Kumar Boyini:
public class EmployeePortlet extends MVCPortlet{

@Override
public void processAction( ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long userId = themeDisplay.getUserId();
System.out.println("######LoginAction class########"+userId);
}
}



Hi Kiran,
Try putting the following code in try catch block and put e.printStackTrace() in catch block so that you can point to exact error, however this code is flawless.
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
	                                 long userId = themeDisplay.getUserId();
	                                 System.out.println("######LoginAction  class########"+userId);


Thanks and Regards,
Apoorva Prakash
Siby Mathew,修改在11 年前。

RE: how to get the userId in action class

Expert 帖子: 268 加入日期: 11-3-4 最近的帖子
Do you mean the themeDisplay object is null...or you are getting the userId as blank.
Also try out : PortalUtil.getUser(request);
Ali Reza Akbarian,修改在11 年前。

RE: how to get the userId in action class

New Member 帖子: 6 加入日期: 12-4-7 最近的帖子
perhaps you send a action request with guest role.
thumbnail
Sreeraj AV,修改在11 年前。

RE: how to get the userId in action class

Regular Member 帖子: 239 加入日期: 10-4-27 最近的帖子
Kiran Kumar Boyini:
My code as follows


------------------------------------

public class EmployeePortlet extends MVCPortlet{

@Override
public void processAction( ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long userId = themeDisplay.getUserId();
System.out.println("######LoginAction class########"+userId);

}
}



--------------------------------------------------

Thanks ,

Kiran





You can use the given code for getting current signed in user's id. Please note that, for a non-logged(guest) user this method will return 0 as the userId..

PortalUtil.getUserId(actionRequest);
Prathyush S,修改在9 年前。

RE: how to get the userId in action class

New Member 帖子: 7 加入日期: 15-3-26 最近的帖子
@Kiran,
Are you able to get the current user id/username in the "EmployeePortlet" class?
I have created a new portlet and deployed into liferay. Portlet have a servlet. In my servlet how to get the current liferay username or userid.
How to execute the "EmployeePortlet" class in you post.

Can you please help me how to get the user details in java class?If it is possible or not can you please tell me.


Regards,
Ramesh