Foros de discusión

how to get the userId in action class

Kiran Kumar Boyini, modificado hace 11 años.

how to get the userId in action class

Expert Mensajes: 287 Fecha de incorporación: 2/06/11 Mensajes recientes
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, modificado hace 11 años.

RE: how to get the userId in action class

Expert Mensajes: 347 Fecha de incorporación: 12/12/11 Mensajes recientes
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, modificado hace 11 años.

RE: how to get the userId in action class

Expert Mensajes: 287 Fecha de incorporación: 2/06/11 Mensajes recientes
Thank you,

But I am getting same exception as I specified above
Siby Mathew, modificado hace 11 años.

RE: how to get the userId in action class

Expert Mensajes: 268 Fecha de incorporación: 4/03/11 Mensajes recientes
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, modificado hace 11 años.

RE: how to get the userId in action class

Expert Mensajes: 287 Fecha de incorporación: 2/06/11 Mensajes recientes
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, modificado hace 11 años.

RE: how to get the userId in action class

Liferay Master Mensajes: 658 Fecha de incorporación: 15/06/10 Mensajes recientes
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, modificado hace 11 años.

RE: how to get the userId in action class

Expert Mensajes: 268 Fecha de incorporación: 4/03/11 Mensajes recientes
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, modificado hace 11 años.

RE: how to get the userId in action class

New Member Mensajes: 6 Fecha de incorporación: 7/04/12 Mensajes recientes
perhaps you send a action request with guest role.
thumbnail
Sreeraj AV, modificado hace 11 años.

RE: how to get the userId in action class

Regular Member Mensajes: 239 Fecha de incorporación: 27/04/10 Mensajes recientes
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, modificado hace 9 años.

RE: how to get the userId in action class

New Member Mensajes: 7 Fecha de incorporación: 26/03/15 Mensajes recientes
@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