A year ago, the article Asynchronous Rendering of Portlet Content With Ajax Technology demonstrated how to apply Asynchronous JavaScript and XML (Ajax) to portlets. Since then, Ajax has become increasingly popular in the software arena and many new Ajax technologies have emerged. Examples are JavaScript libraries and toolkits, such as the Dojo Toolkit, the Yahoo! UI Library, the Google Web Toolkit, In light of the many developments in the past year and the host of feedback on how to use Ajax in portlets, this article describes several helpful tips and practices on how best to exploit Ajax in portlets that comply with the Java Specification Request (JSR) 168: Portlet Specification. The article refers to an updated version of the sample, Ajax Portlet Invoice Viewer, from the original article. You can download the binary Web archive (WAR) file.
The source
is available in the Open Source Portal
Repository on Contents
Calling Other Application Resources
A significant disadvantage of using Ajax with JSR 168 portlets is that you cannot make asynchronous calls to portlets through portlet URLs. The portlet JavaServer Pages (JSP) tag library offers simple tags for constructing portlet URLs, but the tags do not produce the desired results. Portlet URLs enable window state changes and mode changes that require a subsequent render request to the portal or portlet container. The result? The response contains not only the content for the target portlet, but also that for the entire portal, including all the other rendered portlets. Figure 1 shows an example of a Hello World! portlet that made an asynchronous call with
Currently, the expert group for JSR 286, which defines Portlet Specification 2.0, is evaluating solutions to better support portlets that make asynchronous calls with Ajax. In the meantime, solutions do exist. Here is one: Because a portlet is a Web application that can contain other resources, such as servlets and JSP pages, you can make the asynchronous requests to the resources that are bundled with the portlet. For instance, the Invoice Viewer sample includes a servlet that you can call to asynchronously render new content in the portlet. To take maximum advantage of this flexibility, the portlet and the servlet should be able to share information between themselves. How do you make that happen? Read on. Maintaining Portlet State
When you dynamically update portlet content with an Ajax call to a servlet, how do you maintain the portlet's state? The answer is readily at hand. Consider the following excerpt from the Portlet Specification 1.0 (PLT.15.4) in the JSR 168 specification: A Portlet Application is also a Web Application. The Portlet Application may contain servlets and JSPs in addition to portlets. Portlets, servlets and JSPs may share information through their session. Accordingly, while in the servlet, you can store stateful data in the For example,
Afterwards, the portlet's main JSP page (
Note: You must specify Figure 2 illustrates how a bundled portlet and servlet in an Ajax portlet share information with the
Using a JavaScript Library
Recall that you can use many emerging JavaScript libraries and toolkits within a portlet's Web application. This section focuses on the Dojo Toolkit. In June, Sun joined the Dojo Foundation. Dojo is a robust and mature open-source JavaScript library that offers numerous widgets for dynamic Web development. Most importantly for the sample, Dojo provides an intuitive wrapper for the
To recap the steps in the preceding code segment:
To learn more about Note: To optimize performance, standardize on one or a few JavaScript libraries in your portlets or portal. Otherwise, you must load multiple portlets, each containing different libraries, into the JavaScript evaluator on the client. Loading multiple portlets weighs down the client and negatively impacts performance. Understanding Namespace Collisions
If multiple portlets on a portal might include similar Ajax JavaScript functions and libraries, such as Dojo, consider the possibility of namespace collisions. This section discusses two problems that pertain to namespaces. Portlet Namespace
Be sure to uniquely name the variables and functions in multiple copies of an Ajax portlet on a portal by defining the
Dojo Namespace
The sample bundles the Dojo JavaScript library in its WAR file and, once deployed, references that library. However, other portlets on the portal might also use Dojo, but Dojo allows only one top-level reference for its reserved namespace. To alleviate this problem, before defining the Dojo namespace, verify that it is not loaded by checking the namespace and dynamically loading the Dojo library only when it is absent. Subsequently, the script dynamically adds JavaScript on the client when the content is loaded. For example, in the sample's main JSP page (
The script checks whether the Dojo namespace is already defined. If the answer is no, the script inserts a new tag in the Document Object Model (DOM) at the top of the portlet's main content. Note: In the case where you have already defined the Dojo namespace, that Dojo version applies. However, that version, which is loaded by another portlet, might differ from the one that is deployed with the portlet in question. Currently, no simple solutions exist to tackle this possible conflict. As a safeguard, deploy only one version of the Dojo library for your portlets or portal. Deploying the Sample
The sample is a JSR 168 portlet that installs into any portlet container, such as the reference implementation Apache Pluto. To deploy the sample, first ensure that you have a working version of Sun Java System Portal Server 7 (henceforth, Portal Server) on your system and then follow these steps:
Building the Sample
The In addition, you can load the sample source into a Java technology-based IDE, such as the NetBeans IDE. Conclusion
Indisputably, limitations and caveats exist in programming Ajax with respect to JSR 168 portlets. But should that preclude you from taking advantage of this exciting new technology? Until JSR 286 is available, bear in mind the best practices and guidelines described in this article when applying Ajax and the associated JavaScript libraries to portlets. References
|
| ||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||