"Read the directions and directly you will be directed in the right direction."
The framework documentation is written for active web developers and assumes a working knowledge about how Java web applications are built. For more about the underlying nuts and bolts, see the Key Technologies Primer.
When Java servlets were first invented, many programmers quickly realized that they were a Good Thing. They were faster and more powerful that standard CGI, portable, and infinitely extensible.
But writing HTML to send to the browser in endless
statements was tiresome and
problematic. The answer to that was
Now developers could easily mix HTML with Java code, and
have all the advantages of servlets.
The sky was the limit!
Java web applications quickly became "JSP-centric". This in-and-of itself was not a Bad Thing, but it did little to resolve flow control issues and other problems endemic to web applications.
Clearly, another paradigm was needed ...
Many clever developers realized that JavaServer Pages AND servlets could be used together to deploy web applications. The servlets could help with the control-flow, and the JSPs could focus on the nasty business of writing HTML. In due course, using JSPs and servlets together became known as Model 2 (meaning, presumably, that using JSPs alone was Model 1).
Of course, there is nothing new under the Sun ... and many have been quick to point out that JSP's Model 2 follows the classic Model-View-Controller design pattern abstracted from the venerable Smalltalk MVC framework. Java Web developers now tend to use the terms Model 2 and MVC interchangeably. In this guide, we use the MVC paradigm to describe the framework architecture, which might be best termed a Model 2/MVC design.
The Apache Struts Project was launched in May 2000 by Craig R. McClanahan to provide a standard MVC framework to the Java community. In July 2001, version 1.0 was released, and IOHO, Java Model 2 development has never been quite the same.
The term "MVC" originated with the SmallTalk Model-View-Controller framework. Under MVC, an application is seen as having three distinct parts. The problem domain is represented by the Model. The output to the user is represented by the View. And, the input from the user is represented by Controller.
The Model portion of an MVC-based system can be often be divided into two major subsystems -- the internal state of the system and the actions that can be taken to change that state.
In grammatical terms, we might think about state information as nouns (things) and actions as verbs (changes to the state of those things).
Many applications represent the internal state of the system as a set of one or more JavaBeans. The bean properties represent the details of the system' state. Depending on your application's complexity, these beans may be self contained (and know how to persist their own state), or they may be facades that know how to retrieve the system's state from another component. This component may be a database, a search engine, an Entity Enterprise JavaBean, a LDAP server, or something else entirely.
Large-scale applications will often represent the set of
business operations as methods that can be called on the
bean or beans
maintaining the state information. For example, you might
have a shopping
cart bean, stored in session scope for each current user,
that represent the current set of items that the user has
purchase. This bean might also have a
that authorizes the user's credit card and sends the order
warehouse to be picked and shipped. Other systems will
available operations separately, perhaps as Session
In a smaller scale application, on the other hand, the
operations might be embedded within the
classes that are
part of the framework control layer. This can be useful when
the logic is very
simple or where reuse of the business logic in other
environments is not
The framework architecture is flexible enough to
support most any
approach to accessing the Model, but we
recommend that you
separate the business logic ("how it's done") from the
classes play ("what to do"). 'nuff said.
For more about adapting your application's Model to the framework, see the Building Model Components chapter.
portion of a Struts-based application is most often
constructed using JavaServer Pages (JSP) technology. JSP
contain static HTML (or XML) text called "template text",
ability to insert dynamic content based on the
interpretation (at page
request time) of special action tags. The JSP environment
set of standard action tags, such as
whose purpose is described in the
JavaServer Pages Specification.
In addition to the built-in actions, there is a standard
define your own tags, which are organized into "custom tag
The framework includes a set of custom tag libraries that
creating user interfaces that are fully internationalized
interact gracefully with
ActionForms capture and validate whatever input is
For more about the Struts taglibs and using presentation pages with the framework, see the Building View Components section. Additional documentation regarding the taglibs is also available in the Taglibs subproject.
Struts provides the
portion of the application.
The Controller is focused on receiving
requests from the client (typically a user running a web
what business logic function is to be performed, and then
responsibility for producing the next phase of the user
an appropriate View component. The primary component of
Controller in the framework is a servlet of class
is configured by defining a set of
An ActionMapping defines a
that is matched against the
request URI of the incoming request and usually specifies
fully qualified class name of an Action class. All Actions
are subclassed from
encapsulate calls to business logic classes, interpret the
ultimately dispatch control to the appropriate View
component to create the
While the framework dispatches to a View, actually
View is outside its scope.
The framework also supports the ability to use
classes that have additional properties beyond the
standard ones required
to operate the controller. This allows you to store
specific to your application and still utilize the
remaining features of
the framework. In addition, the framework lets you define logical
"names" to which
control should be forwarded so that an action method can
ask for the
"Main Menu" page (for example), without knowing the
location of the
corresponding JSP page. These features greatly assist you
the control logic (what to do) with the view logic (how
For more about the control layer, see the Building Controller Components chapter.
The framework provides several components that make up the Control layer of a MVC-style application. These include a controller component (servlet), developer-defined request handlers, and several supporting objects.
The Struts Taglib component provides direct support for the View layer of a MVC application. Some of these tags access the control-layer objects. Others are generic tags found convenient when writing applications. Other taglibs, including JSTL, can also be used with the framework. Other presentation technologies, like Velocity Templates and XSLT can also be used with the framework.
The Model layer in a MVC application is often project-specific. The framework is designed to make it easy to access the business-end of your application, but leaves that part of the programming to other products, like JDBC, Enterprise Java Beans,Object Relational Bridge, or iBATIS, to name a few.
Let's step through how this all fits together.
When initialized, the controller parses a configuration
and uses it to deploy other control layer objects.
Together, these objects form the
defines (among other things) the collection of
for an application.
The controller component consults the ActionMappings as it
requests to other components in the framework. Requests
may be forwarded to JavaServer Pages or
subclasses provided by the
application 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:
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 mainMenu page.
Action objects have access to the application's controller component, and so have access to that members'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 shopping cart.
Most of the business logic in an application 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. You can define your own set of input bean
The ActionForm class makes it
easy to store
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, the framework has a shared mechanism for raising and displaying error messages.
Another element of the 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 Struts Taglib component provides custom tags that can automatically populate fields from a JavaBean. All most JavaServer Pages really need to know is the field names to use and where to submit the form.
Other 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 localization and will render the best available message for a user's locale.
The framework and Struts Taglib 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 message resource. 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 location.
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 variables.
In a database application, for example:
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 sections in this document cover the various framework components in greater detail. The Struts Taglib component includes several Developer Guides covering various aspects of the custom tags. A number of sample applications are bundled with the distribution that show how it all comes together.
The framework is distributed under the Apache Software Foundation license. The code is copyrighted, but is free to use in any application.