Struts in Action
Building web applications with the leading Java framework

Ted N. Husted, Cedric Dumoulin, George Franciscus, David Winterfeldt

2002 | 672 pages
ISBN: 1930110502

$44.95 Softbound print + PDF eBook
$35.99 eBook edition (PDF only)

RESOURCES

DESCRIPTION

Struts solves the most common problems of web development. By basing your application on the Struts framework, you can reuse proven solutions and focus on what's unique to your own case. Struts is an open-source product distributed by the Apache Software Foundation. Struts in Action is a comprehensive introduction to the Struts framework. It covers initial design, data validation, database access, dynamic page assembly, localization, product configuration, and other important areas. It shows you how to use both JSP tags and Velocity templates. It carefully explains the Struts architecture and control flow, as well as how to extend framework classes.

Differences between Struts 1.1 and Struts 1.0. are pointed out and a case-study illustrates the 1.0 to 1.1 transition. The book shows you dozens of proven design techniques, patterns, and strategies, many of them not found elsewhere.

What's Inside:

WHAT THE READERS SAY ABOUT THIS BOOK

"I have a team currently refactoring and customizing a 40KLOC web app for a client. The developer in charge of the web tier was not familiar with Struts (which was used in the webapp) and asked me for some quality and practical documentation about it, I pointed him to the electronic Struts in Action available on your site, he bought and downloaded it straight away and less than 48h later he was able to see that the current implementation is an absolute mess and was not using Struts correctly.

Clearly a book that can give this kind of perception to a developer with no knowledge of the framework (and willing to learn) during that amount of time, is a must have."
-- Stephane Bailliez Linagora

ABOUT THE AUTHORS

Ted Husted is an acknowledged Struts authority, an active member of the Struts development team, and manager of the JGuru Struts Forum. As a consultant, Ted has worked with professional Struts development teams throughout the United States. Ted also helps manage the Apache Jakarta project, which hosts the Struts framework. Ted lives in Fairport, New York with his wife, two children, four computers, and an aging cat.

Cedric Dumoulin is an active member of the Struts development team and the author of the Tiles framework. Cedric is presently a researcher at the University of Lille. He has also worked in the R&D department of a leading international internet banking company. He lives in Lille, France.

George Franciscus is a principal at Nexcel, providing technical and management consulting services in several industries including Telecommunications, Banking, Life Insurance and Property and Casualty Insurance. George has expertise in Java, J2EE, Domino, relational databases, and mainframe technologies. He holds a BSc in Computer Science from the University of Toronto. George lives in Toronto, Ontario with his wife and three children.

David Winterfeldt is a Struts committer and author of the Commons Validator package. He works as a senior developer at a major company implementing J2EE technology. David currently lives in New York City.

Sample Chapters

Chapters 11 and 12 of Struts in Action are available here in Portable Document Format (PDF); you need Adobe's free Acrobat Reader software to view them. You may download Acrobat Reader here.

Download Chapter 11. (182 Kb)

Download Chapter 12. (238 Kb)

Foreword/Preface


Foreword

You're holding in your hands the result of the hard labor of some of Struts' most important developers. Ted, Cedric, George, and David have done an outstanding job of explaining how Struts works and how it is used in practice. If you're a new developer, Struts in Action will make it much easier for you to learn the framework and quickly put it to work on your own projects. But even the most seasoned Struts developer is certain to learn something new by reading this book.

I became interested in web application development in the late 1990s. I was ready for a language that would let me address one of the most problematic aspects of advanced development—the need to free up dynamically allocated memory when I was through with it.

In the beginning, all I really hoped to accomplish was to make life a little easier for a few developers building web applications. The incredible popularity that Struts has achieved since then means that I wasn't the only one who struggled, Struts fills a very common need.

When the early public drafts of the JavaServer Pages specification (versions 0.91 and 0.92) became available, one of the intriguing concepts embedded in these documents was the idea of two basic design styles for JSP-based applications. A Model 1 design is characterized by form submits that go back to the servlet or JSP page that created the form. This design encourages you to mix the presentation logic (used to create the form) with the business logic (used to validate the form input and process the requested transaction). Such a design is often used when developers of only one skill set (either page authors who know a little programming, or Java developers who know a little HTML) are available. It is also useful when time is of the essence ("The prototype needs to work by next Monday or we don't get our venture capital funding"). Experience has taught us that Model 1 designs can be difficult to maintain and enhance in the future.

In contrast, a Model 2 design submits forms to a controller component. The controller component dispatches to an appropriate business-logic component to perform the requested transaction. The business-logic component interacts with the database and acquires the information it needs for the next user interaction. The controller component delegates the creation of the response page to a presentation component whose sole purpose is to create that response.

You're probably thinking that the Model 2 style sounds much more complicated—perhaps even like overkill for simple applications. Indeed, creating an application based on the Model 2 design does take longer than building the same application in a Model 1 style. But the primary benefits show up quickly. If you've created the proper architecture, major changes to one tier should have relatively little (if any) impact on the other tier, and you can reuse the logic in the unaffected tier immediately.

While all of this intellectual investigation of web application architectures was going on, my professional career was leading me in interesting directions as well. I was working for a company that provided information services to the long-haul trucking industry in the United States, and we wanted to expand this service into Europe. This created the need to deal with multiple languages and internationalization. I quickly whipped up a simple controller servlet that let me implement the basic MVC architecture, but it didn't address, say, the need to include a Select Language control.

Our first effort at internationalization started me down the path of creating "user interface components" using the new custom tags facilities of JSP 1.1—which led ultimately to things like the <bean:message> tag that is a part of Struts today.

Shortly after this, I joined Sun Microsystems to work with the Tomcat servlet and JSP container (I was the primary architect of the Catalina servlet container that is the basis of Tomcat 4). A large portion of this development took place in the open source community at Apache, as part of the Jakarta Project—initiated when Sun contributed the source code of what had been the servlet and JSP reference implementation to Apache in 1999. However, I was never happy with the state of Model 2-oriented application designs, so I resolved to do something about it.

Although I had a pretty good idea of how to solve the remaining problems, the actual code for Struts did not come into being until, much to the chagrin of my wife, I took my laptop along with me to the 2000 Memorial Day weekend with my family on the Oregon coast. The very first version of what became the ActionForm was born that weekend, and it turned out to solve a number of interesting design problems. In addition, the idea of defining logical names for the presentation and business logic components—and centralizing the definition of those names in a single configuration file—was clearly beneficial in solving the overlapping problems of coordination between the development of the two tiers and the goal of insulating tiers from changes in the other.

Through my work on Tomcat I had recognized the benefits of open source development so it was a natural choice to bring Struts to the world of open source as well. This choice—and the elegance of how Struts deals with some of the most basic problems of web application design—has resulted in acceptance that is truly astounding. Thousands of developers have downloaded Struts, gone through the learning curve, asked questions (and received answers) through the STRUTS-USER mailing list, and have successfully deployed applications based on Struts all over the world.

Of course, I was not able to accomplish all of this on my own. Ted, Cedric, David, and all the other past and present Committers for the Struts project, along with George and the community of Struts developers, have made the framework far more useful than I ever could have done alone. To them, I offer my heartfelt thanks. To you, the reader of this much-needed book, I hope that you find Struts a useful addition to your arsenal, well worth the investment of time to learn its techniques and APIs.

Enjoy!

Craig McClanahan
Portland, Oregon


Preface

By 2000, Java had come of age. The dust from the early hype had settled and some very interesting development tools and libraries were cropping up. I had already been writing web applications for several years. Like many developers, I started with simple apps in JavaScript and Perl. A powerful combination, but a bear to maintain. Next came ColdFusion, which was much more powerful but at the time too expensive for my client's pocketbook. I even tried FileMaker Pro, which was fun, but very, very proprietary.

My major client for this succession of web applications was a public broadcasting station. The station's major fund-raiser was (and still is) an annual auction. Local vendors donate goods and services, and people buy them at auction to support the station. Of course, we were quick to post images of the high-end items on the web site: objets d'art, a car, vacation packages, autographed items, and so forth.

In 1998, we used an application written with JavaScript and Perl to accept "pre-bids" on the high-end items. The actual bidding on these items took place in live television bid-downs. All the application really did was set the starting bid price. In 1999, we accepted both online and phone bids right up to when the item sold. Each year, I used a different platform to put the auction online, because each year I found that the platform didn't meet all my needs.

Since we were already satisfied users of the Apache HTTPD server, I invested some time in wandering through the nascent Jakarta site, where I discovered Struts. At first, I wasn't even sure the project was still active. But the documentation seemed promising, so I subscribed to the list to see if anyone was home. An example application was already bundled with the documentation. I started working my way through the example, trying to figure out the framework as I went along. This journey turned into the "Walking Tour of the Struts Application," which describes how the example application works, screen by screen. I posted the tour to the list, where some of the subscribers gently corrected my understanding of the finer points.

I continued to follow the list, helping others when I could, and being helped by those who had traveled this road before me. Traffic on the list grew steadily. Toward the end of the year, Struts' architect and lead developer, Craig McClanahan, was looking for people to help with the documentation for the 1.0 release. I was elected a Struts Committer in December 2000, and we finally shipped Struts 1.0 in June 2001.

Along the way, I started my "More About Struts" page. At first, it was just a place where I could keep the links to the Struts material I was writing. Then I began adding links to the Struts "extensions" people had started to distribute, and then to the many Struts articles that had begun to appear. My Struts Resources page grew larger, and more popular, and so I moved it to the main Struts site. It is now a set of several pages with links to everything known about the Struts universe.

The Struts list remained a treasure trove of helpful information, especially since Craig himself was usually on hand to shed light on the implementation details and architectural philosophy. But finding the best bits in the list archive could be a challenge. So, I started a "threads page" within links to the best email nuggets, which grew into a fairly large FAQ. In June 2001, JGuru decided to open a Struts forum and FAQ, and we moved the main Struts FAQ to JGuru, where I continue to manage it.

Around the same time, publishers started to take notice of Struts, and offers began to arrive in my mailbox. After consulting with some of the other Struts Committers, we eventually decided to work with Manning Publications. Like Apache, Manning has a longstanding commitment to quality. While we wanted to get a Struts book out as soon as we could, we also wanted to be sure that it would be the best book possible.

The result is Struts in Action. It is very much a "team book." David Winterfeldt, the creator of the Struts Validator, was kind enough to draft our Validator chapter. Likewise, Cedric Dumoulin, the creator of Tiles, drafted the Tiles chapter. George Franciscus provided the critical chapter 1 of the book, which is designed to help bring newbies into the fold. We even dragged a foreword out of Craig (who would "rather be programming"). Of course, other Struts developers and Committers reviewed the manuscript at every stage, and we are thankful for the many helpful comments.

Oh, and the auction? We are going on our third year using Struts. Now instead of rewriting it every year, we improve it every year.

Ted Husted
Fairport, New York

WHAT REVIEWERS ARE SAYING

"Provides a good overview of Struts especially advanced topics such as the Validator framework and Action Mappings..."
-- Chicago Java Users Group

"If you are working in the IBM Websphere area for web application development, you will often encounter references to the Struts architecture framework. IBM highly recommends this standard for Java application development for Websphere. And if you want to acquaint yourself with that framework, this is a good book to get you there.
-- Portland Domino/Notes User Group

"The clarity of Struts in Action is even more appealing when compared to Sue Spielmann's The Struts Framework and James Goodwill's Mastering Jakarta Struts."
-- Dr. Dobb's Journal, May 2003

"...it doesn't just talk about how to configure and develop with Struts. It's a web application manifesto."
-- Slashdot.org

"...the author's approach to making the reader a Struts expert is effective... I would not hesitate recommending this book to anyone that wants to go from Struts beginner to Struts expert."
-- JavaRanch.com

Source Code

Source code for Struts in Action is contained in a single ZIP file. Free unzip programs can be found at www.download.com.

husted_src.zip (10 Mb)