In this book we have done our very best to bring you useful and interesting recipes to meet the challenges of Struts developers. As is the case with most well-garnished dinner tables, these recipes include a little of something for everyone. There are plenty of meat and potatoes recipes to guide you through some of the common, everyday things you need to do. To satisfy those with a big appetite we have included recipes to secure and internationalize your applications. An entire chapter has been devoted to making sure you have the tools you need to validate input. If you enjoy a nice dessert, but dont like to spend a lot of time in the kitchen, you are invited to check out an entire chapter devoted to the Struts-Layout library. There is nothing better than a gourmet meal thats ready to serve! For those with exotic tastes we have devoted another chapter to advanced Struts recipes. To make sure your application works as expected, we have included a chapter showing how to apply several facets of testing. The book has a diverse set of recipes in several key areas of interest.
This chapter contains some of the simpler and more common recipes in the book. It contains a mixture of everyday recipes you will find yourself using time and time again. Among other things, you will see different ways to work with resource bundles, learn how to default your application to an
ActionForward, learn to create an automated build, and be introduced to an effective technique to manage constants. The recipe that shows you how to perform an automated build using Ant will take the drudgery out of the implementation process. Almost every application draws something from these recipes.
Forms are the heart and soul of most web applications. In this chapter we cover a number of recipes designed to allow you to work with forms. Several recipes are dedicated to using
ActionMessages. In addition we show you a new way to alternate row colors. We also show how to create a single-form wizard.
The Struts tag library plays a critical role to deliver the view portion of applications. In this chapter we cover the most significant and useful Struts tags.
The Struts-Layout tag library is an exceedingly popular and useful third-party library available to Struts developers. In this chapter we delve into this library by showing you how to create tabs, skins, trees, and much more.
No application is complete without validation. In this chapter we show you how to use one of the newest and most useful Struts featuresdeclarative exception handling. We show you how to aggregate your exceptions and present an interesting recipe to bring synergy between the
DispatchAction and validation. We present recipes showing how to use the Validator to create both server-side and client-side validation. We roll up our sleeves and discuss how to create your own pluggable validator to perform cross-form validation. In addition, this chapter contains a recipe describing one of the most undervalued features in the Validator frameworkValidator constants. Finally, we cover applying validation to wizard applications.
Internationalizing applications is essential in the global marketplace. In this chapter we delve into making the best use of Struts inherent capability to internationalize applications. We show you how to internationalize both text and images. In addition, we cover how to use the
RequestProcessor to make your
ActionForward locale sensitive. We also dig into the built-in internationalization feature bundled inside the Validator and Tiles.
No application is complete without addressing security. In this chapter we present recipes on signing in and signing out. We present a series of recipes yielding techniques to protect your applications at various levels of granularity, including action mapping, areas, and fields. In addition, we include a recipe showing you how to make good use of the Struts SSL extension for HTTP/HTTPS switching to turn SSL on and off declaratively.
This chapter contains several advanced Struts recipes. In this chapter we show you how to create a PDF and how to generate HTML from XML using XSL. You will find a recipe to create a well-structured layered application using several best practices and patterns. This chapter covers how to cache resources by creating your own custom Struts plug-in. You will find a recipe on how to use the Tiles controller to manipulate data. We also reveal how to use the popular Hibernate project within a Struts application. We are sure you will find many interesting and useful recipes in this chapter.
Experienced developers value a diligent testing effort. In this chapter we explore Struts testing by providing several recipes designed to ensure your application is robust and scalable. This chapter gives you step-by-step instructions on how to perform outside-the-container testing with
StrutsTestCase. Another recipe shows how to combine
StrutsTestCase and Cactus to test your application in one or more containers. We present an interesting technique to test
DynaActionForms and a strategy to approach module testing. To make sure your application is performing as expected, we demonstrate how to combine
StrutsTestCase and JUnitPerf. Finally, we have included a recipe regarding the use of jCoverage to ensure your application has been tested thoroughly.
The material in this book is wide ranging. There is a little bit of something for everyone, but its difficult to cover it all. We were undecided about the need for a Struts-EL recipe. One minute it was in, the next it was out. A better approach to this topic would be to discuss the advantages and disadvantages of JSTL and Struts-EL, but, alas, this is out of the scope of a cookbook. For that reason we decided to exclude it from this book.
Each recipe is divided into six sections:
This book lists several best practices designed to help you avoid common pitfalls and benefit from the experiences of others. The art of creating software involves making choices. Reflecting on past choices allows us to separate the good choices from the bad. Sharing best practices is one of the ways we can leverage each others knowledge. Although the best practices listed in this book are appropriate for the majority of situations, you are encouraged to evaluate and share best practices to ensure the best practice addresses your particular circumstance.
For your convenience, we have included a table of contents listing all of the best practices in the book on page xv.
This book has the spirit of a cookbook. If you need to cook something up because you have company coming over, or you have a deadline looming, then this book is designed to let you look up what you need in the table of contents and get to work. For that reason, the best place to start is the table of contents. You can use the table of contents as your compass to navigate the book. Although each recipe stands on its own, this doesnt preclude you from reading the book cover to cover. The book is roughly organized in the manner in which you might develop an application. If you want to skip a recipe along the way, then go right ahead. Well be sure to tell you if we think another recipe is recommended reading.
We have made the best use of the newest features in Struts 1.1. Therefore, we suggest you download Struts 1.1 or later before you set foot in the kitchen.
This book contains an extensive set of code listings. Different readers have different preferences when it comes to code listings. Some readers want abbreviated code listings, others want all the gory details. We decided to go with the lowest common denominator by providing code listings as complete as possible wherever it made sense. Every effort has been made to assure the correctness of the code listings. However, people are not perfect, including authors. We are very interested in learning about any errors in the code or text. If you discover any, please contact us through the Author Online forum at www.manning.com/franciscus so we can make the necessary corrections as soon as possible.
The following conventions are used throughout the book:
Couriertypeface is used in all code listings.
Courier Boldis sometimes used to draw your attention to a section of code.
Couriertypeface is used in text for the following: Struts classes (
ActionForward, etc.), HTML/XML tags, attributes, events, commands, methodsin general, most code words.
The ramification of good naming conventions is that its tricky to distinguish between nouns representing different things. One such situation occurs with the word validator. When referring to the Apache Validator product, we capitalized the letter V. When referring to an object performing validation logic within the Validator, we use a small case v. The same convention was used to describe Tiles. When referring to the Apache Tiles project, now integrated into Struts, we capitalize the letter T. When referring to tile as a software artifact, we use a lowercase t.
Often concrete implementation of
ActionForm classes are nicknamed form beans. This nickname is likely attributed to the
<form-bean> tag found in the struts-config.xml file. Throughout the book we have used the term form bean in this context.
Bibliographic references are indicated with square brackets in the body of the text. The contents within the square brackets represents a handle to the reference. The References section of each chapter contains short descriptions of the handles, and the References area at the rear of the book contains full publication details. The Related section of each recipe lists other recipes in the book that relate to the recipe being discussed.
Purchase of Struts Recipes includes free access to an Author Online forum, a private web discussion board run by Manning Publications. You are encouraged to use this forum to make comments about the book, ask technical questions, and receive help from the authors and other readers. Use your browser to navigate to www.manning.com/franciscus to take advantage of this free service. The forums welcome page gives you all the information you need to sign up and get going.
The Author Online forum is one of the ways Manning remains committed to readers. The authors participation in the forum is voluntary and without a specified level of commitment. The forum is a great way to share ideas and learn from each other. The Author Online forum will remain accessible from the publishers web site as long as the book is in print.
As software developers we are very interested in well-designed solutions. Wherever possible we applied best practices in our code samples. In some circumstances we might have broken our own rules to make the code clearer and less confusing. You might notice that we have placed a good deal of emphasis on the Model-View-Controller design pattern. MVC is the cornerstone of good design for all significant applications. Anyone choosing to work with Struts is a firm believer (or soon will be) in MVC. It is our intention that the recipes in this book show you how to stay true to MVC principles.
Experienced developers understand that no solution is perfect, and not every shoe fits every foot. Usually an advantage leads to some other compromise. The goal is to understand the advantages, and then balance them against the compromises to make the best judgment call for the situation at hand. The Discussion sections address these issues to ensure that you are armed with the correct information to make your own decisions.
Each recipe is written from the vantage point of experience, often the only quality that counts. In writing each recipe we strive to point the way towards a more flexible, maintainable, secure, robust, scalable, and easy-to-use application that is encapsulated and orthogonal. You should do your best to keep these goals in mind as you read each recipe. As you cook up your own recipes, we hope that you draw upon these principles. We encourage you to share your ideas and thoughts as a means to create higher-quality applications for all of us to use.
GEORGE FRANCISCUS is a consultant at Nexcel.ca, providing technical and management consulting services. George has almost 20 years of experience in a diverse range of technologies, including Java, J2EE, Domino, relational databases, and mainframe technologies. George is the coauthor of Manning Publications Struts In Action. He holds a BSc in Computer Science from the University of Toronto. George lives in Toronto, Ontario, with his wife and three children.
DANILO GUROVICH is Manager of Web Development at LowerMyBills.com in Santa Monica, California. His experiences include designing and implementing Struts-based applications in high-traffic commerce, enterprise application integration monitoring and controlling, and business process management software. His non-Java experience extends into GUI design, human factors, and graphics. He graduated from Claremont McKenna College a long time ago, when FORTRAN was big, disk space was small, and nobody had heard of a PC. Danilo lives in Northridge, California, with his wife and daughter and spends what little free time he has pursuing old-school fine photography and restoring Citroens.
The figure on the cover of Struts Recipes is a Griaega de Atenas, a Greek woman of Athens. Judging by her fancy dress and jewelry, she is clearly well-to-do, the wife of a successful merchant or tradesman perhaps. The illustration is taken from a Spanish compendium of regional dress customs first published in Madrid in 1799. The books title page states:
Coleccion general de los Trages que usan actualmente todas las Nacionas del Mundo desubierto, dibujados y grabados con la mayor exactitud por R.M.V.A.R. Obra muy util y en special para los que tienen la del viajero universal
which we translate, as literally as possible, thus:
General collection of costumes currently used in the nations of the known world, designed and printed with great exactitude by R.M.V.A.R. This work is very useful especially for those who hold themselves to be universal travelers
Although nothing is known of the designers, engravers, and workers who colored this illustration by hand, the exactitude of their execution is evident in this drawing. The Griaega de Atenas is just one of many figures in this colorful collection. Their diversity speaks vividly of the uniqueness and individuality of the worlds towns and regions just 200 years ago. This was a time when the dress codes of two regions separated by a few dozen miles identified people uniquely as belonging to one or the other. The collection brings to life a sense of isolation and distance of that periodand of every other historic period except our own hyperkinetic present.
Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.
We at Manning celebrate the inventiveness, the initiative, and, yes, the fun of the computer business with book covers based on the rich diversity of regional life of two centuries ago? brought back to life by the pictures from this collection.