Inter Portlet Communication shortly we can call IPC is the way of making communication among different Portlets which are in same page or reside in other pages.
In the IPC mechanism Portlet will share data among the Portlets and Portlet will work based on the other Portlet.
From JSR 168/Portlet 1.0 we have feature of Inter Portlet Communication with Portlet Sessions.
Each Portlet have its own session that is called Portlet Session and session data won’t be shared with other Portlets.
To make Inter Portlet Communication with help of Portlet session we will make Portlet session as public so that data will be shared in other Portlets.
IPC with Portlet Sessions Implementation
Step: 1
We explicitly make Portlet session as public so that it can be accessible by other Portlets to make this we need to add following xml tag in the liferay-portlet.xml
<portlet> ------ <private-session-attributes>false</private-session-attributes> ------ </portlet> |
Step: 2
We need set some value in Portlet session so that it will be accessed by other Portlet. Let’s say we have Portlet A so that we need to set some value for Portlet A session.
Assume when we submit some values we need to set value in Portlet A doView(--) OR processAction(--)OR Custom Action method and this values can be accessed in Portlet B at the time of rendering Portlets.
We need to remember that Portlet session scope should be Application Scope then only value can be available in other Portlets.
The following is code to set value in Portlet session
doView(--) method
String emailAddress=ParamUtil.getString(renderRequest,"inputEmailAddress"); PortletSession portletSession = renderRequest.getPortletSession(); portletSession.setAttribute("emailAddress",emailAddress,PortletSession.APPLICATION_SCOPE); |
processAction(--) OR Custom Action
String emailAddress=ParamUtil.getString(actionRequest,"inputEmailAddress"); PortletSession portletSession = actionRequest.getPortletSession(); portletSession.setAttribute("emailAddress",emailAddress,PortletSession.APPLICATION_SCOPE); |
Step: 3
Once we set the value in Portlet A session now we can access the value in Portlet B.
While you access values we should use same scope that was used in Portlet A session.
The following is code snippet to access data from Session
PortletSession portletSession = renderRequest.getPortletSession(); String emailAddress = (String)portletSession.getAttribute("emailAddress ",PortletSession.APPLICATION_SCOPE); |
Download Portlet Sessions IPC Plugin Portlets
Sender Portlet Screen
Receiver Portlet Screen
Inter Portlet Communication between Portlets in the page
Complete Code Example
Sender Portlet
Liferay-portlet.xml file (/PortletSessionsIPCSender-portlet/docroot/WEB-INF/liferay-portlet.xml)
<?xml version="1.0"?> <!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd"> <liferay-portlet-app> <portlet> <portlet-name>PortletSessionsIPCSenderPortlet</portlet-name> <icon>/icon.png</icon> <private-session-attributes>false</private-session-attributes> <header-portlet-css>/css/main.css</header-portlet-css> <footer-portlet-javascript> /js/main.js </footer-portlet-javascript> <css-class-wrapper> portletsessionsipcsenderportlet-portlet </css-class-wrapper> </portlet> <role-mapper> <role-name>administrator</role-name> <role-link>Administrator</role-link> </role-mapper> <role-mapper> <role-name>guest</role-name> <role-link>Guest</role-link> </role-mapper> <role-mapper> <role-name>power-user</role-name> <role-link>Power User</role-link> </role-mapper> <role-mapper> <role-name>user</role-name> <role-link>User</role-link> </role-mapper> </liferay-portlet-app> |
View.jsp (/PortletSessionsIPCSender-portlet/docroot/html/portletsessionsipc/view.jsp)
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%> <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%> <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%> <portlet:defineObjects /> <liferay-theme:defineObjects /> <portlet:actionURL var="sendDataActionURL" windowState="normal" name="getData"> </portlet:actionURL> <h1>Portlet Sessions IPC Sender Portlet</h1> <aui:form action="<%=sendDataActionURL%>" method="post" name="smsForm"> <aui:input name="userEmailAddress" id="userEmailAddress" label="User Email Address"> <aui:validator name="required" /> <aui:validator name="email"></aui:validator> </aui:input> <aui:button type="submit" value="Submit"></aui:button> </aui:form> |
Portlet Action Class (PortletSessionsIPCSenderAction.java)
package com.meer.liferay.ipc; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import javax.portlet.PortletSession; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.util.bridges.mvc.MVCPortlet; public class PortletSessionsIPCSenderAction extends MVCPortlet { public void getData(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException, SystemException { String userEmailAddress = ParamUtil.getString(actionRequest, "userEmailAddress"); PortletSession portletSession = actionRequest.getPortletSession(); portletSession.setAttribute("userEmailAddress",userEmailAddress, PortletSession.APPLICATION_SCOPE); } } |
Receiver Portlet
Liferay-portlet.xml /PortletSessionsIPCReceiver-portlet/docroot/WEB-INF/liferay-display.xml)
<?xml version="1.0"?> <!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd"> <liferay-portlet-app> <portlet> <portlet-name>PortletSessionsIPCReceiverPortlet</portlet-name> <icon>/icon.png</icon> <private-session-attributes>false</private-session-attributes> <header-portlet-css>/css/main.css</header-portlet-css> <footer-portlet-javascript> /js/main.js </footer-portlet-javascript> <css-class-wrapper> portletsessionsipcreceiverportlet-portlet </css-class-wrapper> </portlet> <role-mapper> <role-name>administrator</role-name> <role-link>Administrator</role-link> </role-mapper> <role-mapper> <role-name>guest</role-name> <role-link>Guest</role-link> </role-mapper> <role-mapper> <role-name>power-user</role-name> <role-link>Power User</role-link> </role-mapper> <role-mapper> <role-name>user</role-name> <role-link>User</role-link> </role-mapper> </liferay-portlet-app> |
View.jsp (/PortletSessionsIPCReceiver-portlet/docroot/html/portletsessionsipc/view.jsp)
<%@page import="javax.portlet.PortletSession"%> <%@page import="com.liferay.portal.NoSuchUserException"%> <%@page import="com.liferay.portal.service.UserLocalServiceUtil"%> <%@page import="com.liferay.portal.model.User"%> <%@page import="com.liferay.portal.kernel.util.ParamUtil"%> <%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%> <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%> <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%> <portlet:defineObjects /> <liferay-theme:defineObjects /> <h1>Receive User Data from Sender Portlet</h1> <% String message=null; PortletSession portletSessionobject = renderRequest.getPortletSession(); String userEmailAddress=null; if(portletSessionobject.getAttribute("userEmailAddress", PortletSession.APPLICATION_SCOPE)!=null){ userEmailAddress = (String)portletSessionobject.getAttribute("userEmailAddress", PortletSession.APPLICATION_SCOPE); } User senderPortletUserOject=null; try{ if(userEmailAddress!=null){ senderPortletUserOject= UserLocalServiceUtil.getUserByEmailAddress(themeDisplay.getCompanyId(), userEmailAddress); } }catch(NoSuchUserException e){ message="No User exists with the given Email Address."; }catch(Exception e){ message="There is problem in view the user details."; } if(senderPortletUserOject!=null){ %> <table border="1"> <tr> <td>User Id</td> <td><%=senderPortletUserOject.getUserId()%></td> </tr> <tr> <td>First Name</td> <td><%=senderPortletUserOject.getFirstName()%></td> </tr> <tr> <td>Last Name</td> <td><%=senderPortletUserOject.getLastName()%></td> </tr> <tr> <td>Email Address</td> <td><%=senderPortletUserOject.getEmailAddress()%></td> </tr> <tr> <td>Screen Name</td> <td><%=senderPortletUserOject.getScreenName()%></td> </tr> </table> <%}else{%> <%=message%> <%}%> |
Portlet Action Class (PortletSessionsIPCReceiverAction.java)
package com.meera.liferay.ipc; import com.liferay.util.bridges.mvc.MVCPortlet; public class PortletSessionsIPCReceiverAction extends MVCPortlet { } |
0 comments:
Post a Comment