Inter Portlet Communication (IPC) can be implemented in different ways. In this article IPC is demonstrated using public render parameter. According to JSR 168(Portlet Specification 1.0) Â there is no concept of public render parameter. In JSR 286 (Portlet Specification 2.0) concept of public render parameter is introduced. An ordinary parameter can be declared as public so that other portlet can read the parameter.Â
What makes a parameter a public?
You have to define the parameter as public in portlet.xml. While implementing IPC both the portlet.xml should have the same declaration. Consider we want to make a parameter ‘myname‘ as public. This is done as belowÂ
i) In portlet.xml before </portlet> tag add below linesÂ
<supported-public-render-parameter> myname </supported-public-render-parameter>
ii)Â In portlet.xml after </portlet> tag add below lines
<public-render-parameter> <identifier>myname</identifier> <qname xmlns:x="https://proliferay.com/public">x:myname</qname> </public-render-parameter>
Note : Both i) and ii) are mandatory
Complete portlet.xml should look like this:Â
<?xml version="1.0"?> <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0"> <portlet> <portlet-name>render-parameter-sender</portlet-name> <display-name>Render Parameter Sender</display-name> <portlet-class>com.proliferay.demo.RenderParameterSenderPortlet</portlet-class> <init-param> <name>view-template</name> <value>/view.jsp</value> </init-param> <expiration-cache>0</expiration-cache> <supports> <mime-type>text/html</mime-type> </supports> <portlet-info> <title>Render Parameter Sender</title> <short-title>Render Parameter Sender</short-title> <keywords>Render Parameter Sender</keywords> </portlet-info> <security-role-ref> <role-name>administrator</role-name> </security-role-ref> <security-role-ref> <role-name>guest</role-name> </security-role-ref> <security-role-ref> <role-name>power-user</role-name> </security-role-ref> <security-role-ref> <role-name>user</role-name> </security-role-ref> <supported-public-render-parameter> myname </supported-public-render-parameter> </portlet> <public-render-parameter> <identifier>myname</identifier> <qname xmlns:x="https://proliferay.com/public">x:myname</qname> </public-render-parameter> </portlet-app>
Important
Suppose you have two portlets. First portlet is A and second portlet is B. Both are in separate WARs. From portlet A you want to define and set public render parameter. From portlet B you want to read the parameter. For this in both the portlet.xml of the portlet A and B you should have the same public render parameter definition. Otherwise public render parameter is not going to work.
How to set value for the public render parameter:
In this article for setting value for the public render parameter a separate portlet is created (it’s name is render-parameter-sender-portlet). From this portlet public render parameter is set in process action. Below are the detailsÂ
view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <portlet:defineObjects /> <portlet:actionURL var="actionURL" name="setRenderParameter"/> <aui:form action="<%=actionURL.toString() %>" method="post"> <aui:input name="name" label="Set Your Name In Public"/> <aui:button-row> <aui:button type="submit" value="Set"/> </aui:button-row> </aui:form>
RenderParameterSenderPortlet.java:
package com.proliferay.demo; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import javax.portlet.ProcessAction; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.util.bridges.mvc.MVCPortlet; /** * * @author Hamidul Islam * */ public class RenderParameterSenderPortlet extends MVCPortlet { @ProcessAction(name="setRenderParameter") public void processAction(ActionRequest request, ActionResponse response) throws IOException, PortletException { String name = ParamUtil.getString(request, "name",""); /** * myname is same as like normal parameter. But it is configured * as public portlet.xml file * Any other portlet can read this parameter * * To read this parameter in your portlet you * should tell your portlet that you are going to * use public render parameter. That configuration is done again in * portlet.xml */ response.setRenderParameter("myname", name); } }
How to read public render parameter:
In this article for reading value of the public render parameter a separate portlet is created (it’s name is render-parameter-receiver-portlet). Below are the detailsÂ
view.jsp
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> <!-- In JSP we can directly read public render parameter --> <!-- Also public render parameter is available in any other life cycle methods --> <% String name = ParamUtil.getString(request, "myname"); %> <H3>Public parameter name is 'myname'</H3> <H3>Its value is </H3> <b><%=name %></b>
RenderParameterReceiverPortlet.java:
package com.proliferay.demo; import java.io.IOException; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import com.liferay.util.bridges.mvc.MVCPortlet; public class RenderParameterReceiverPortlet extends MVCPortlet{ @Override public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { /** * Reading public render parameter in render phase */ String myname = renderRequest.getParameter("myname"); System.out.println("=========myname======="+myname); super.doView(renderRequest, renderResponse); } }
Note: In our case the public render parameter name is ‘myname‘
Â
 Related PostÂ
https://proliferay.com/inter-portlet-communiation-ipc-event/Â
very nice post
very usefull
Good n simple..
Thanks Hamid bhai
Would it work if both portlets are placed on different pages?