Sample JSR 168 Application in ASP.NET
One benefit of portals is the ability to perform integration at the UI layer, letting a portal page update multiple back-end systems simultaneously. To enable this, it is essential that portlets share data. WebSphere inter-portlet communication is facilitated by a broker that lets portlets exchange similarly typed properties. Essentially, the source portlet must define a WSDL file that specifies an output message (see Listing Six), while target portlets must define a WSDL file that specifies the related input message format (Listing Seven).
The WSDL files are fairly standard except within the operation node. Referring to Listing Six, you must define a named action and named parameter of the ActionRequest to which you attach your data as a named attribute. Within your .NET code, the data is applied to the attribute (Listing Eight).
<types> <xsd:schema targetNamespace="http://www.ibm.com/wps/search"> <xsd:simpleType name="SearchType"><xsd:restriction base="xsd:string"></xsd:restriction></xsd:simpleType> </xsd:schema> </types> <message name="Search"><part name="Text" type="tns:SearchType"/></message> <portType name="Service"><output message="tns:Search"/></operation></portType> <binding name="Binding" type="tns:Service"> <portlet:binding/> <operation name="Search"> <portlet:action name="Action" type="standard" caption="action" description="Search text" actionNameParameter="ACTION_NAME"/> <output><portlet:param name="demo_text" partname="Text" boundTo="request-attribute" caption="text"/></output> </operation> </binding>
<types> <xsd:schema targetNamespace="http://www.ibm.com/wps/search"> <xsd:simpleType name="SearchType"><xsd:restriction base="xsd:string"></xsd:restriction></xsd:simpleType> </xsd:schema> </types> <message name="Search"><part name="Text" type="tns:SearchType"/></message> <portType name="Service"><operation name="Result"><input message="tns:Search"/></operation></portType> <binding name="Binding" type="tns:Service"> <portlet:binding/> <operation name="Result"> <portlet:action name="Action" type="standard" caption="action" description="Search text" actionNameParameter="ACTION_NAME"/> <input><portlet:param name="demo_text" partname="Text" caption="text"/></input> </operation> </binding>
PortletRequest pr = vmw.portlet.PortletUtils.getPortletRequest(); ActionRequest ar = pr as ActionRequest; if (ar != null) { string actionName = ar.getParameter("ACTION_NAME"); if (actionName == "Action") { ar.setAttribute("demo_text", data); } }
The target portlet reads the data back from the ActionRequest (Listing Nine). The sample application demonstrates how to write two portlets that cooperate. The first portlet provides a search text box and a submit button (Search.aspx). Once clicked, the portlet loads a new page (SearchResults.aspx) with a DataGrid displaying the result of the search. In a real-world application, this would probably involve a web service call or database look-up; however, for simplicity the sample code simply loads some XML from a file of test data. Initially, the second portlet is empty, but when the user selects a row in the DataGrid, a number of values from the selected row are broadcast via the property broker and displayed in the page LinkedData.aspx. If the second portlet interrogates another database using this data, you have a simple method of integrating two systems at the UI layer.
ActionRequest ar = vmw.portlet.PortletUtils.getPortletRequest() as ActionRequest; if (ar != null) { string actionName = ar.getParameter("ACTION_NAME"); if (actionName == "Action") { string demoText = ar.getParameter("demo_text").ToString(); } }
Once the portlets are deployed, they must be "wired" up. This process is an administrative task that defines to the WebSphere property broker the particular output message that the source portlet data is to send to the target portlet. The wiring tool is described in the WebSphere Portal documentation.
To run the sample, you need Visual Studio 2003 and a copy of Mainsoft for Java EE, Portal Edition (trial versions are available). The sample app (available online at www.ddj.com/code/) includes a Visual Studio.NET Java EE Portal project consisting of two simple portlets demonstrating inter-portlet communication as described in this article.