The Portlet 1.0 specification has serious limitations when it comes to inter-portlet communication, but not everyone is ready to jump to Portlet 2.0. In this addition to the JavaWorld Portlet Packet, author Sandeep Tol shows you how to use the Dojo toolkit to enhance communication between portlets based on Portlet 1.0. Level: Intermediate
In the world of Java portlet programming, inter-portlet communication, also called portlet-to-portlet communication, allows multiple portlets to respond to events triggered by other portlets. For example, a product setup portlet and a product configuration portlet might communicate with each other to update data on a third portlet. Many portal platforms allow developers to code event handlers or JMS requests to achieve this. In addition, asynchronous content rendering for specific portlets is possible using portal server features. However, these techniques require coding and configuring on the server, and are tightly coupled to specific vendor implementations. They also require a server request for very event.
In contrast, with Ajax you can dynamically update the data displayed by a portlet without submitting an action request or refreshing the page. You can also enable client-side inter-portlet communication without relying on different server-side IPC implementations. By using the open source Dojo JavaScript toolkit, you can simplify coding, reduce lines of code, and make scripts easily to understand. This article will help you build portlets that can dynamically update the data displayed without submitting an action request or refreshing the page, and also enable client-side inter-portlet communication without relying on different server-side IPC implementations. This article describes how to use Ajax for client-side inter-portlet communication using the Dojo JavaScript toolkit. You'll also learn how to render the data asynchronously on client side using Dojo. This article is intended for developers who are beginning to use Ajax with Java portlets. To get the most out of this article, you should have a good understanding of JavaScript programming, the Dojo toolkit, a Java portal server, and portlet development. Dojo is an open source JavaScript library that offers numerous scripts, widgets, and the like for dynamic Web development. Most importantly, Dojo provides an wrapper for the Dojo includes an event communication system that allows you to handle application events. This article uses the event publish/subscribe system, known as topics, for communication between portlets. There are three functions that you need to understand to use Dojo's topic system: Here's the syntax for these functions: Most of the magic of the That's all the code you need to include! You provide the URL from which you want to get the data, and a callback function that you'd like to have called when you actually get the data back. You can also handle potential exceptions as shown in Listing 2. Figure 1 gives an overview of the example you'll consider in this article. You can download the package that contains the complete code for this application, and follow along with it as you walk through the discussion that follows. The Product Setup portlet displays a drop-down menu from which you can choose different configurations. Once the user selects a configuration, an event will be fired and a message sent to the listener of the application's other portlet, which then displays the selected configuration. In the next few sections, you'll walk through the code that performs this task. Because Dojo is a JavaScript API, you first must load dojo.js, as in Listing 3. For this code to work, you'll need to ensure that the Dojo files are available in the server context path.
About Dojo
XMLHttpRequest
object. The sample discussed in this article uses the dojo.io.bind()
function call to make an asynchronous call to dynamically update a portlet, and uses the dojo.event.topic.publish()
and dojo.event.topic.subscribe()
functions for inter-portlet communication.
Dojo event handling
dojo.publish()
, dojo.subscribe()
, and dojo.unsubscribe()
. dojo.publish()
calls any functions that are connected to a topic via dojo.subscribe()
, passing to those subscribed functions arguments that are published. As you might expect, dojo.unsubscribe()
will cause a previously subscribed function to no longer be called. dojo.publish(Topic Name [string], Arguments to Pass to Subscribed Function [array])
handle = dojo.subscribe (Topic Name [string], Context of Linked Method [string or null], Linked Method [string or function])
dojo.unsubscribe (Handle [handle object])
Dojo bind()
dojo.io
package is exposed through the bind()
method. dojo.io.bind()
is a generic asynchronous request API that wraps multiple transport layers (queues of iframes, XMLHTTP, mod_pubsub, LivePage, and more). Dojo will pick the best available transport mechanism for a request at runtime. To make a request that returns raw text from a URL, you would call bind()
as illustrated in Listing 1. Listing 1. Calling bind()
dojo.io.bind ({
url: "http://localhost:9080/Ipcdemo/myportlet",
handle: function(type, data, evt)
{ /*do something w/ the data */ },
mimetype: "text/plain"
});Listing 2. Exception handling in bind()
dojo.io.bind({
url: "http://localhost:9080/Ipcdemo/myportlet",
handle: function(type, data, evt)
{
if (type =="error")
{
/*print the error message */
},
/*do something w/ the error*/
},
mimetype: "text/plain"
});Building an inter-portlet communication example
Figure 1. The example application in action (click to enlarge)
Publishing an event
Listing 3. Loading dojo.js
src="<%= renderRequest.getContextPath() %>/dojo/dojo.js">
Inter-portlet communication with the Dojo toolkit
Labels: IBM WebSphere Portal 6.1
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment