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

Accessing a Form Bean in the Servlet
Struts actions have a nice way of collecting all the HTML form elements using an Action form bean. Behind the scenes, the Struts Framework binds all the HTML form elements to a form bean as per the action mapping specified in the Struts configuration file.

Through an AJAX request you can use a similar mechanism using a BeanUtils package when you're posting a form to a servlet so you don't have to do a request.getParameter() for each element value. For more information on the BeanUtils package see the Apache Struts Framework Web site.

Here's the call to AJAX request posting the HTML form:

AJAXEngine.request({
     "url" : url,
     "title" : "Side Menu",
     "responseFunction" : receiveLinkResults,
     "postBody" : serializeContentForm(),
     "method" : "POST"
});

function serializeContentForm() {
     var url="";
     url+= "filter="+encodeURIComponent(document.getElementById("filter").value);
     url+= "&selectType=sideMenuContent";
     url+="&namespace="+this.namespace;
     return url;
}

On the server, you can retrieve form elements posted as below:

ContentBean contentBean = new ContentBean();
try {
   BeanUtils.populate(contentBean,arg0.getParameterMap());
} catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
       e.printStackTrace();
} catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
System.out.println("Filter="+contentBean.getFilter());
System.out.println("SelectType="+contentBean.getSelectType());

We need to make sure that the ContentBean has the properties specified in the HTML form. In our case, we used the same Action form bean used by the Struts portlet.

Session Sharing
Once you define the servlets in the same portlet application, you can share session objects among the portlets, JSP, and servlets. The only requirement is to make sure that the session objects are declared with APPLICATION_SCOPE.

About the Sample Code
The sample code is a downloadable zip file with a strutsAJAXportlet.war file in it and can be downloaded from the online version of this article at http://ajax.sys-con.com You can install the war file in the WebSphere Portal server and put a portlet on any portlet page to see it work. If you install the war file in IBM's Rational Application Developer you'll find the source code for the client-side AJAX engine, JavaScript functions for request and response handling, and the server-side portlet and servlet for request processing.

Conclusion
Many organizations today want to leverage existing assets to embrace new technologies. The Struts Framework is a very popular MVC pattern implementation and very suitable for large enterprise applications. The Struts Portlet Framework is an extension of Struts that's supported by all major releases of WebSphere Portal. In this article, I explained how to blend the Struts Portlet Framework with AJAX. With AJAX on the client side it can work with any server-side framework. There are several other initiatives to get AJAX working with Java Server Faces (JSF), JSON-RPC, and Java-like programming so that frameworks can convert it into client-side script.

References

Apache Struts home page: http://struts.apache.org/
IBM Redbook: IBM Rational Application Developer V6 Portlet Application Development and Portlet tools:
Executing Struts actions during the render phase of IBM WebSphere Portal:
Error validation and exception handling in portlets, using the Struts Portlet Framework:
Developing JSR168 Struts portlets:

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.