Josymar De Leon
Josymar De Leon

Reputation: 147

PortletSession using JSF Liferay Portlets

I am developing a JSF 2 Portlet, but i need get global session variable.

The request works fine with:

FacesContext facesContext = FacesContext.getCurrentInstance();
    PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest();

But the session is null:

PortletSession session = request.getPortletSession(false);

if put this code in liferay-portlet.xml file works fine, but i can't do IPC: Inter Portlet Comunication

<!-- Portlet session -->
    <private-request-attributes>false</private-request-attributes>
    <private-session-attributes>false</private-session-attributes>

How i get and set variables session without use the last code?

Thanks

Today i i tried with this:

FacesContext facesContext = FacesContext.getCurrentInstance(); 
PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest(); 
HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(request); 
HttpSession session = httpRequest.getSession(true); 
System.out.println(session.getAttribute("XXXX"));

but is null

Upvotes: 0

Views: 4965

Answers (4)

Josymar De Leon
Josymar De Leon

Reputation: 147

Warning: Using <private-session-attributes>false</private-session-attributes> with Liferay Faces has been known to cause memory leaks. Use at your own risk!

I solved my problem configuring liferay-portlet.xml

1.) I changed the liferay-portlet.xml only with:

<private-session-attributes>false</private-session-attributes>

2.) In the Java Class i created method:

FacesContext facesContext = FacesContext.getCurrentInstance();
PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest();
PortletSession session = request.getPortletSession(false);

If you get session attributes

session.getAttribute("XXXXXX",PortletSession.APPLICATION_SCOPE);

If you set session attributes

session.setAttribute("NAME","VALUE",PortletSession.APPLICATION_SCOPE);

Upvotes: 1

Felix Christy
Felix Christy

Reputation: 2193

There are 2 options :

1) When you set the attribute in the PortletSession, use this method public void setAttribute(String name, Object value, int scope), where scope should be PortletSession.APPLICATION_SCOPE

When you do this, please change this to your liferay-portlet.xml

<private-request-attributes>false</private-request-attributes>
<private-session-attributes>false</private-session-attributes>

Warning: Using <private-session-attributes>false</private-session-attributes> with Liferay Faces has been known to cause memory leaks. Use at your own risk!

2) Use HttpSession instead of PortletSession, in order to get the HttpSession, please use PortalUtil.getHttpServletRequest(portletRequest) and from the httpServletRequest, get the HttpSession.

Hope this helps.

Thanks

Upvotes: 1

Ashutosh Khanna
Ashutosh Khanna

Reputation: 49

Warning: Using <private-session-attributes>false</private-session-attributes> with Liferay Faces has been known to cause memory leaks. Use at your own risk!

Reorder it will work!

<portlet>
    <portlet-name>distribution</portlet-name>
    <icon>/icon.png</icon>
    <instanceable>false</instanceable>
    <private-session-attributes>false</private-session-attributes>
            ...
            ...
            ...
            ...

Upvotes: 4

Samuel Tian
Samuel Tian

Reputation: 787

Christy's 2nd option worked for me. When i set

<private-session-attributes>false</private-session-attributes>

it causes my portlet deploy fail. There was an error while parsing xml file.

I dont know why. Liferay Version 6.1.1

Upvotes: 0

Related Questions