Friday, February 10, 2012

Interportlet communication in different war files (portlet applications) – Part 1

Inter-portlet communication between multiple portlets in different portlet applications has been made easy with JSR 286 portlet API. It has brought a new revolution in portlet development. In JSR 168 a.k.a portlet specification 1.0, inter-portlet communication between portlets in different war files was not possible. The only way to achieve it was through IBM URL Generation classes. But JSR 286 gives you more flexibility to achieve this functionality.
There are 2 ways to achieve interportlet communication between portlets in different portlet applications.
1. By using public render parameters of JSR 286 portlet development API.
2. By using processEvent() method.
I will be discussing the first method in this article. The second approach will be discussed in the next article. In the first method you can pass only string params while the second one gives you flexibility to pass complex collection objects. I have created sample applications for both approaches.
The sample application uses IBM websphere portal 7, JSR 286 portlet development API, Annotation based Spring MVC Portlet framework 3.1. There are 2 portlets applications PortletApplication 1 which has TestPortlet1 and PortletApplication2 which has TestPortlet2.
Listing 1: TestPortlet1Controller.java
@Controller
@RequestMapping("VIEW")
public class TestPortlet1Controller{
@RenderMapping
protected ModelAndView defaultHandleRenderRequest(RenderRequest request,RenderResponse response) throws Exception {
ModelAndView mav = new ModelAndView("list1");
return mav;
}
@ActionMapping(params = "action=insert")
public void insert(ActionRequest request, ActionResponse response) {
String name = request.getParameter("name");
response.setRenderParameter("name", name);
}
}
Listing 1.1 TestPortlet1.xml:
<portlet>
<supported-public-render-parameter>name</supported-public-render-parameter>
<public-render-parameter>
<identifier>name</identifier>
<qname xmlns:customns="http://testportlet1_params/">customns:name</qname>
</public-render-parameter>
</portlet>
Listing 2: TestPortlet2Controller.java
@Controller
@RequestMapping("VIEW")
public class TestPortlet2Controller{
@RenderMapping
protected ModelAndView defaultHandleRenderRequest(RenderRequest request,
RenderResponse response) throws Exception {
ModelAndView mav = new ModelAndView("list2");
String name = request.getParameter("name");
System.out.println("TestPortlet2Controller========"+name);
mav.addObject("name",name);
return mav;
}
}
Listing 2.1 TestPortlet2.xml:
<portlet>
<supported-public-render-parameter>name</supported-public-render-parameter>
<public-render-parameter>
<identifier>name</identifier>
<qname xmlns:customns="http://testportlet1_params/">customns:name</qname>
</public-render-parameter>
</portlet>

8 comments:

  1. This is what I have was looking for since long. JSR 286 with Spring MVC and Websphere portal 7

    ReplyDelete
  2. Is it possible to provide the sample source code?

    ReplyDelete
  3. I have included the sample code already. Just put this in RAD and it should work. Let me know if you face any issues.

    ReplyDelete
  4. Any examples of receiving the WCM render Parameters in a user created portlet. And how to get WCM to share these with other portlets on the page?


    Shared path-info parameter of WebSphere Portal
    PATH_INFO
    wcm:path-info


    WCM public context
    PUBLIC_CONTEXT
    wcm:context

    ReplyDelete
  5. What exactly are you looking for? Do you want to share content from wcm to share between 2 portlets?

    ReplyDelete
  6. no I want my portlet to display data related to the current WCM portlet. For example in the CTC3 version there is a conference sample with sessions. I want to have my portlet display data related to the session that the user navigated to. So in this sample the user chooses the program then the track and then the session I want to have a portlet that knows the path and the context to display this info. Which I get from the public render parameters. I figured this out, except some wcm pages in this sample can be navigated to from WCM portlets and portal navigation. In those cases there are no public render parameters so I guess I'll need another way to dynamically find out what the current WCM content is on the portal page. Thx

    ReplyDelete
  7. Hi

    Please provide event based communication example. communication between two different wars and send complicated data(not String value ,Bean class object) between portlets.

    Thanks,
    Sathish

    ReplyDelete