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:

  • PORTLET_SCOPE session: Objects stored in the session are available to the portlet during requests for the same portlet window that the objects where stored from.
  • APPLICATION_SCOPE session: Objects stored are visible to all other portlets that are packaged as part of the same portlet application (same war), this is the default behavior. Liferay provides a way to share the data even with portlets that are not located in the same war.

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 :

  • Defining the private-session-attributes in Liferay-portlet.xml

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 :

  • Storing Data in the sender portlet

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 :

  • Retrieving the stored data in the receiver portlet

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.

github-mark
The code source is available on Github. Download the source

Let’s consider two portlets :

  • CarList (Portlet 1): This portlet shows a list of cars. The user can select a car from the list. When the user select a car, the car’s id is stored in the session. The car information will be displayed in the second portlet.
  • CarInformation  (Portlet 2): If a car is selected (Id stored in the session), this portlet will display the car information.
IPC using Porlet session

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

  • Portlet.xml

Defining the CarList portlet in the portlet.xml file.

  • Liferay-portlet.xml

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

  • Selecting the car

Storing the data in the session.

Portlet 2 : CarInformation

  • Portlet.xml

Defining the CarInformation portlet in the portlet.xml file.

  • Liferay-portlet.xml

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

  • Getting the carId from the session :

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

Liferay IPC using Portlet session before choosing a car

Below the result after selecting Ferrari car.

Liferay IPC using Portlet session

Liferay IPC using Portlet session after choosing a car

github-mark
The code source is available on Github. Download the source