Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Kiran Kumar Boyini
how to get the userId in action class
November 29, 2012 4:28 AM
Answer

Kiran Kumar Boyini

Rank: Expert

Posts: 277

Join Date: June 2, 2011

Recent Posts

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
Juhi Kumari
RE: how to get the userId in action class
November 29, 2012 5:17 AM
Answer

Juhi Kumari

Rank: Expert

Posts: 347

Join Date: December 12, 2011

Recent Posts

Hi Kiran,

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


Regards
Juhi
Kiran Kumar Boyini
RE: how to get the userId in action class
November 29, 2012 5:30 AM
Answer

Kiran Kumar Boyini

Rank: Expert

Posts: 277

Join Date: June 2, 2011

Recent Posts

Thank you,

But I am getting same exception as I specified above
Siby Mathew
RE: how to get the userId in action class
November 29, 2012 6:21 AM
Answer

Siby Mathew

Rank: Expert

Posts: 261

Join Date: March 4, 2011

Recent Posts

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
RE: how to get the userId in action class
November 29, 2012 6:58 AM
Answer

Kiran Kumar Boyini

Rank: Expert

Posts: 277

Join Date: June 2, 2011

Recent Posts

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
Apoorva Prakash
RE: how to get the userId in action class
November 29, 2012 7:04 AM
Answer

Apoorva Prakash

Rank: Liferay Master

Posts: 659

Join Date: June 15, 2010

Recent Posts

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.
1ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
2                                     long userId = themeDisplay.getUserId();
3                                     System.out.println("######LoginAction  class########"+userId);


Thanks and Regards,
Apoorva Prakash
Siby Mathew
RE: how to get the userId in action class
November 29, 2012 7:04 AM
Answer

Siby Mathew

Rank: Expert

Posts: 261

Join Date: March 4, 2011

Recent Posts

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
RE: how to get the userId in action class
November 30, 2012 6:21 AM
Answer

Ali Reza Akbarian

Rank: New Member

Posts: 6

Join Date: April 7, 2012

Recent Posts

perhaps you send a action request with guest role.
Sreeraj AV
RE: how to get the userId in action class
December 3, 2012 3:57 AM
Answer

Sreeraj AV

Rank: Regular Member

Posts: 233

Join Date: April 26, 2010

Recent Posts

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..

1PortalUtil.getUserId(actionRequest);