Foren

how to get the userId in action class

Kiran Kumar Boyini, geändert vor 11 Jahren.

how to get the userId in action class

Expert Beiträge: 287 Beitrittsdatum: 02.06.11 Neueste Beiträge
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, geändert vor 11 Jahren.

RE: how to get the userId in action class

Expert Beiträge: 347 Beitrittsdatum: 12.12.11 Neueste Beiträge
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, geändert vor 11 Jahren.

RE: how to get the userId in action class

Expert Beiträge: 287 Beitrittsdatum: 02.06.11 Neueste Beiträge
Thank you,

But I am getting same exception as I specified above
Siby Mathew, geändert vor 11 Jahren.

RE: how to get the userId in action class

Expert Beiträge: 268 Beitrittsdatum: 04.03.11 Neueste Beiträge
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, geändert vor 11 Jahren.

RE: how to get the userId in action class

Expert Beiträge: 287 Beitrittsdatum: 02.06.11 Neueste Beiträge
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, geändert vor 11 Jahren.

RE: how to get the userId in action class

Liferay Master Beiträge: 658 Beitrittsdatum: 15.06.10 Neueste Beiträge
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, geändert vor 11 Jahren.

RE: how to get the userId in action class

Expert Beiträge: 268 Beitrittsdatum: 04.03.11 Neueste Beiträge
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, geändert vor 11 Jahren.

RE: how to get the userId in action class

New Member Beiträge: 6 Beitrittsdatum: 07.04.12 Neueste Beiträge
perhaps you send a action request with guest role.
thumbnail
Sreeraj AV, geändert vor 11 Jahren.

RE: how to get the userId in action class

Regular Member Beiträge: 239 Beitrittsdatum: 27.04.10 Neueste Beiträge
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, geändert vor 9 Jahren.

RE: how to get the userId in action class

New Member Beiträge: 7 Beitrittsdatum: 26.03.15 Neueste Beiträge
@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