A key Role in the Initial Growth of the World Wide Web

Apache Web Server Journal

Subscribe to Apache Web Server Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Apache Web Server Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Apache Web Server Authors: Elizabeth White, Liz McMillan, Pat Romanski, Janakiram MSV, Gil Allouche

Related Topics: RIA Developer's Journal, Apache Web Server Journal

RIA & Ajax: Article

Struts Portlets with AJAX in Action

Challenges faced during implementation

JavaScript Objects
AJAX applications use the XMLHttpRequest Object and client-side JavaScript, and as these applications become more complex it becomes difficult to maintain the code using plain JavaScript functions. Object-oriented programming is ideal and proven for such complex applications. JavaScript offers many different ways of defining objects, but it's not a full-fledged object-oriented programming language. There are several efforts by different people aimed at providing workarounds for object-oriented programming features such as inheritance, reflection, and interfaces using JavaScript.

JSON (JavaScript Object Notation) provides guidance on JavaScript object notation. In our example, we have used JSON guidelines and defined all the JavaScript functions as objects. We have also defined all the functions as external js files so that all the client-side logic is kept modular and separate from the JSP file, following portlet best practices.

Here is the JavaScript code snippet from the downloadable sample application defining an object and calling the function as object instance methods.

if (typeof(contentHelper) === 'undefined'){
     var contentHelper = [];
}
contentHelper['<%=contentAppNamespace%>'] = new ContentHelper({
     namespace : "<%=contentAppNamespace%>",
     urlContextPath : "<%=request.getContextPath()%>"
});

I defined all other JavaScript functions in different js files. Below is the definition of the 'ContentHelper' function.

function ContentHelper(params){
     //Parameter Variables
     if (!params){
       alert('Missing parameters while instantiating!');
       return {};
     }
     if (typeof(WebFormUtils)=='undefined'){
       alert("The utils package is not included!");
       return;
     }
     var namespace = params.namespace;
     var urlContextPath = params.urlContextPath;
     var formUtils = new WebFormUtils({
     namespace : namespace
});
var AJAXurls = new AJAXURLs({
     namespace : namespace,
     urlContextPath : urlContextPath
});

var AJAXEngine = new AJAXEngine({
     redirectHandlerFunction : formUtils.handleAJAXRedirect,
     retryPromptMessage : "Warning, there was a problem
     during an attempt to communicate with the server\n"
     +
     "Would you like to retry? (It's recommended
     to wait a bit first)\n" +
     "If the problem persists, please contact Help
     Desk",
     autoRetries : 2,
     retryDelay : 10000
});
.....
.....
}

The object/function above in turn instantiates other objects such as AJAXUrls and AJAXEngine.

Handling Namespace with an AJAX Response
As specified in Figure 3, Struts portlets make asynchronous AJAX requests to servlets. Servlets process the request and send the response back to the client, which in turn is suppose to refresh certain parts of the portlet JSP page. Portlets being namespace-aware, it's very important that any Web page refresh be namespace-aware. Unfortunately, the servlet is not aware of the portlet namespace. To resolve this issue we have passed namespace as a parameter for every AJAX request so that the servlet response can prefix all object ids with the appropriate namespace.

More Stories By Kris Vishwanathan

Kris Vishwanathan is an IT Architect who has been with IBM since 2003. He worked as a senior developer and architect for IBM Systems and Technology Group before joining IBM Software Services for Lotus at the end of 2004. He has been part of the delivery team implementing WebSphere Portal and Web Content Management (WCM) solutions. In addition to architecting solutions for clients, he also sets up portal production environments involving portal clustering, Tivoli Access Manager, LDAP, user registry configurations, and portlet development using JSR168, JSF, IBM API, and WCM API. He is an IBM Certified WebSphere Portal System Administrator and Solution Developer.

Comments (3)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.