Portlets communication on the same page in render phase.

Posted on 20/11/2014 by Charalampos Chrysikopoulos

Once again I faced the same old problem: I have one portlet A which carries a value X and 2 other portlets on the same page B and C that needs this value X on their render phase. The problem exists, because also the portlet A has this value on its render phase. So, how can you transfer the value X from the render phase of the portlet A to the render phases of the portlets B and C?

According to this wiki article we cannot use the IPC methods because we have no action phase on portlet A. So, only two options remain:

  1. portlet session
  2. cookies
Lets have a look at the portlet session and lets assume we have made all the modifications on the portlets so, that the portlet A can set the value X in the session in application scope and the portlets B and C can read it.
There is a still a (big) problem: You cannot ensure that portlets A render phase will be executed before the render phases of portlet B and C.
But this time the search on the internet was fruitful. I found out that there is an element in liferay-portlet.xml which defines the order of the render phase of the portlets on the same page. At last. And the solution was always there, in the DTD. The hint of Olaf, to read the dtds, was correct.
So, the element is

<render-weight>1</render-weight>

According to the documentation in the DTD (liferay 6.1):

The default value of render-weight is 1. If set to a value less than 1, the portlet is rendered in parallel. If set to a value of 1 or greater, then the portlet is rendered serially. Portlets with a greater render weight have greater priority and will be rendered before portlets with a lower render weight. If the ajaxable value is set to false, then render-weight is always set to 1 if it is set to a value less than 1. This means ajaxable can override render-weight if ajaxable is set to false.

So, setting those element value to 3 for portlet A and to 2 for the portlets B and C will ensure that A will execute its render method before the other two portlets.

This entry was posted in Liferay and tagged communication, ipc, portlets, render, session by Charalampos Chrysikopoulos