The first version of the portlet specification, JSR-168, did not include any support for Inter Portlet Communication. The second version, JSR-286, which is supported by Liferay since version 5 does. This page describes a series of methods that can be used to communicate portlets prior to that version, or that you might prefer to use even after that version. It also includes links to articles with information about the mechanisms introduced in JSR-286.
Standard IPC introduced in JSR-286 (Portlets 2.0) #
JSR-286 introduces two methods of Inter Portlet Communication:
- Shared render parameters: allows portlets to set params that can be read by other portlets. This rather simple mechanism will probably be enough for all but the most complex communication needs.
- Events: needed for complex scenarios. The main advantage of this second method is that it allows a fully decoupled communication. A portlet issues an event and doesn't have to care if there is anyone listening for it.
A more detailed description about these two methods an other new features of Portlets 2.0 can be read in What's new in the Java Portlet Specification V2.0 (JSR 286)?.
|The Client side events mechanism #
A full wiki article covers this topic in detail: Client-side Inter-Portlet Communication]
The pattern is:
You make an ajax call from one portlet and provide this portlet with id in another portlet. When you have an answer from the ajax call you update the div with the information you got.
It would be convenient to route the calls through a container object. Whenever, a portlet is added or removed, it needs to register/un-register itself to the container. The container would refer to the portlet through it's id and the reference to the DIV element.
Session variables sharing #
Another way is to store variables in the session and make them sharable for portlets. This way you can put stuff into session and display different things based on the information in the session. Look here: Sharing session variables
Use Liferay portlet url tag #
Proprietary portlet url tag that allows you to target one portlet to another (cross-linking). It looks something like this
<liferay-portlet:actionURL portletName="xxx" />
Where "xxx" is the <portlet-name>xxx</portlet-name> of the portlet as defined in the portlet.xml file.
Note that it also needs to include the WAR reference if it is in a portlet war.
e.g. A war named "sample-struts-liferay-portlet" having a portlet with the name "sample_struts_liferay_portlet" will have an fully qualified name as:
Furthermore, if the portlet is instanceable (<instanceable>true</instanceable> in liferay-portlet.xml) it will contain an instance identifier like so:
making the full name:
The end result is a portlet tag like so:
<liferay-portlet:actionURL portletName="sample_struts_liferay_portlet_WAR_samplestrutsliferayportlet_INSTANCE_im3I" />
You can also look at this topic for discussion and details: http://www.liferay.com/web/guest/community/forums/message_boards/message/272753