Foren

Inter-Portlet Communication between two portlets on different pages

Praneeth Chinta, geändert vor 13 Jahren.

Inter-Portlet Communication between two portlets on different pages

New Member Beiträge: 4 Beitrittsdatum: 07.10.10 Neueste Beiträge
Hi Everyone,

Here is my Use case.

1. I have a portlet "sender" on Page A which has 5 links
2. I have another portlet receiver on Page B which wants to know the link clicked by the user on the Page A.
3. Now when user on Page A clicks a link on sender portlet I wan to publish an event and redirect the page to Page B.
4. When page B loads I want the receiver portlet to consume that event and show the name of the link selected on sender portlet.

I am able to achieve this by using <portlet:action-url> with params and shared session attributes perfectly.However, Ideally I want to achieve this by eventing mechanism.

Could you suggest the standard way of doing this??

More Details:
Liferay version: 6.0.4
JDK v1.6
Tomcat v6.0.26
The sender and receiver portlets are in two different wars.
I have already tried using standard events and changing portal-ext.properties with "portlet.event.distribution=ALL_PORTLETS".
I am able to do the communication if the portlets are in the same page.
I have also gone through all the blogs and threads related to this but could not figure out a way.
Praneeth Chinta, geändert vor 13 Jahren.

RE: Inter-Portlet Communication between two portlets on different pages

New Member Beiträge: 4 Beitrittsdatum: 07.10.10 Neueste Beiträge
I have found a workaround, though its not a clean solution, have a look and let me know any better way to do it.

In the sender portlet on Page A, in the processAction() method I have published an event and redirected the page to B.

From view.jsp of Sender portlet

<div><a href="<portlet:actionURL name=" sendname"><portlet:param name="name" value="John" />"&gt;John</a></div>
<div><a href="<portlet:actionURL name=" sendname"><portlet:param name="name" value="Jack" />"&gt;Jack</a></div>
<div><a href="<portlet:actionURL name=" sendname"><portlet:param name="name" value="Henry" />"&gt;Henry</a></div>
<div><a href="<portlet:actionURL name=" sendname"><portlet:param name="name" value="Hobbs" />"&gt;Hobbs</a></div>
<div><a href="<portlet:actionURL name=" sendname"><portlet:param name="name" value="Dexter" />"&gt;Dexter</a></div>



From SenderPortlet's Class "processAction()"

	String name = request.getParameter("name");
	QName qName = new QName("http://liferay.com/events", "ipc.name");
	response.setEvent(qName, name);
	response.sendRedirect("<page_b_url>");
</page_b_url>


In the reciever Portlet which is on Page B,
Consumed the event in processEvent method and set a session attribute in the portlet's scope.Used the session attribute in the view.jsp of Reciever Portlet to show the link selected.

From RecieverPortlet's class "processEvent()"

        Event event = request.getEvent();
	String name = (String)event.getValue();
	request.getPortletSession(true).setAttribute("name",
			name,
			PortletSession.PORTLET_SCOPE);

From view.jsp of receiver portlet

&lt;%
String name = (String)renderRequest.getPortletSession(true).getAttribute("name",PortletSession.PORTLET_SCOPE);
%&gt;
You have selected <br> <span id="fav"><b>&lt;%=name%&gt;</b> </span>


Can you people suggest me a better way or any changes needed in the above code?
Deepak Kenchamba, geändert vor 13 Jahren.

RE: Inter-Portlet Communication between two portlets on different pages

New Member Beiträge: 3 Beitrittsdatum: 12.10.10 Neueste Beiträge
The implementation proposed above is good. We could tidy it up a bit more.
  • Instead of directly accessing the PortletSession attribute in the jsp, i would prefer accessing it in the render() method of the portlet & use it to setup the appropriate Model which inturn can be used within the jsp.
  • Also, it would be a good practice to clear the PortletSession attribute after use, unless there is a reason to intentionally retain it.
thumbnail
ankit yakkundi, geändert vor 13 Jahren.

RE: Inter-Portlet Communication between two portlets on different pages

Regular Member Beiträge: 221 Beitrittsdatum: 05.03.10 Neueste Beiträge
Hi..
I am using liferay 6 with plugin sdk.
I want to do the same thing which you all have done.
Can i get the complete source code of the project ie java,jsp,xml,properties,jar and also the war file ,etc file.
I want to study it in detail for my project.

Its little urgent,can anyone send me the files.

Thanks in advance...
thumbnail
ritresh girdhar, geändert vor 10 Jahren.

RE: Inter-Portlet Communication between two portlets on different pages

Junior Member Beiträge: 67 Beitrittsdatum: 15.07.11 Neueste Beiträge
Please check IPC between two portlet on different pages.

IPC1.java
package com.test;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.xml.namespace.QName;

/**
* Portlet implementation class IPC1
*/
public class IPC1 extends GenericPortlet {

public void init() {
viewJSP = getInitParameter("view-template");
}

@Override
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println(" IPC1 : : processAction ");
QName qName=new QName("http://liferay.com", "test","x");
response.setEvent(qName, "Juventus testing");
response.sendRedirect("/web/guest/ipc2");
}
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(viewJSP, renderRequest, renderResponse);
}

protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {

PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String viewJSP;

private static Log _log = LogFactoryUtil.getLog(IPC1.class);

}


************************************************************
IPC2.java

package com.test;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.io.IOException;

import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
* Portlet implementation class IPC2
*/
public class IPC2 extends GenericPortlet {

public void init() {
viewJSP = getInitParameter("view-template");
}

@Override
public void processEvent(EventRequest request, EventResponse response)
throws PortletException, IOException {

javax.portlet.Event event = request.getEvent();
String value = (String)event.getValue();
System.out.println( " IPC2 ::: processEvent "+value);
}

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

include(viewJSP, renderRequest, renderResponse);
}

protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {

PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String viewJSP;

private static Log _log = LogFactoryUtil.getLog(IPC2.class);

}

***************************************************************************
portlet.xml

<?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>ipc1</portlet-name>
<display-name>IPC1</display-name>
<portlet-class>com.test.IPC1</portlet-class>
<init-param>
<name>view-template</name>
<value>/html/ipc1/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>IPC1</title>
<short-title>IP C1</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-publishing-event xmlns:x='http://liferay.com'>
<qname>x:test</qname>
</supported-publishing-event>
</portlet>
<portlet>
<portlet-name>ipc2</portlet-name>
<display-name>IP C2</display-name>
<portlet-class>com.test.IPC2</portlet-class>
<init-param>
<name>view-template</name>
<value>/html/ipc2/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>IP C2</title>
<short-title>IP C2</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-processing-event xmlns:x='http://liferay.com'>
<qname>x:test</qname>
</supported-processing-event>
</portlet>
<event-definition xmlns:x='http://liferay.com'>
<qname>x:test</qname>
<value-type>java.lang.String</value-type>
</event-definition>


</portlet-app>


*****************************
portal-ext.properties

portlet.event.distribution=ALL_PORTLETS



Regards
Ritresh
Liferay Developer
Juventus Software