Saturday, September 15, 2007

Introduction to Struts 1.x

Some years ago, I was asked to add a new feature to an existing struts-based application. The feature would include some form validation, form pre-population as well as some embedded logic depending on the authenticated user. Double submissions were also a big concern and had to be detected and avoided. These, among others, are many of the tasks where Struts can be extremely helpful. Sure, there are many things that experienced developers will find as shortcomings (just as any other tool has weaknesses), yet Struts continues to grow from these and is now a well-defined Java Framework for building Enterprise Applications using a Model-View-Controller (MVC) architecture.

One thing to remember is that Struts is not the only solution, is only one of the many. Specifically, it's one commercially popular in the Java programming language. There are other solutions available such as WebWork (now merged with Struts to make Struts2), Spring MVC, Swing, JSF (which is now the official MVC specification from Sun MicroSystems), Stripes, JFace, OpenBravo, etc. Alternatively, other programming languages (such as Ruby on Rails) have an MVC approach of their own. In general, it is the pattern (MVC), not the tool (Struts), that we must remember brings success to an Enterprise Application.

If you are a newbie, be aware that Struts is now branched in two different projects: Struts1 and Struts2. The first one aims to continue development on the original Struts Framework. Struts2 is the new approach for MVC development as developers from the Struts and WebWork2 (OpenSymphony) communities joined forces to provide a cleaner (and simpler) way of developing web-based applications. You may want to consider choosing one first (as suggested on their official site) if you have never used either version as both bring to the table fundamental differences. Unfortunately, as a newbie you are going to need to have some understanding of basic technologies. Although the Struts documentation suggests some of these, I would list the following as the key technologies to understand:

  • HTTP Request/Response Cycles
  • Properties Files and Resource Bundles
  • Servlets, Filters, and Web Containers (e.g. Tomcat, Resin, Weblogic, WebSphere)
  • Java Server Pages Technology (JSPs, Tags)
  • XML
  • Model-View-Controller Pattern
Reading about these shouldn't be difficult for the average developer. The challenge will come later when a developer uses Struts in a real case scenario. It is fundamentally important to remember that Struts is a tool and not the "only" tool to generate Enterprise Applications. Often times, I've found source code where developers have misused Struts by forcing their many features to address a business problem already solved by other technologies (e.g. JSP, Filters, etc). Start with small sample projects and work your way up. For the curious newbie, I've found the JAD decompiler (while developing using Eclipse) to be a great tool for development of Struts Application using your favorite IDE (so you can look at the needed source). This source code is available anyway, but with this tool you can view only what you need, when you need it. Tracing the ActionServlet Servlet object in the org.apache.struts.action package can prove to be a very insightful experience... Enjoy!

Before Struts, your would have to extract each parameter of a request using a "getParameter()" method of the ServletRequest object. Struts encapsulates this with their ActionForm object, thus learning the life cycle of this class is essential when dealing with incoming HTTP requests. Also, prior to Struts, you would also have to write certain logic to learn how to forward a request to another Servlet for further processing. Struts solves this with their ActionServlet and one or more XML configuration files that drive the application flow. Double submissions of HTML Forms were handled by some hack in between Servlet calls. Struts solves this problem by using a "token-per-request" approach and the methods isTokenValid(), resetToken() and saveToken(). In general, Struts provides an Object Oriented API which helps visualize these problems and many more as objects.

In my experience, learning about the Model-View-Controller pattern is the key to using Struts successfully. This understanding, coupled with adherence to good and useful programming practices (which I plan to address in a different blog entry) will help you appreciate Struts better. So read about this and other practices and use this framework to do what is meant to do: " create/help web applications utilize an MVC architecture" (Reference)


Anonymous said...


I'm very interested in your opinion of Struts 1 vs. Struts 2.

I have used Struts 1 before, and it seemed to do the job fine on a very large enterprise application.

I've been looking into Struts 2 and it seems way too complicated. I tried to learn XWork first but there was nothing except a bad tutorial on the opnesymphony site. It was useless.

So I moved on to Struts 2 tutorials and documentation and it was also very bad.

I'm considering sticking with Struts 1.

It's seem good enough to get the job done for most things.

Alberto said...

If you are starting a new application from scratch (or have the budget and resources to do so), then I would recommend to take the time to learn it.

When Struts2 joined with XWork, the MVC framework gained a lot of good practices from both. One of my favorite features are Dependency Injection, Annotations, Multiple Submit button processing within a form, Ajax integration and JSF integration. It's built using jdk 5 as a requirement, which is what you should be using to use the best of the JVM. Thus far, I like it very much since I see I find myself solving less problems as I would with Struts 1.x.

Granted, documentation is not the best, but by looking at the source, it should be very evident how things are managed. This is how I have been figuring a lot of my hard questions.

Let me know if you have specific questions.