Wednesday, 24 June 2015

How to do Liferay IPC using Public Render Parameter

How to share data among Liferay Portlets. Liferay provides multiple ways to share the data between more than two portlets. IPC (Inter Portlet Communication) is one of the ways.

Lets see how can we share data using Public Render Parameter:

There are three simple steps available

1)  Modify portlet.xml file and add variable to be shared
2)  Modify controller class to add / access shared variable
3)  Redeploy portlet and verify changes

Here are the detailed steps for doing same:

1)  Modify portlet.xml file and add variable to be shared 

  Here is the code snippet for portlet.xml file
   
    <portlet-app>
    <portlet>
       <portlet-name>FirstPortlet</portlet-name>
        :
        :
        <supported-public-render-parameter>studentName</supported-public-render-parameter>
   </portlet>
   <portlet>
       <portlet-name>SecondPortlet</portlet-name>
        :
        :
        <supported-public-render-parameter>studentName</supported-public-render-parameter>
   </portlet>

<public-render-parameter>
        <identifier>studentName</identifier>
        <qname xmlns:x="http://www.liferay.com/public-render-parameters">x:studentName</qname>
       </public-render-parameter>
        </portlet-app>

    We have used two portlets FirstPortlet and SecondPortlet. Both the Portlet's <portlet> tag contains attribute "<supported-public-render-parameter>"
    where we need to add entry of the parameter shared parameter. If we dont specify "<support..." entry that portlet will not be able to access shared variable.
   
    Outside the <portlet> tag we need to specify "<public-render-parameter>" which specifies the list of all the shared parameters. In our case
    we have provided "x:studentName" as the shared parameter.
   
2)  Modify controller class to add / access shared variable    

        We have two portlets. First and Second. In below snippet we can see that FirstPortlet sets the shared variable inside processAction method.
        Second portlet reads the shared variables value inside render phase.
   
        FirstController
actionResponse.setRenderParameter("studentName", "Test");
       
        SecondController
        String name  = ParamUtil.getString(renderRequest, "studentName");
LOGGER.info("Details -> " + name);

3)  Redeploy portlet and verify changes

    Now lets build the portlet and deploy it. Drop both the portlets on the same portal page. Perform some action on FirstPortlet so that value of "studentName" gets set.
    During render method of the SecondPortlet read the value and print the value on console.

Here is the Output we can see in console:        
        06:53:16,062 INFO  [SecondPortlet:42] Details -> Test

Cheers! You are done.

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics

No comments:

Post a Comment