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 ...
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.
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); }
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 }