0
votes

Actually I am trying to share the data between 2 portlets in a 2 different plugin projects

Below are the steps I followed to share the data :

Step1: Create liferay plugin project named as Senderproj and created one portlet under Senderport then write below code in doView method

 PortletSession session=req.getPortletSession();
 String s="naresh";
 session.setAttribute("gates",s,PortletSession.APPLICATION_SCOPE); 

step2: Create liferay plugin project named as Receiverproj and created one portlet named as Receiverport then write below code in doView method

 PortletSession ps = req.getPortletSession();
 String tabName = (String)ps.getAttribute("gates",PortletSession.APPLICATION_SCOPE);
 System.out.println("this is from doView of ipc receiver portlet"+tabName);

Step 3: I added the property in liferay-portlet.xml like below

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

When I drop two portlet in a portal page I got session value null in Receiverport.

can any one help out

1
You should really avoid using the session and session sharing if possible. There are many better solutions to sharing session data that I've enumerated here: community.liferay.com/blogs/-/blogs/…stiemannkj1

1 Answers

-1
votes

Fist check that <private-session-attributes>false</...> is correctly set in both portles (sender and receiver).

Then, set and get session attributes using APPLICATION_SCOPE:

renderRequest.getPortletSession().setAttribute(
    "name", "some value", PortletSession.APPLICATION_SCOPE
);

renderRequest.getPortletSession().getAttribute(
    "name", PortletSession.APPLICATION_SCOPE
);

So far it seems that's what you're already doing.

If they are on the same page, we must ensure that they are loaded in the correct order. In Liferay this can be achieved by setting the render-weight. (In a real case it is better that they do not depend on the order in which they are loaded.)

<!-- Sender -->
<portlet>
    <portlet-name>test-a</portlet-name>
    <icon>/icon.png</icon>
    <instanceable>true</instanceable>
    <private-session-attributes>false</private-session-attributes>
    <render-weight>3</render-weight> 
    ...
</portlet>

<!-- Receiver -->
<portlet>
    <portlet-name>test-b</portlet-name>
    <instanceable>true</instanceable>
    <icon>/icon.png</icon>
    <private-session-attributes>false</private-session-attributes>
    <render-weight>2</render-weight> 
    ...
</portlet>

Besides, this link may be helpful: Liferay Session Sharing Demystified