Inter portlet communication in same and different WAR files


Inter Portlet Communication

(portlets reside in same Application and different Application)
______________________________________________________________________________
There are mainly two ways using
      1.            Public render parameter (PRP)
      2.            Portlet Events
      3.            URL Generation API
Public render parameters:
a)      when two portlets are in same application( same WAR):
            create two portlets, Portlet1 and Portlet2, one will set the PRP  and other will consume it.
            create a PRP in portlet.xml with a name  TEXT and Identifier as TEXT and choose default namespace



and then subscribe this PRP to both portlets1 and portlet2
in portlet1 process action set PRP by using any Action event from UI
lets take the following use case

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
            // Get value when UI action occured
             String text = request.getParameter(“text”);
             
             // Setting PRP
             response.setRenderParameter(“TEXT”, text);
    }
In portlet portlet2 display the text that is placed in PRP TEXT
as follows
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
             // Set the MIME type for the render response
             response.setContentType(request.getResponseContentType());
             
             // Reading  PRP
             //request.setAttribute(“TEXT”, request.getParameter(“TEXT”));
             
             // Invoke the JSP to render, replace with the actual jsp name
             PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(“/jsps/Test2.jsp”);
             rd.include(request,response);
     }
b)      when two portlets are in DIFFERENT applications ( Different WARs):
         Create two different Portlet Projects Test1 and Test2
            In Test1 create portlet1 similar to above case
create portlet2 in Test2 application
All you need to do is…
create the PRP TEXT in both portlet.xml’s of Test1 and Test2
with same Name ,Identifier and NameSpace
Portlet Events:
a)      when two portlets are in same application( same WAR):
            Consider the same use case
insted of setting render parameter set Event
public void processAction(ActionRequest request, ActionResponse response)
 throws PortletException, java.io.IOException {
            //Read text from UI
 String text = request.getParameter(“TEXT”);
             
             // Set Event
             response.setEvent(“TestEvent”, text);
    }
create event “TestEvent” with name TestEvent value type java.lang.String


            subscribe portlet1 in Test1 and portlet2 in Test2 to the event TestEvent and set Enable portlet1 to publish event. and set Enable the portlet2 to listen to the event.
In poertlet 2 process the event as follows
public void processEvent(EventRequest request, EventResponse response)
             throws PortletException, IOException {
             Event event = request.getEvent();
             if(event.getName().toString().equals(“TestEvent”)){
                         response.setRenderParameter(“TEXT”, (String)event.getValue());
             }
    }
            public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
             // Set the MIME type for the render response
             response.setContentType(request.getResponseContentType());
             
             request.setAttribute(“TEXT”, request.getParameter(“TEXT”));
             
             // Invoke the JSP to render, replace with the actual jsp name
             PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(“/jsps/Test2.jsp”);
             rd.include(request,response);
    }
Wire these two portlets in WPS to the event
b)      when two portlets are in DIFFERENT applications ( Different WARs):
Here all you need to do is..
make sure that create same Event in  two portlet.xml’s
with same Name and value.
      3.            We can also do the Inter portlet communication using urlGeneration(Diffrent WAR’s)
           
contentNode is page unique Name where target portlet resides
by passing params through URL
as follows
            <%! String url; %>
<div>
            <portal-navigation:urlGeneration contentNode=”com.avnet.Test”
                         portletWindowState=”Maximized”>
                        <% url= wpsURL.toString(); %>
            </portal-navigation:urlGeneration>
   
            <form action='<%=url%>’>
                         Enter Text : <input name=”TEXT”> <input type=”submit”
                         value=”Submit”>
            </form>
</div>
           
In portlet2 we will recive the param as
             //URL PARAMS
             HttpServletRequest req = PortletUtils.getHttpServletRequest(request);
             request.setAttribute(“TEXT”, req.getParameter(“TEXT”));

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks