The Struts framework provides several components that make up the Control
layer of a MVC-style application. These include a controller servlet,
developer-defined request handlers, and several supporting objects.
The Struts custom tag libraries provide direct support for the View layer
of a MVC application. Some of these access the control-layer objects.
Others are generic tags found convenient when writing applications. Other
can also be used with Struts. Other presentation technologies, like
Velocity Templates and
can also be used with Struts.
The Model layer in a MVC application is often project-specific. Struts is
designed to makes it easy to access the business-end of your application,
but leaves that part of the programming to other products, like
Enterprise Java Beans,
Object Relational Bridge, or
to name a few.
Let's step through how this all fits together.
When initialized, the controller parses a configuration
struts-config.xml) and uses it to deploy other control layer objects.
Together, these objects form the Struts Configuration. The Struts Configuration
defines (among other things) the
org.apache.struts.action.ActionMappings] for an application.
The Struts controller servlet consults the ActionMappings as it routes HTTP
requests to other components in the framework. Requests may be forwarded to JavaServer Pages or
org.apache.struts.action.Action] subclasses provided by the
Struts developer. Often, a request is first forwarded to an Action and then to a JSP
(or other presentation page). The mappings help the controller turn HTTP requests into application
will usually contain a number of properties including:
- a request path (or "URI"),
- the object type (Action subclass) to act upon the request, and
- other properties as needed.
The Action object can handle the request and respond to the client (usually a Web
browser) or indicate that control should be forwarded elsewhere. For example, if
a login succeeds, a login action may wish to forward the request onto the
Action objects have access to the application's controller servlet, and so have access
to that servlet's methods. When forwarding control, an Action object can indirectly
forward one or more shared objects, including
JavaBeans, by placing them in one
of the standard contexts shared by Java Servlets.
For example, an Action object can create a shopping cart bean, add an item to the
cart, place the bean in the session context, and then forward control to
another mapping. That mapping may use a JavaServer Page to display the contents of the user's cart.
Since each client has their own session, they will each also have their own
In a Struts application, most of the business logic can be
represented using JavaBeans. An Action can call the properties of a JavaBean
without knowing how it actually works. This encapsulates the business logic,
so that the Action can focus on error handling and where to forward control.
JavaBeans can also be used to manage input forms. A key problem in designing
Web applications is retaining and validating what a user has entered between
requests. With Struts, you can define your own set of input bean classes, by
org.apache.struts.action.ActionForm]. The ActionForm class makes it
easy to store and validate the data for your application's input forms.
The ActionForm bean is automatically saved in one of the standard, shared context
collections, so that it can be used by other objects, like an Action object or
The form bean can be used by a JSP to collect data from the user ... by an
Action object to validate the user-entered data ... and then by the JSP again to
re-populate the form fields. In the case of validation errors, Struts has a
shared mechanism for raising and displaying error messages.
Another element of the Struts Configuration are the
This is a collection of
that are used to create instances of the ActionForm objects at runtime.
When a mapping needs an ActionForm, the servlet looks up the form-bean descriptor by name and uses
it to create an ActionForm instance of the specified type.
Here is the sequence of events that occur when a request calls for an mapping that uses an ActionForm:
- The controller servlet either retrieves or creates the ActionForm bean instance.
- The controller servlet passes the bean to the Action object.
- If the request is being used to submit an input page, the Action object
can examine the data. If necessary, the data can be sent back to the input
form along with a list of messages to display on the page. Otherwise the data can
be passed along to the business tier.
- If the request is being used to create an input page, the Action object can
populate the bean with any data that the input page might need.
The Struts framework includes custom tags that can automatically populate
fields from a JavaBean. All most JavaServer Pages really need to know
about the rest of the framework is the field names to use and where to submit
Other Struts tags can automatically output messages queued by an Action
or ActionForm and simply need to be integrated into the page's markup.
The messages are designed for
and will render the best available message for a user's locale.
The Struts framework and its custom tag libraries were designed from the ground-up
to support the internationalization features built into the Java platform. All the field labels
and messages can be retrieved from a
To provide messages for another language, simply add another file to the resource bundle.
Internationalism aside, other benefits to the message resources approach are consistent labeling
between forms, and the ability to review all labels and messages from a central
For the simplest applications, an Action object may sometimes handle the business logic
associated with a request. However, in most cases, an Action object should
invoke another object, usually a JavaBean, to perform the actual business logic.
This lets the Action focus on error handling and control flow, rather than
business logic. To allow reuse on other platforms, business-logic JavaBeans should not refer to any Web
application objects. The Action object should translate needed details from the
HTTP request and pass those along to the business-logic beans as regular Java
In a database application, for example:
- A business-logic bean will connect to and query the database,
- The business-logic bean returns the result to the Action,
- The Action stores the result in a form bean in the request,
- The JavaServer Page displays the result in a HTML form.
Neither the Action nor the JSP need to know (or care) from where
the result comes. They just need to know how to package and display it.
Other chapters in this document cover the
various Struts components in greater detail. The Struts release also
includes several Developer Guides covering various aspects of the
frameworks, along with sample applications, the standard Javadoc API,
and, of course, the complete source code!
Struts is distributed under the Apache Software Foundation license. The code
is copyrighted, but is free to use in any application. See the
ASF license for specifics.