|Section 1: Understanding Struts
Capabilities, Pros, and Cons of Apache Struts
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:
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.
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.
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:
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.
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.
In JSP, you can use
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.
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
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.
Struts encourages consistent use of MVC throughout your application.
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.
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.
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.
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.
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
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.
- Servlet, JSP, and Struts 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 Nov 30-Dec 3 in Columbia, MD.
Advanced Servlet, JSP, & Struts Programming. Advanced hands-on course for developers with significant previous experience with servlets and JSP. Offered Dec 7-10 in Columbia, MD.
- Applying Apache Struts. Advanced Struts-only course for those with significant previous JSP and servlet experience.
On-site JSP, Servlet, Struts, & JSF Training Courses. Customizable courses taught by Marty at your organization. Choose any combination of topics from Marty's Java, JSP, servlet, Struts, and JSF courses.
- Contact Marty
- Send email to [email protected] to report errors and omissions in this writeup or inquire about JSP/servlet/Struts training courses.
- Apache Struts home page.
- Apache Struts documentation.
- JSP and servlet programming resources.
- Servlet 2.3 and JSP 1.2 API.
- Servlet 2.4 API.
- JSP 2.0 API.
- JSP, servlet, and Struts training and consulting
- Apache Struts Books
- Struts in Action: Building Web Applications with the Leading Java Framework.
- Programming Jakarta Struts.
- Professional Jakarta Struts.
- Servlet & JSP Books
- Core Servlets and JavaServer Pages, Second Edition.
- Free online version of the First Edition of Core Servlets and JavaServer Pages. In PDF.
- More Servlets and JavaServer Pages.