What is Struts?

If you've developed with ASP, JSP or Servlets you've probably had to maintain web applications that are a tangled mess of HTML, scripting code, data access and business logic. And if you're asked to add internationalization to an already complex web app you might be tempted to give up programming and become a tech analyst instead.

The answer to our web woes is separation. This means splitting an application into parts with distinct functions and well-defined protocols for communication between them. This way we can maintain or work on some parts without having to worry about others.

Struts is a Java based framework that helps you achieve this separation Zen.

Certainly, if you're planning to deploy simple pages - no data access needed, no complex business rules and no internationalization - Struts is probably overkill. If some of these requirements are met then your application would probably benefit from using Struts.

Where do I start?

Just to be sure we're all on the same page I'll assume you know Java. You should know (at the barest minimum) what servlet technology is about even if you've not used it.

Some experience with either JSP, ASP or PHP is also necessary if only to appreciate the problems that Struts solves (or doesn't solve). If you haven't any experience with these I'd recommend investing some time learning the basics of JSP.

Overview

One natural way of splitting an application is with the "Model-View-Controller" paradigm:

Model
Parts which handle data access and persistence.
View
Parts that handle user interaction and display of information.
Controller
Parts which handle validation, business logic and generally a go-between Model and View.

In Struts:

Models
are any old Java class that handles data access.
Views
are JSP pages but with a twist: Struts uses custom tags that help you factor out scripting code you'd normally embedded in JSP, to place them into Model and Controller portions of your app.
Controllers
fall into 2 categories: form validation is done by subclasses of the Struts ActionForm while business logic is handled by subclasses of the Struts Action class. When the user submits a form, control goes to the ActionForm subclass for validating which then passes the validated form to the Action subclass for processing business logic.

Besides these there are 3 static files:

  1. Java Resource files which end with the extension .properties. These are files you create to store static data shared between Model, View and Controller. For simple applications you can make do with just one and we'll be calling ours Application.properties.
  2. A configuration file to rule them all: struts-config.xml. This helps Struts piece together your web application.
  3. A required JSP configuration file called web.xml. For simpler applications you can use a simple "Struts-enabled" web.xml file without change for your applications.

In Summary

To write a typical Struts application you'll need to:

  1. Centralize data access into a few Java classes (that is, Models).
  2. Write your JSP pages, taking care to hide your text in Application.properties, to make internationalization easier.
  3. Write subclasses of ActionForm to handle form validation.
  4. Write subclasses of Action to handle business logic.
  5. Tie everything together by writing struts-config.xml.
  6. Make changes (if any) to the default web.xml file.
  7. Deploy your app onto your servlet container.

With all that theorizing I'm sure you're dying to get your hands dirty. In the next section you'll see how to install Struts and after that, you'll learn to create a toy Struts web application.

Web Resources

  1. Apache foundation's Jakarta site, where you'll find Struts and other open-source goodies.
  2. Valuable information on servlets.
  3. A JSP primer.
  4. A few good books on JSP.