Inter Portlet Communication


Image

 

There are 3 ways to share the data between 2 portlets.

        1.  Portlet session

        2.  IPC Mechanisms

               Public Render Parameters

   Event

               Client-Side IPC

         3.Cookies

 

Example:

Event Inter Portlet Communication:

Step:1 

           Create two portlet  one is Sender  portlet  and  another one is Receiver portlet.

Step:2

        Go to your  portlet location open the sender portlet folder.Then   edit the Portlet.xml file(docroot/WEB-INF/Portlet.Xml)

 

   <?xml version="1.0"?>

<portlet-app

            version="2.0"

            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 >

 

           

 

             <portlet>

                        <portlet-name>sender</portlet-name>

                        <display-name>Sender Portlet</display-name>

                        <portlet-class>com.liferay.portlet.SenderPortletAction</portlet-class>

                        <init-param>

                                    <name>view-jsp</name>

                                    <value>/html/sender/view.jsp</value>

                        </init-param>

                        <expiration-cache>0</expiration-cache>

                        <supports>

                                    <mime-type>text/html</mime-type>

                        </supports>

                        <portlet-info>

                                    <title>Sender Portlet</title>

                                    <short-title>Sender Portlet</short-title>

                                    <keywords>Sender Portlet</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-publishing-event>

                                    <qname xmlns:x="http://liferay.com">x:userDetails</qname>

                        </supported-publishing-event>

            </portlet>

</portlet-app>

 

Step3: 

            Create a  SenderPortletAction.java .      Under      src/com/liferay/portlet dir.

package com.liferay.portlet;

import javax.portlet.ActionRequest;

import javax.portlet.ActionResponse;

import javax.xml.namespace.QName;

import com.liferay.portal.kernel.util.ParamUtil;

import com.liferay.util.bridges.mvc.MVCPortlet;

public class SenderPortletAction extends MVCPortlet {

public void addform(ActionRequest actionRequest, ActionResponse actionResponse){

                         QName qName = new QName("http://liferay.com", "userDetails", "x");

                         String username=ParamUtil.getString(actionRequest, "username");

                       

                       

 

 actionResponse.setEvent(qName, username);

                           }

            }

 

Step:4

          Create a view.jsp under docroot/html/sender/view.jsp dir.

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<%@page import="javax.portlet.PortletURL"%>

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@page import="javax.portlet.PortletSession"%>

<portlet:actionURL name="addform" var="updateURL">

</portlet:actionURL >

<aui:form action="<%=updateURL%>"  method="post" name="fm" >

<h1>IPC: Sender portlet</h1>

<aui:input label="User Name"  name="username" type="text"/><img title="Please Enter the     User Name"  style="margin-left:74px;position:relative;top:-43px" src="/html/themes/control_panel/images/portlet/help.png"/>

<aui:button-row>

            <aui:button type="submit" />

</aui:button-row>

</aui:form>

 

 

Step:5

        Go to your portlet location open the receiver portlet folder.Then   edit the Portlet.xml file(docroot/WEB-INF/Portlet.Xml)

 

<?xml version="1.0"?>

 

<portlet-app

            version="2.0"

            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>

 

            <portlet>

                        <portlet-name>receiver</portlet-name>

                        <display-name>Receiver</display-name>

                        <portlet-class>com.liferay.portlet.ReceiverPortletAction</portlet-class>

                        <init-param>

                                    <name>view-jsp</name>

                                    <value>/html/receiver/view.jsp</value>

                        </init-param>

                        <expiration-cache>0</expiration-cache>

                        <supports>

                                   

                    <mime-type>text/html</mime-type>

                        </supports>

                        <portlet-info>

                                    <title>Receiver</title>

                                    <short-title>Receiver</short-title>

                                    <keywords>Receiver</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-processing-event xmlns:x="http://liferay.com">

                                    <qname>x:userDetails</qname>

                        </supported-processing-event>

            </portlet>

</portlet-app>

 

 

Step:6 

    Create  a  ReceiverPortletAction.java  Under    src/com/liferay/portlet dir.

package com.liferay.portlet;

import java.io.IOException;

import javax.portlet.Event;

import javax.portlet.EventRequest;

import javax.portlet.EventResponse;

import javax.portlet.PortletException;

import com.liferay.util.bridges.mvc.MVCPortlet;

public class ReceiverPortletAction extends MVCPortlet {

 

 public void processEvent(EventRequest request, EventResponse response)

                                    throws PortletException, IOException {

                        Event event = request.getEvent(); 

                        response.setRenderParameter("USERNAME", (String) event.getValue());

            }         

}

 

Step:7

       Create a view.jsp under docroot/html/receiver/view.jsp dir.

 

 

 

 

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<%@page import="javax.portlet.PortletURL"%>

 

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@page import="javax.portlet.PortletSession"%>

<%

String username= renderRequest.getParameter("USERNAME");

%>

<h1>IPC: Receiver portlet</h1>

The UserName is: <%=username %>

 

Step:8  

           Then, ant-deploy  for both  portlets.

It’s not about ideas It’s about making ideas happen !!!

Image Image Image