+

Search Tips   |   Advanced Search

 

Passing properties to other portlets

 

This topic describes how portlets can exchange properties. You should be familiar with the concepts for launching console pages before reading this topic.

Information can be provided to portlets on the same page using the addSharedPortlet() method. Property values are of the type com.ibm.portal.propertybroker.property.PropertyValue. The example in Launching pages shows these properties passed to the page using an instance of the PropertyValue interface (propertyValues) and shows how that object was created using the PropertyFactory interface.

The SetServer.java source from the PortletContext sample shows how to pass properties to a portlet on the same page. It is assumed that the portlet has already performed a JNDI lookup to determine that the PropertyFactory, DynamicUIManagerFactoryService, and URLGeneratorFactoryService services are available. This is described in Launching pages

Context ctx = null;
ObjectID portletDefinitionID1 = null;
ObjectID portletDefinitionID2 = null;

String portletname1="com.ibm.isclite.samples.PortletContext/PortletGetPerformance";
String portletname2="com.ibm.isclite.samples.PortletContext/PortletGetApps";

PortletSession ps  = request.getPortletSession(false);
        
try
   {
      ctx = new InitialContext(com.ibm.portal.jndi.Constants.ENV);
      portletDefinitionID1 =
         (ObjectID)ctx.lookup("portal:config/portletdefinition/"+portletname1);
      portletDefinitionID2 =
         (ObjectID)ctx.lookup("portal:config/portletdefinition/"+portletname2);   } catch (NamingException ne)
     {
        logger.log(Level.FINE, "portletdefinitionID not found - Naming exception:"+ne.getMessage());
        return;
     }
logger.log(Level.FINE, "portletdefinitionID="+portletDefinitionID1.toString());    
        
try
{
   PropertyController cproperty = 
      propertyFactoryService.createProperty(myconfig);
   cproperty.setType("String");

   PropertyValue[] propertyValues = new PropertyValue[1];
   propertyValues[0] = 
      propertyFactoryService.createPropertyValue(request, cproperty, serverName);


DynamicUICtrl dmanagerCtrl = dynamicUIManagerFactoryService.getDynamicUICtrl(request, response, "isc.tasklaunch"); ObjectID newPortletID1 = dmanagerCtrl.addSharedPortlet(portletDefinitionID1, null, propertyValues); ObjectID newPortletID2 = dmanagerCtrl.addSharedPortlet(portletDefinitionID2, null, propertyValues); logger.log(Level.FINE, "portlet ID created:"+newPortletID1.getOID()); } catch ...

In some cases, the target portlet might be on a separate page. In this case, the properties are passed using the addPage() method. The target portlet receives the properties only when the page is launched that contains the portlet. If a property value is set multiple times before the page is launched, the value that was set last for the property is passed to the portlets on the page. You can use the URLGeneratorFactoryService interface to redirect the console to the page for the target portlet. This is demonstrated in the GetApps portlet of the PortletContext sample.

ObjectID newPageID = dmanagerCtrl.addPage(pageDefinitionID, null, propertyValues);
RedirectURLGenerator urlGenerator = urlGeneratorFactoryService.getURLGenerator(request, response);
EngineURL redirectURL = urlGenerator.createPageURL(newPageID);
response.sendRedirect(redirectURL.toString()); 

To use the URLGeneratorFactoryService, the portlet must first perform a JNDI lookup for this portlet service. Refer to the GetApps.java source to see how to obtain a reference to the portlet service.

 

Receiving properties

To receive properties, the target portlet must provide the com.ibm.portal.pagecontext.enable preference parameter in the portlet.xml with a value of true. If the portlet should receive any subsequent updates, the com.ibm.portal.context.enable read-only preference should also be set to true. Only String property types are supported and the context is passed as parameters of the action request. The following example shows how the GetApps portlet receives properties passed by the SetServer portlet.

public void processAction(ActionRequest request, ActionResponse response) 
       throws PortletException, IOException {

        PortletSession ps = request.getPortletSession(true);
        appDeployed = request.getParameter("appDeployed");
        serverName=request.getParameter("servername");
        
        ps.setAttribute("servername",serverName);
        ps.setAttribute("appDeployed",appDeployed);
        launchPage(request, response);

    }


A target portlet can check com.ibm.portal.action parameter of the request during action processing to determine if any properties have been passed. If properties are being passed to the portlet, the value of this parameter is com.ibm.portal.pagecontext.receive. For example:

 String action = req.getParameter(com.ibm.portal.action.name);
 if (action!=null && action.equalsIgnoreCase("com.ibm.portal.pagecontext.receive")) {

   // code to get the properties as a parameter on the request

 }




 

Related information


Launching pages