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 :

In this post, we will see how to communicate between JSF portlets using public render parameters.

If you are interested in portlet communication between none-JSF portlets, this article may interest you.

Public Render Parameters

Public Render Parameters was introduced in JSR 286 (Portlet 2.0) and aims to pass data from a portlet to another. Below the steps to use public render parameters:

  • Define the public render parameter in the portlet.xml

First of all, you must define the public render parameter that will be handled by portlets. The definition is done in the portlet configuration file Portlet.xml. Below the declaration:

Give a unique identifier to the public render parameter.

  • Configure portlets to use the public render parameter in the portlet.xml

All portlets that want to handle the public render parameter must declare the property <supported-public-render-parameter> in their Portlet.xml. This property must refer to the public render parameter identifier. To facilitate the understanding, let’s consider two portlets :

Sender portlet : A first portlet named “senderPortetName” that supports the public render parameter “identifier”. The JSF controller that manages the  senderPortletName view is called “senderPortletManagedBean”.

Receiver portlet : A second portlet named “receiverPortletName” that also supports the public render parameter “identifier”. The JSF controller that manages the  senderPortletName view is called “receiverPortletManagedBean”.

  • Send/Receive the public render parameter

With JSF Portlet, there is two ways to send and receive the public render parameter. The first one is by using a application-extension in the faces-config.xml. The second is by using the FacesContext actionReponse and renderRequest.

The following section will convers the two ways :

1- Using application extension in the faces-config.xml

Liferay provides the Liferay faces bridge that allows you to use the public render parameter mechanism by simple configuration in the faces-config.xml file. Below steps to follow :

  • Adding the liferay-faces-bridge-2.0-extension namespace in the faces-config.xml

Open the faces-config.xml file of your application, and add the following namespace to your <faces-config> property :

Your faces-config.xml must correspond to :

  • Defining the public parameter bridge

Portlet 2.0 provides a bridge that maps a render parameter to a backing bean using settings in your faces-config.xml and portlet.xml. The backing bean should have a String attribute with getter and setter.

The public parameter “identifier” of the portlet “senderPortletName” will be mapped to the attribute “senderIdentifierAttribute” of the backing bean “senderPortletManagedBean

The public parameter “identifier” of the portlet “receiverPortletName” will be mapped to the attribute “receiverIdentifierAttribute” of the backing bean “receiverPortletManagedBean

  • Processing the public render parameter in a bridge handler

You can define a bridge handler to process public render parameters. It can be done by creating a class that implements BridgePublicRenderParameterHandler as below :

Once you implemented the BridgePublicRenderParameterHandler, all portlets that handle the public render parameter must define it in their portlet.xml as below :

2- Using the FacesContext actionReponse and renderRequest

Another way to communicate between JSF Portlet using public render parameter is by using the portlet actionReponse and renderRequest. Below how to operate it :

  • Sender portlet

In the processAction phase of the sender portlet, you can set a value to the defined public render parameter as following :

  • Receiver portlet

Retreiving the value of the public render parameter is done from the portlet renderRequest as following :

Public render parameter can only be removed in processAction phase

The portal stores all public render parameters that are merged with regular parameters set in render URLs or on an ActionResponse.

Now you learned how to communicate between JSF portlets, let’s take a concrete example.

Concrete example

Let’s see a full example of using the IPC with public render parameter.

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

Let’s consider two portlets :

  • CarList (Sender portlet): 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 setted in the public render parameter. The car information will be displayed in the second portlet.
  • CarInformation  (Receiver portlet): If a car is selected (Id stored in the public render parameter), this portlet will display the car information.
IPC using Porlet session

IPC using Portlet public render parameter

Defining the public render parameter

The first step is to define the public render parameter that will be fired and processed by portlets in the portlet.xml file. If the portlets that handle the public render parameter are in the same Liferay Plugin Project, so only one definition must be in the portlet.xml file. If not, each Liferay plugin project must declare it in its portlet.xml file.

In this example, the two portlets are in the same Liferay Plugin Project.

The public render parameter identifier is carId and below the definition :

Sender portlet : CarList

  • Defining the CarList portlet in the portlet.xml file

The CarList portlet will handle the public render parameter “carId“. It must define the <supported-public-render-parameter> property :

  • Creating an attribute in the managed bean

Creating an attribute of type String in the sender portlet view controller with getter and setter which will be mapped to the defined public render parameter.

  • Selecting a car

The car selection is done by calling from the facet the method selectCar of the controller. Below the XHTML code :

In the method selectCar, we set the selected car identifier to the attribute selectedCarId. Below the code :

We must now map the public render parameter to the selectedCarId attribute in the faces-config.xml

  • Mapping the public render parameter with the backing bean attribute

In the faces-config.xml file, add the following mapping :

Receiver portlet : CarInformation

  • Defining the CarInformation portlet in the portlet.xml file

The CarInformation portlet will handle the public render parameter “carId“. It must define the <supported-public-render-parameter> property :

  • Creating an attribute in the managed bean

Creating an attribute of type String in the sender portlet view controller with getter and setter which will be mapped to the defined public render parameter.

  • Getting the selected car

When rendering the portlet view, the selectedCarId attribute will be setted by the value of the public render parameter.

We must now map the public render parameter to the selectedCarId attribute in the faces-config.xml

  • Mapping the public render parameter with the backing bean attribute

In the faces-config.xml file, add the following mapping :

Rendering after deployment

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

Liferay IPC using Portlet session

Liferay JSF IPC using Portlet public render parameter before choosing a car

Below the result after selecting Ferrari car.

Liferay IPC using Portlet session

Liferay JSF IPC using Portlet public render parameter after choosing a car

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