Roufid

Liferay IPC using Portlet session (Inter Portlet Communication)

Introduction

Once a portlet is placed on a portal page, its data is not directly shared with other portlets. IPC (Inter Portlet Communication) defines the ways that a Portlet can interact and communicate with another Portlet. There are four ways to make inter portlet communication :

This post will show how to operate a Portlet communication using Portlet session.

Portlet session

Inter Portlet Communication using Portlet session was introduced in Portlet 1.0 (JSR 168) and carried in Portlet 2.0 (JSR 268).

Portlets have two kinds of session:

How to communicate using Portlet session?

To communicate with other portlets, use the Portlet APPLICATION_SCOPE session. Let’s consider that we want to make a communication between two portlets, Portlet S (Portlet Sender: which will send data) and Portlet R (Portlet Receiver : Portlet which will receive data). Below steps to follow :

First step is to define the “private-session-attributes” property in the Liferay-portlet.xml file of each portlet. Add the following line within the “<portlet>” tag :

In your sender portlet, store the object you want to send in the session. To do so, get the portlet session from the action request or render request and set an attribute with the APPLICATION_SCOPE option. Below how to do :

In your receiver portlet, retrieve the stored data from the session. To do so, get the portlet session from the render request and get the stored object using the APPLICATION_SCOPE option. Below how to do :

You can store object bean in the session. The stored object must be Serializable.

Concrete example

Let’s see a full example of using the IPC with Portlet session.


The code source is available on Github. Download the source

Let’s consider two portlets :

IPC using Portlet session

In this example, I used JSF as portlet framework. You can choose whatever you want. The logic remains the same.

Portlet 1 : CarList

Defining the CarList portlet in the portlet.xml file.

Registring the Liferay portlet in the liferay-portlet.xml file with the private-session-attributes set to false.

Storing the data in the session.

Portlet 2 : CarInformation

Defining the CarInformation portlet in the portlet.xml file.

Registring the Liferay portlet in the liferay-portlet.xml file with the private-session-attributes set to false.

Retrieving the data from the session and deleting it after use.

Rendering after deployment :

The CarList portlet is on the left. CarInformation portlet is on the right.

Liferay IPC using Portlet session before choosing a car

Below the result after selecting Ferrari car.

Liferay IPC using Portlet session after choosing a car


The code source is available on Github. Download the source