Showing posts with label Inter Portlet Communication(IPC). Show all posts
Showing posts with label Inter Portlet Communication(IPC). Show all posts

Inter Portlet Communication(IPC) in Liferay


Inter Portlet Communication ie IPC is needed when we have to communicate between two portlets. Some times these portlets are on same page sometimes they may be on different page. 

Example:- We have a drop down in one portlet by which we can select country name and another portlet display the currency, states, national flag, climate on the basis of country selected by first portlet. In this case we have to send value from first portlet to another.


We can achieve IPC by following ways:-

1)IPC using Portlet Session
IPC by Portlet session work for both scenario ie Sender and Receiver portlet are on same page or different page. You can read more in my blog IPC by Portlet Session.


2)IPC using Public Render Parameter
IPC by public render parameter work if both portlet are on same page. But by adding one property in portal-ext.properties:-

portlet.public.render.parameter.distribution=layout-set

You can add portlet on different page also.For more detail you can read IPC using Public Render parameter.


3)IPC using Events
Events is introduced in Portlet 2.0  by which you can do IPC in very easily. Here the two portlets are on same page only. For more details you may refer IPC using Events.


4)Client Side IPC
Liferay provide two methods one for Sender(Liferay.fire) and one for receiver(Liferay.on) by which you can achieve IPC. In this case portlets must be on same page. For More details you may read  Client Side IPC in Liferay.





Hope this will Help....


Related Post:-

Client Side Inter Portlet Communication in Liferay


Today we will discuss Client Side Inter Portlet Communication in Liferay . In my opinion it is the simplest way for sharing data between two portlets which are on same page. Liferay provide two basic methods for this:-

1)For Client

Liferay.fire('eventName',{
            parameter1: value1,
            parameter2: value2

    });

2)For Receiver

 Liferay.on('eventName',function(event) {
   var firstValue = event.parameter1,
   var  secondValue = event.parameter2

});

Note:- Their may be one sender and many receiver but sender and all the receiver are must be on the same page . 

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.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 client-side-sender and click finish. After that Right Click on your project 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 view.jsp 
Open view.jsp and paste this:-


Explanation:-

  • On click of button we fetch the value of selected radio button and set in foodType.
  • We give the eventName as selectedFoodType which is used to fetch the data in receiver.


Note:-Change <requires-namespaced-parameters> is false inside liferay-portlet.xml.






For Receiver

Step 1:-Create Liferay Project and Portlet
Similarly as in Sender Create project with name client-side-sender and create a portlet in it.



Step 2:-Change view.jsp
Open view.jsp and paste this:-



Explanation:-
Here we get the value from event and on the basis of input we show the output.


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




Inter Portlet Communication (IPC) using Events in Liferay


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:-

<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:-

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:- 



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


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 . 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.

Inter Portlet Communication (IPC) using Public Render Parameter in Liferay


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.

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 public render parameter tag and public render parameter tags :-

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>

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:-


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:-

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:-

Inter Portlet Communication (IPC) by Portlet Session in Liferay


Inter Portlet Communication can be done by many ways like by using events,public render parameters and Portlet Session. Today we discuss about IPC by using Portlet Sessions. Here we set some value in one portlet and get value in other Portlet.

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
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as Session_Sender and click finish.

Step 2:-Create MVC Portlet inside project
Right Click on your project ie Session_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 3:-Change liferay-portlet.xml
Open liferay-display.xml and set private session attribute to false so that attribute set in session available to different wars:-

<private-session-attributes>false</private-session-attributes>

Also we are not using AUI so set required namespace parameter to false:-

<requires-namespaced-parameters>
                    false
</requires-namespaced-parameters>

Now your liferay-portlet.xml become:-



Step 4:-Change view.jsp
Open view.jsp and paste this content:-


Explanation:-
Creating a simple form with two radio button on the submittion of form processFood method is called.


Step 5:-Change the Controller
Open your java class and paste this:-


Explanation:-
Here we fetch the value from form and set in Session Object with Application Scope so that it is available to whole application.We have one more Scope called Portlet scope.






For Receiver

Step 1:-Create Liferay Project
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as Session_Receiver and click finish.

Step 2:-Create MVC Portlet inside project
Right Click on your project ie Session_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 3:-Change liferay-portlet.xml
Open liferay-display.xml and set private session attribute to false as in Sender.

Step 4:-Change your Controller
Open your java class and paste this:-


Explanation:-
Here we fetch the value from session that is set by the Sender and again set in renderRequest so that it is available in view.jsp.


Step 5:-Change view.jsp
Open view.jsp of receiver as:-


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 or different page. I am adding Sender on one page and Receiver on other.

Here i am adding Sender on Welcome page and select Vegetarian and click Submit:-


The Receiver Portlet is on about page:-



Now again go to and select Non- Vegetarian and click submit and see the Receiver as:-