ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button JSP and Servlets

Introduction to Jakarta Struts Framework


Web application development was once simple: there was an HTML page and a browser. That was it. No dynamic data, no business applications, no headaches. But that simplicity lasted for only a short time. Today we are faced with complicated Web and business application development using a plethora of technologies.

This article focuses on one aspect of that development process: how to use the Struts framework to assist in front end integration. Struts is an open source framework developed for encouraging an application architecture based on the Model-View-Controller (MVC) design paradigm, useful in building Web applications with Java servlet and Java Server Pages (JSP) technology.

This article assumes a working knowledge of JSP, servlets, custom tag libraries, and XML. Refer to some of my previous columns on JSP customer tag library development if you need to brush up on the basics. This column is Part One in a two-part series on using the Struts framework.

In The Beginning

Struts is a Model-View-Controller (MVC) framework compatible with Sun's J2EE platform and primarily based on servlet and JSP technology. The Struts framework has been around for about a year now and was originally conceived by Craig McClanahan. Struts is now part of the Jakarta project (which is part of ASF, the Apache Software Foundation). The official home for Struts is at http://jakarta.apache.org/struts. The framework has gained considerable attention over the course of the last year because of its ease of use and ability to fit today's developers' needs in building applications and building them fast. Struts combines Java servlets, Java Server Pages (JSP), custom tags, and message resources into a unified framework and saves the developer the time of coding an entire MVC model, a considerable task indeed.

The Struts package, even though it is a revision 1.0, has complete documentation. This includes a user's guide as well as developer's guides. If you are a new JSP programmer or aren't familiar with using the MVC design pattern, this might be a chunk to chew off in the beginning, but you will come up to speed quickly.

One thing important to note is that even though this is a 1.0 product, it is extremely stable. I have been using the Struts framework since the 0.9 version (and have recently updated to the 1.0 release) on a large-scale client project, and have not experienced any problems with it. In fact, it has saved our team a considerable amount of engineering time for the complex screen interactions of this particular project. Kudos to the contributing developers who work on the Struts project.

Comment on this articleIn general, what has been your experience with Apache's Jakarta projects?
Post your comments

Also in JSP and Servlets:

Learning the New Jakarta Struts 1.1, Part 2

Learning the New Jakarta Struts 1.1, Part 1

JSP Standard Tag Libraries, Part 2

Also importantly, if you are just starting out with the framework, the Struts' mailing list has a fair amount of activity, and can be found along with the other open source Jakarta projects at http://jakarta.apache.org/site/mail.html.

Bring on the Framework

The framework itself can be broken down into four primary areas, three of which correspond nicely to the MVC pattern:

  • The model, which is an Action class (that we'll discuss in a minute), provides the business logic written by the developer. The dispatch from the controller to the Action class is based on a configuration that is provided by a struts-config.xml file.

  • The view, which is a set of JSP custom tag libraries, works in concert with the controller servlet, and that allows for fast creation of forms for an application.

  • The controller, which is a servlet, dispatches incoming requests to an appropriate Action class.

  • A number of utility classes support XML parsing, automatic population of JavaBean properties, and internationalization of prompts and messages.

Let's examine the various parts of the framework and get a feel for the interaction of objects in the system.


Before being able to use Struts, you must set up your JSP container so that it knows to map all appropriate requests with a certain file extension to the Struts action servlet. This is done in the web.xml file that is read when the JSP container starts. While it's possible to define multiple controllers in the web.xml file, one for each application should suffice. A web.xml file that defines a simple configuration is below; the comments explain the content. We'll talk about the details of the struts-config.xml file when we get to the Action class.

Declare the OreillyAction servlet to be of type ActionServlet from the framework. Include the configuration file as defined in the config param. Set the debug level to 2 with a detail of 2 when loading the servlet. Create 2 instances.

  All incoming requests that end in .action, send to the OreillyActionServlet.
    <servlet-name> OreillyActionServlet </servlet-name>
  Send initial requests to the login page for this application
  Make all of the necessary related Struts JSP custom tag libraries
available and define where to find them.


The Controller

The controller is really the traffic cop of the framework and determines when things happen. The controller servlet is responsible for packaging and routing HTTP request to the appropriate object in the framework. This is either a JSP page or an Action class.

The controller is in the web.xml file as an instance of org.apache.struts.action.ActionServlet. In our sample above, this is the OreillyActionServlet. When the control is initialized, it reads a configuration file (struts-config.xml) that specifies action mappings for the application, as well as a slew of other things that we'll discuss later. The controller then uses these action mappings to determine where to send the HTTP request. The application action then takes over and performs whatever business logic is necessary. An important aspect to note is that Action objects have access to the servlet's methods. This is a powerful feature, because when the Action object forwards control to another object, one or more shared objects can be forwarded as well by adding them to one of the standard collections shared by Java servlets.

Pages: 1, 2

Next Pagearrow