Struts 2.0 in action

Custom tags, themes, and code reuse aid developer productivity

Whether or not you've used version 1 of the Struts Web development framework, you should check out Struts 2.0 to see if it will be useful for your MVC-based projects. In this article, Venkatray Kamath walks you through the high points of a Struts 2.0 application, demonstrating how it can help boost your productivity.

A Web-based application is a crucial part of any enterprise solution, and to develop such an application, you need a good Model-View-Controller (MVC) framework in place. For Java Web-based applications, there are lots of frameworks that you can choose from: Struts, Spring MVC, WebWork, and Tapestry are some of the most popular, and version 2.0 of the Struts framework has recently been released by the Apache Foundation.

This article will explore the new Struts 2 framework and attempt to highlight the core features that you can take advantage of in implementing your own Web application. This article doesn't attempt to compare the various MVC frameworks available; rather, I will focus on sharing my experiences in working with Struts 2 that will help you get started with the framework.

You'll need to have a good understanding of how an MVC framework works to quickly grasp the contents of this article. However, you don't specifically need background in version 1.x of the Struts framework. If you're already familiar with Struts 1.x, or looking to migrate code that was built for that framework, you might want to read "Adopting Struts 2.0," an JavaWorld article written specifically to compare the two versions of the framework (see the Resources section for a link).

Struts: An architectural overview

In this article, you'll walk through the implementation of a sample application to really get a feel for how Struts 2 works and what it can do for you. Before diving into these specifics, though, you should get a bird's-eye view of the framework's architecture. Figure 1 offers a visual outline of how Struts 2 works.

Architecture of the Struts 2 framework
Figure 1. Struts 2 architecture (click for a larger image)

As you can see in Figure 1, an initial request from the browser goes to the servlet container. The Struts FilterDispatcher is a standard servlet filter that intercepts this request. FilterDispatcher delegates control to the ActionProxy class, which consults the framework configuration files manager; this is initialized from struts.xml, a configuration file that stores the request-URL-to-Action-class mapping and lots of other information that you'll learn more about later on in this article.

The interceptors that implement common concerns across actions are then called (in the before() method) in advance of invoking the Action itself. Interceptors built into Struts 2 can perform core processing, like populating request parameters into Action classes, performing validation, uploading files, and so on. You can also define custom interceptors, as you'll see when you examine this article's sample application.

The Action class typically invokes the business layer and populates the model objects, which are instances variables of the Action class. Next, the request is dispatched to the view layer (which is built on a technology like JavaServer Pages, FreeMarker, or Velocity), which renders the GUI. The interceptors are executed again (in reverse order, calling the after() method). Finally, the response returns through the servlet filter chain.

This all probably seems pretty abstract at the moment, but you can get a better feel for how it works in practice by taking a look at a sample application.

1 2 3 4 5 Page 1
Page 1 of 5