We already know that render Parameter set in processAction is available to render method of same portlet. But if we set render parameter in one portlet and want to get in another portlet . In this case we use Public Render Parameter so that parameter set in one portlet can be available to another Portlet.This feature is known as Inter Portlet Communication(IPC) using Public Render Parameter.
We take a simple scenario where user can select food type from one portlet and second portlet shows food items on the basis of first portlet. This is a Sender, Receiver kind of scenario.Here we create two project and each project contain one portlet.
So lets start this step by step:-
For Sender
Step 1:-Create Liferay Project and Portlet
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as prp_sender and click finish. After that Right Click on your project ie prp_sender and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click finish.
Step 2:-Change portlet.xml
Open portlet.xml and enter supported public render parameter tag and public render parameter tags :-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>sender</portlet-name> | |
<display-name>Sender</display-name> | |
<portlet-class>com.sender.Sender</portlet-class> | |
<init-param> | |
<name>view-template</name> | |
<value>/html/sender/view.jsp</value> | |
</init-param> | |
<expiration-cache>0</expiration-cache> | |
<supports> | |
<mime-type>text/html</mime-type> | |
<portlet-mode>view</portlet-mode> | |
</supports> | |
<portlet-info> | |
<title>Sender</title> | |
<short-title>Sender</short-title> | |
<keywords></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>foodType</supported-public-render-parameter> | |
</portlet> | |
<public-render-parameter> | |
<identifier>foodType</identifier> | |
<qname xmlns:x="http://aditya/public">x:foodTypeParam</qname> | |
</public-render-parameter> | |
</portlet-app> |
Explanation:-
Here we set two things:-
1)Tell Portlet that we are using public render parameter as:-
<supported-public-render-parameter>
foodType
</supported-public-render-parameter>
1)Tell Portlet that we are using public render parameter as:-
<supported-public-render-parameter>
foodType
</supported-public-render-parameter>
2)Use Qualified name(qname) so that parameter name-spaced properly to resolve conflicts.
<public-render-parameter>
<identifier>foodType</identifier>
<qname xmlns:x="http://aditya/public">
x:foodTypeParam
</qname>
</public-render-parameter>
Step 3:-Change view.jsp
Open view.jsp and paste this content:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<portlet:defineObjects /> | |
<portlet:actionURL name="processFood" var="actionUrl"></portlet:actionURL> | |
<form action="<%=actionUrl %>" method="POST"> | |
<h3><input type="radio" name="foodType" value="veg">Vegetarian<br><br></h3> | |
<h3><input type="radio" name="foodType" value="non-veg">Non - Vegetarian<br><br></h3> | |
<input type="submit" name="Submit" value="Submit"> | |
</form> |
Note:- We are not using namespaces, so set required namespace parameter property is false in liferay-portlet.xml.
Step 4:-Change the Controller
Open your Controller and paste this:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sender; | |
import java.io.IOException; | |
import javax.portlet.ActionRequest; | |
import javax.portlet.ActionResponse; | |
import javax.portlet.PortletException; | |
import com.liferay.portal.kernel.util.ParamUtil; | |
import com.liferay.util.bridges.mvc.MVCPortlet; | |
public class Sender extends MVCPortlet { | |
public void processFood(ActionRequest request, ActionResponse response) throws PortletException, IOException { | |
String foodType = ParamUtil.getString(request, "foodType" ,"default"); | |
if(!foodType.equalsIgnoreCase("default")) | |
{ | |
response.setRenderParameter("foodType", foodType); | |
} | |
} | |
} |
Explanation:-
Here we just fetch the value from jsp and set in render Parameter.
For Receiver
Step 1:-Create Liferay Project and Portlet
Similarly as in Sender Create project with name prp-receiver and create a portlet in it.
Step 2:-Change portlet.xml
Similarly as in Sender change portlet.xml of Receiver as:-
Step 3:-Change the Controller
Open your Controller and paste this:-
Explanation:-
Here we get the value from render parameter and set in attribute so that we can use this on jsp.
Step 4:-Change the jsp
Open view.jsp and paste this :-
Explanation:-
Here we get the value that is set in Controller and on the basis of condition show different messages.
Note:- We are using jstl tage here so provide entry in liferay-plugin-package.property file as:-
For Output
Now deploy both sender and receiver portlet and add to same page .
Now Select Vegetarian and click submit
Select Non-Vegetarian and click submit
Note:-Now if you want to add the portlet on different pages .You need to provide a entry in portal-ext.properties as:-
portlet.public.render.parameter.distribution=layout-set
Hope this will Help....
You can Download Source code from IPC using Public Render Parameter.
Related Post:-
Similarly as in Sender change portlet.xml of Receiver as:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>receiver</portlet-name> | |
<display-name>Receiver</display-name> | |
<portlet-class>com.receiver.Receiver</portlet-class> | |
<init-param> | |
<name>view-template</name> | |
<value>/html/receiver/view.jsp</value> | |
</init-param> | |
<expiration-cache>0</expiration-cache> | |
<supports> | |
<mime-type>text/html</mime-type> | |
<portlet-mode>view</portlet-mode> | |
</supports> | |
<portlet-info> | |
<title>Receiver</title> | |
<short-title>Receiver</short-title> | |
<keywords></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>foodType</supported-public-render-parameter> | |
</portlet> | |
<public-render-parameter> | |
<identifier>foodType</identifier> | |
<qname xmlns:x="http://aditya/public">x:foodTypeParam</qname> | |
</public-render-parameter> | |
</portlet-app> |
Step 3:-Change the Controller
Open your Controller and paste this:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.receiver; | |
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 Receiver extends MVCPortlet { | |
@Override | |
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { | |
String foodType = renderRequest.getParameter("foodType"); | |
if(foodType!=null){ | |
renderRequest.setAttribute("foodType", foodType); | |
} | |
super.doView(renderRequest, renderResponse); | |
} | |
} |
Here we get the value from render parameter and set in attribute so that we can use this on jsp.
Step 4:-Change the jsp
Open view.jsp and paste this :-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | |
<portlet:defineObjects /> | |
<c:set var="food" value="${foodType}"/> | |
<c:choose> | |
<c:when test="${food == 'veg'}"> | |
<h3><font color="green"> You are Vegetarian. You Can Enjoy Dosa</font></h3> | |
</c:when> | |
<c:when test="${food == 'non-veg'}"> | |
<h3> <font color="red">You are Non - Vegetarian. You can enjoy Chicken.</font></h3> | |
</c:when> | |
<c:otherwise> | |
<h3>Please Select Food Type</h3> | |
</c:otherwise> | |
</c:choose> |
Explanation:-
Here we get the value that is set in Controller and on the basis of condition show different messages.
Note:- We are using jstl tage here so provide entry in liferay-plugin-package.property file as:-
For Output
Now deploy both sender and receiver portlet and add to same page .
Now Select Vegetarian and click submit
Select Non-Vegetarian and click submit
Note:-Now if you want to add the portlet on different pages .You need to provide a entry in portal-ext.properties as:-
portlet.public.render.parameter.distribution=layout-set
Hope this will Help....
You can Download Source code from IPC using Public Render Parameter.
Related Post:-