This section gives a broad overview of what Struts is all about.
There are several different ways of looking at Struts. The
three main ways are that Struts is:
- An MVC Framework.
Struts provides a unified framework for deploying servlet and JSP
applications that use the MVC architecture.
- A Collection of Utilities.
Struts provides utility classes to handle many of the most common
tasks in Web application development
- A Set of JSP Custom Tag Libraries.
Struts provides custom tag libraries for outputting bean properties,
generating HTML forms, iterating over various types of data
structures, and conditionally outputting HTML.
But which is the
proper way to view Struts? The answer depends on
what you are going to use it for, but the MVC framework is the most
common way of looking at Struts.
You can easily implement the MVC approach
by using
RequestDispatcher.forward in your servlets and jsp:getProperty or the
JSP 2.0 expression language in your JSP pages. However, Struts offers a number
of significant advantages over these techniques alone. Here is a summary:
- Centralized File-Based Configuration.
Rather than hard-coding information into Java programs, many
Struts values are represented in XML or property files.
This loose coupling means that many changes can be made
without modifying or recompiling Java code,
and that wholesale changes can be made by editing a single file.
This approach also lets Java and Web developers focus on their
specific tasks (implementing business logic, presenting certain
values to clients, etc.) without needing to know about the
overall system layout.
- Form Beans.
In JSP, you can use property="*" with jsp:setProperty to
automatically populate a JavaBean component based on
incoming request parameters. Unfortunately, however, in the
standard API this capability is unavailable to servlets, even
though with MVC it is really servlets, not JSP pages, that should usually
be the target of form submissions. Apache Struts extends this
capability to Java code and adds in several useful utilities, all
of which serve to greatly simplify the processing of request parameters.
- Bean Tags.
Apache Struts provides a set of custom JSP tags
(bean:write, in particular) that let
you easily output the properties of JavaBeans components.
Basically, these are concise and powerful variations
of the standard jsp:useBean and
jsp:getProperty tags.
- HTML Tags.
Apache Struts provides a set of custom JSP tags
to create HTML forms that are associated with JavaBeans components.
This bean/form association serves two useful purposes:
- It lets you get initial form-field values from Java objects.
- It lets you redisplay forms with some or all
previously entered values intact.
- Form Field Validation.
Apache Struts has builtin capabilities for checking
that form values are in the required format. If values are
missing or in an improper format, the form can be automatically
redisplayed with error messages and with the previously entered values
maintained.
This validation can be performed on the server (in Java),
or both on the server and on the client (in JavaScript).
- Consistent Approach.
Struts encourages consistent use of MVC throughout your application.
Now, if these advantages sound significant, they were meant to.
But, before you rush out and convert all your existing projects
to Struts, look at the disadvantages as well.
Although Struts has a number of significant advantages over
the standard servlet and JSP APIs alone, due to
its complexity it has some serious drawbacks as well.
- Bigger Learning Curve.
To use MVC with the standard RequestDispatcher, you need
to be comfortable with the standard JSP and servlet APIs.
To use MVC with Struts, you have to be comfortable with the standard JSP and servlet APIs
and a large and elaborate framework that is almost equal
in size to the core system. This drawback is especially
significant with smaller projects, near-term deadlines, and
less experienced developers; you could spend
as much time learning Struts as building your actual system.
- Worse Documentation.
Compared to the standard servlet and JSP APIs, Struts
has fewer online resources, and many first-time users find the
online Apache documentation confusing and poorly organized.
There are also fewer books on Apache
Struts than on standard servlets and JSP.
- Less Transparent.
With Struts applications, there is a lot more going on behind the scenes
than with normal Java-based Web applications. As a result,
Struts applications are:
- Harder to understand
- Harder to benchmark and optimize
- Rigid Approach.
The flip side of the benefit that Struts encourages
a consistent approach to MVC is that Struts makes
it difficult (but by no means impossible) to use
other approaches.
Now, if these disadvantages sound significant, they were meant to.
Struts has some significant advantages, but the overhead and
complexity of Struts can be a burden as well. Before you decide
if Struts is right for your projects, you should try a simple
Struts application to get a feel for it. Then, carefully weigh
the advantages and disadvantages and decide if all of the
Struts benefits outweigh the negatives. Sometimes they will;
other times they won't.
-
J2EE Short Courses
- Personally developed and taught by the author of Core Servlets & JSP,
More Servlets & JSP, and this Jakarta Struts tutorial.
-
-
Programming with Servlet & JSP Technology. Fast-paced, hands-on course for developers
who know Java but have little or no experience with servlets and JSP.
Offered June 20-23 in
Columbia, MD.
-
Jakarta Struts and Advanced Servlet & JSP Programming.
Advanced hands-on course for developers
with significant previous experience with servlets and JSP.
Offered June 26-30 in
Columbia, MD.
-
Web App Development with JavaServer Faces (JSF).
Advanced hands-on course for developers
with significant previous experience with servlets and JSP.
Offered July 11-13 in
Columbia, MD.
-
Applying Jakarta Struts. Advanced Struts
course for those with significant previous JSP and
servlet experience.
-
On-site Training Courses: JSP, Servlets, Jakarta Struts, JSF, AJAX, and Java 5.
Customizable courses taught by Marty at your organization. Choose
any combination of topics from Marty's Java 5, JSP, servlet, Struts, JSF, and AJAX courses. Available in any country.
- Contact Marty
- Send email to hall@coreservlets.com
to report errors and omissions in this writeup, or to inquire about
training courses on Struts, JSP, servlets, JSF, AJAX, or Java 5 programming.
|
- References
- Jakarta Struts Books
- Servlet & JSP Books
|