Struts

Struts is a set of technologies for developing M - V - C user interfaces for Web-based applications, especially those supported by Servlet engines or Web Containers.  It was developed by the Apache Foundation, and their Struts site is a good place to start when looking for specs, downloads, and documentation, including APIs.

The basic approach is to use JSPs (Java Server Pages) as the view components; Actions function as the controller components and handle the actions associated with each view/JSP element; and FormBeans carry the data from the view to the action elements and back. The model component is outside of the basic framework, but is generally represented as a conventional Java class (POJO), and its methods are called by the action elements to accomplish the business function of the system.

One critical element of the architecture that is present but not visible to the developer is a central action controller that receives input from the various forms generated by the JSPs and passes control and relevant data to the appropriate action element.  All such interactions go through this "beneath the hood" component.  It also provides another important function --  calling a formbean's validate method to perform initial checks on entered data, such as a non-null value for a loginID or matching password and confirmPassword values.  If such a validation test fails, control and data are returned to the JSP without further processing.

Whereas Struts-based interfaces can be built for conventional Java server engine applications, they are often associated with J2EE systems.  Consequently, they are often built using sophisticated development environments, such as Eclipse and WebSphere Studio (WSAD).  One of the most attractive aspects of the Struts approach is that virtually all of the infrastructure and much of the code are generated automatically by these tools. Thus, developing a conceptual understanding of what the tools are doing is critical.

To understand Struts and to build a Struts application, you must be aware of a broad range of issues that extend from general architecture to setting up the basic infrastructure you will use.  Consequently, the discussions that follow will address Struts architecture, design, and implementation details as well as details for using both the Eclipse and the WSAD environments. A good introduction to Struts is provided by Apache's Struts User's Guide.


Architecture and Design

J2EE infrastructure/context

J2EE Layered Architecture

Conventional M - V- C design

JSP M - V - C Design

Struts M - V - C Design

Struts Diagram

Struts Cycle

Confucius Says . . .
 


Strategy

Describes a sequence of tasks for implementing M - V - C designs using Struts.  The rational for each task and an explanation for its position in the sequence are provided.


FormBeans

A basic Struts component, FormBeans are a special type of DataBean used to carry data from a JSP page/form to an Action control component.  In addition to the normal functions provided by beans, FormBeans also include a validate method that can be called to perform initial validation of data before data reach the Action..


Views as JSPs

Struts uses JavaServer Pages (JSPs) as view components.  they are normally used with several special-purpose JSP taglibs. Among other functions, the taglib simplify the HTML specification of input components, such as TextFields, and automatically transfer data from them to a FormBean associated with the page.


Actions

Actions are the basic components Struts uses to support Controller function.  They closely resemble Servlets, but are invoked through an execute method that individual actions must override to provide function.  They return an ActionForward object that identifies the name of the next object to which control is passed.  These names are general and are bound to actual objects through the struts-config.xml file.


Struts-config.xml

The whole struts set of components is tied together through the special struts-config.xml file.  Several different types of configuration information are explained and an example of a simple complete configuration file is shown.


Eclipse/Tomcat Setup

For building Struts applications in Eclipse, a special Struts plugin is required.  For testing and running Struts applications in Tomcat, a special collection of Struts .jar and configuration files are needed.  No special setup is required for WebSphere Studio since Struts is an integral part of its Web perspective. 


WebSphere Studio Checklist

A checklist for creating a Struts M - V - C system using WebSphere Studio.


Resources