Monday, June 2, 2008

Struts 2 - A Basic Introduction

Struts 2 came after struts started losing its popularity over the last couple of years. I previously used struts 1 in my project and found out there were no updates to the framework for a very long time. This was one of the major reasons for developers to move onto other frameworks. Then came Struts 2 !
Struts 2 is basically a light weight web framework. It is an extension of Struts 1. Struts 2 was originally called Web Works 2 , developed by Open Symphony. Apache and Open Symphony merged Struts and Web Works 2 and named it as Struts 2. Struts 2 also includes a validation framework called XWorks which is again developed by Open Symphony.

In my view, Struts 2 is very similar to Struts 1. For people who know struts 1, this should be a cakewalk. Some of the best features of struts 2 are POJO action forms, POJO action classes, Spring Integration, superb support for Ajax, servlet independent interfaces. Lets see some of the salient features in detail.

Action Class
Struts 2 supports a base action class called ActionSupport to implement commonly used interfaces. Struts 2 action can also implement the Action interface instead of extending from ActionSupport. This gives programmers lots of flexibility as using interfaces is always an advantage. Well, in struts 2, you don't even have to implement the Action interface ! Any POJO which has an execute method can be used as an action class ! Basically this means I can use any class as Action class ! There is no action form in Struts 2. We can use any JavaBean to capture values from input.

HTTP Independent Framework Interfaces
Struts 2 actions are not coupled to the container unlike in Struts 1. By this what I mean is that, struts 2 action classes need not know the HttpRequest and the HttpResponse objects. Now this is an excellent feature because this allows Action classes to be tested in isolation. That means, we can test struts 2 actions by just instantiating it, setting properties and invoking methods ! However, we can still access the Request and the Response objects by implementing the interfaces RequestAware and ResponseAware respectively. The request and the response objects are available as simple Maps to the action class. Similarly, we can also access the session object by implementing the interface SessionAware.

Threading Model
There are no thread safety issues in Struts 2 as action objects are instantiated for each and every requests. Apache claims that "servlet containers generate many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection".

Interceptors
Interceptors execute code before and after the action class is invoked. Think of the possibilties ! Double submit guard, pre-populating objects ( action properties ), type conversions, preparing a page before its being loaded, validations, file uploads and lots more !

Action Forms Eliminated !
Struts 2 maps the action properties to the input properties (Html form elements) and eliminates the need for another class (ActionForm). Action properties can be accessed in the web page using struts 2 tag library. However, we can also use ActionForm pattern. The action properties can be rich object types like value objects and these can be used to map input elements in the JSP.

Apart from all the above features, struts 2 is integrated with Dojo toolkit to provide first-class support for Ajax. To check out the capabilities of struts 2, download the Struts 2 Showcase Application (struts-2.0.11.1-apps.zip) and host it in Tomcat Server.

Now that you know good things about struts 2, let me come to the not so good things part. I have used struts 2 extensively in my project and observed that, the page rendering is slower that what it was in struts 1. I also saw some performance hits in the application. We could eliminate some of them by tweaking certain features in the struts.xml lik, but I was still not convinced with the page rendering speed. Worse, I struggled a lot to find those tweaks ! which indicates that there is less support from communities when it comes to struts 2. The only good forum I saw was the struts-user group in Nabble.

I leave you with this image which gives an analogy between struts1 and struts2.
In my next blog, I would discuss about the Struts 2.0 architecture and a "Hello World" tutorial.

Note: Struts, Webwork and Struts2 logos are courtesy of Apach Foundation and OpenSymphony.