We already know IPC using public render parameters .Today we will see how to do IPC using Events. Events based IPC was introduced in JSR 286 or we can say in Portlet 2.0. According to this a new method processEvent introduced in Life Cycle of Portlet.Here one Portlet can Produce a event which can be consumed by other portlets.
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 but for events there may be one sender and multiple receivers.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 event_sender and click finish. After that Right Click on your project ie event_sender and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click finish.
Note:-You can check the snapshots in my blog on Portlet Configuration Page in Liferay.
Step 2:-Change portlet.xml
Open portlet.xml and enter supported publishing event as:-
Explanation:-
Focus on these lines:-
This file contains hidden or 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-publishing-event> | |
<qname xmlns:food="http://aditya/events">food:foodType</qname> | |
</supported-publishing-event> | |
</portlet> | |
</portlet-app> |
Explanation:-
<supported-publishing-event>
<qname xmlns:food="http://aditya/events"> food:foodType
</qname>
</supported-publishing-event>
Here Supported Publishing event tell that this portlet can generate event with name foodType. The Qualified name(qname) is used so that parameter name space properly to resolve conflict in case where portlet have two events with same name.
Step 3:-Change view.jsp
Open view.jsp and paste this content:-
This file contains hidden or 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> |
Step 4:-Change the Controller
This file contains hidden or 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 javax.xml.namespace.QName; | |
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")) | |
{ | |
//We fetch Qname from portlet.xml and set value in it | |
QName foodEvent = new QName("http://aditya/events", "foodType"); | |
response.setEvent(foodEvent, foodType); | |
} | |
} | |
} |
Explanation:-
Here we fetch the Qname from portlet.xml and produce a event.
For Receiver
Step 1:-Create Liferay Project and Portlet
Similarly as in Sender Create project with name event-receiver and create a portlet in it.
Step 2:-Change portlet.xml
Similarly as in Sender change portlet.xml .After Security-role-ref tag paste this:-
<supported-processing-event>
<qname xmlns:food="http://aditya/events"> food:foodType
</qname>
</supported-processing-event>
Note:- In Sender we use supported publishing event but in receiver we use supported processing event.
Step 3:-Change the Controller
Open your Controller and paste this:-
Explanation:-
1)By using the annotation @ProcessEvent we can create method with any name otherwise we have to use default processEvent method.
2)Here First we get the event and than value from the event which is set by sender portlet .
3)Finally we set value in request so that it can be get in view.jsp
Similarly as in Sender change portlet.xml .After Security-role-ref tag paste this:-
<supported-processing-event>
<qname xmlns:food="http://aditya/events"> food:foodType
</qname>
</supported-processing-event>
Note:- In Sender we use supported publishing event but in receiver we use supported processing event.
Step 3:-Change the Controller
Open your Controller and paste this:-
This file contains hidden or 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 javax.portlet.Event; | |
import javax.portlet.EventRequest; | |
import javax.portlet.EventResponse; | |
import javax.portlet.ProcessEvent; | |
import com.liferay.util.bridges.mvc.MVCPortlet; | |
public class Receiver extends MVCPortlet { | |
@ProcessEvent(qname = "{http://aditya/events}foodType") | |
public void receiveFoodType(EventRequest request, EventResponse response) { | |
Event event = request.getEvent(); | |
String foodType = (String) event.getValue(); | |
request.setAttribute("foodType", foodType); | |
} | |
} |
1)By using the annotation @ProcessEvent we can create method with any name otherwise we have to use default processEvent method.
2)Here First we get the event and than value from the event which is set by sender portlet .
3)Finally we set value in request so that it can be get in view.jsp
Step 4:-Change the jsp
Open view.jsp and paste this :-
This file contains hidden or 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> |
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 . Better you use session or Public render Parameters for IPC because when processEvent method completes in receiver it called doView.
So when portlet is on some other page and you click on navigation bar to change the page only doView of receiver portlet is called and not processEvent.
No comments:
Post a Comment