www.it-ebooks.info Beginning Apache Struts From Novice to Professional ■■■ Arnold Doray Doray_6048Front.fm Page i Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info Beginning Apache Struts: From Novice to Professional Copyright © 2006 by Arnold Doray All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-604-3 ISBN-10 (pbk): 1-59059-604-8 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Steve Anglin Technical Reviewer: Kunal Mittal Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Matt Wade Project Manager: Julie M. Smith Copy Edit Manager: Nicole LeClerc Copy Editor: Liz Welch Assistant Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor: Susan Glinert Proofreader: Lori Bring Indexer: Valerie Perry Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code section. Doray_6048Front.fm Page ii Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info To my darling wife Lillian, for her love, encouragement, and constant support, To my mother for always believing in me, To my father for showing me by example that you can accomplish almost anything you put your mind to, And to the One who gave up His life for us two thousand years ago. Doray_6048Front.fm Page iii Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info Doray_6048Front.fm Page iv Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info v Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi PART 1 ■ ■ ■ Basic Struts ■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■CHAPTER 2 Servlet and JSP Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 ■CHAPTER 3 Understanding Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 ■CHAPTER 4 Custom Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 ■CHAPTER 5 The MVC Design Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 ■CHAPTER 6 Simple Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 ■CHAPTER 7 Processing Business Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ■CHAPTER 8 Basic Struts Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 ■CHAPTER 9 Configuring Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 ■CHAPTER 10 More Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 ■CHAPTER 11 Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 ■CHAPTER 12 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 ■CHAPTER 13 Review Lab: Editing Contacts in LILLDEP . . . . . . . . . . . . . . . . . . . . . 157 PART 2 ■ ■ ■ Advanced Struts ■CHAPTER 14 Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 ■CHAPTER 15 The Validator Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 ■CHAPTER 16 Dynamic Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 ■CHAPTER 17 Potpourri . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 ■CHAPTER 18 Review Lab: The Collection Facility . . . . . . . . . . . . . . . . . . . . . . . . . . 267 ■CHAPTER 19 Developing Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 ■CHAPTER 20 JavaServer Faces and Struts Shale . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Doray_6048Front.fm Page v Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info vi ■APPENDIX A Frameworks for the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 ■APPENDIX B Commonly Used Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 ■APPENDIX C Struts Tag Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 ■APPENDIX D Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 Doray_6048Front.fm Page vi Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info vii Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi PART 1 ■ ■ ■ Basic Struts ■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What Is a Web Application? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What Struts Can Do for You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 About You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 ■CHAPTER 2 Servlet and JSP Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Lab 2: Installing Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Servlet Container Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Important Servlet Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 JavaServer Pages (JSP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Deconstructing Hello.jsp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Final Thoughts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 ■CHAPTER 3 Understanding Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Lab 3: Scopes Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Session and Request Scope Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Contents Doray_6048Front.fm Page vii Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info viii ■CONTENTS ■CHAPTER 4 Custom Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Custom Tag Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 How Custom Tags Are Processed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 The Java Handler Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The TLD File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Lab 4: A Temperature Conversion Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Step 1: Prepare the Development Environment and Scripts . . . . . . 32 Step 2: Write the Java Tag Handler . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Step 3: Writing the Tag Library Descriptor file . . . . . . . . . . . . . . . . . 33 Step 4: Amend web.xml. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Step 5: Write Your JSP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Step 6: Deploy and Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Professional Java Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 ■CHAPTER 5 The MVC Design Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The Registration Webapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Requirement 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Requirement 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Requirement 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Requirement 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Requirement 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Lab 5: MVC Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Which Comes First? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Struts and MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Lifecycle of a Struts Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Frameworks for the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 ■CHAPTER 6 Simple Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Processing Simple Validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Anatomy of ActionForm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Using ActionErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Doray_6048Front.fm Page viii Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info ■CONTENTS ix Lab 6: ContactForm for LILLDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Step 1: Prepare the Development Environment and Scripts . . . . . . 62 Step 2: Implement Getters and Setters for ContactForm . . . . . . . . . 63 Step 3: Implement validate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Step 4: Implement reset() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 What Has Struts Done for You Today? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ■CHAPTER 7 Processing Business Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 1,2,3 Action! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 The Statelessness of Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Subclassing Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Business Logic in the Registration Webapp . . . . . . . . . . . . . . . . . . . . . . . . 70 Complex Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Data Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Lab 7: Implementing ContactAction for LILLDEP . . . . . . . . . . . . . . . . . . . . 76 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 ■CHAPTER 8 Basic Struts Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Page Processing Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Evaluation, Replacement, and Sending . . . . . . . . . . . . . . . . . . . . . . . 81 The View Component of the Registration Webapp . . . . . . . . . . . . . . . . . . 82 Declaring and Installing the HTML and Bean Libraries. . . . . . . . . . . 83 Displaying Static Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Forms and Form Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Data Input Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Displaying Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Synopsis of HTML and Bean Tag Libraries . . . . . . . . . . . . . . . . . . . . . . . . 88 Lab 8: Contact Entry Page for LILLDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 ■CHAPTER 9 Configuring Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 The Structure of struts-config.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Configuring the Registration Webapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Declaring Form Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Declaring Global Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Declaring Global Forwards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Doray_6048Front.fm Page ix Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info [...]... 376 org .apache. struts. action.ActionMessage 376 org .apache. struts. action.ActionMessages 377 org .apache. struts. action.ActionErrors 377 org .apache. struts. action.ActionMapping 377 org .apache. struts. action.Action 377 org .apache. struts. action.ActionForm ... 377 org .apache. struts. action.ActionForm 378 org .apache. struts. upload.FormFile 379 org .apache. struts. tiles.ComponentContext 380 org .apache. struts. action.ExceptionHandler 380 ■APPENDIX C Struts Tag Reference 381 The HTML Tag Library ... part of the book covers Struts basics, as well as JSP and Servlet prerequisites The second part of the book covers the more “advanced” portions of Struts like Tiles, the Validator framework, and how to build your own plug-ins, among other things We’ll also study JSF and Struts Shale in the latter half of the book Useful Links • You’ll find the Apache Struts website at http:/ /struts .apache. org • Ted Husted’s... just hype: • Simplicity: Struts is relatively easy to pick up and use effectively You get a lot out of Struts for the effort you put in to learn it • Robustness: Struts provides much of the infrastructure you need to create webapps You can rely on this tested body of code built on solid design principles, instead of having to come up with solutions of your own • Maintainability: Struts is built around... process text, are examples In most cases, though, plain ol’ Struts will do the job In the course of this book, I’ll give you an overview of a number of technologies that work with Struts, among them Torque, Hibernate (in Appendix A), and JSF (which I’ll cover in detail in Chapter 18, along with Shale) What Struts Can Do for You Craig McClanahan created Struts because he was asked to port a U.S.-centric application... 336 Lab 20: The Struts- Faces Integration Library 337 Step 1: Preparing the Development Environment 337 Step 2: Install JSF, JSTL, and the Struts- Faces Integration Library 337 Step 3: Edit web.xml and struts- config.xml 338 Step 4: Migrate Your Struts JSP Pages 339... Husted’s website, http://husted.com /struts, contains many nuggets on Struts best practices • The ServerSide, www.theserverside.com: has useful articles and discussions on Struts • You’ll find Craig McClanahan’s blog at http://blogs.sun.com/roller/page/ craigmcc • Spring is another web application framework; learn about it at www springframework.org • Pro Jakarta Struts, from Apress, is now in its second... (for this and other interesting bits, see Craig’s blog in “Useful Links” at the end of this chapter) So, needless to say, internationalization is something that Struts makes particularly easy to do But Struts has much to offer beyond this Struts provides an extensive infrastructure for things like user input validation, error handling and reporting, and flow control, all of which are the bread and... other parts Can you see why? Even if you’re a lone developer, Struts enforcement of a separation of concerns translates into modularized and therefore more maintainable code Although this might sound like something you’ve always been doing in Java, Struts helps you do it across a smorgasbord of languages—Java, JSP, JavaScript, and SQL Struts does all this and is easy to learn, which certainly contributes... 10:49 AM PART 1 ■■■ Basic Struts S truts grew out of a personal need (open source developers often call this scratching your own itch) to support the development of an application that I was responsible for I began the process of open sourcing this idea of a web application framework at the Apache Software Foundation What happened next was nothing short of extraordinary— Struts quickly became the . www.it-ebooks.info Beginning Apache Struts From Novice to Professional ■■■ Arnold Doray Doray_6048Front.fm Page i Tuesday, January 24, 2006 4:42 PM www.it-ebooks.info Beginning. . . . . . . . . . 376 org .apache. struts. action.ActionMessage . . . . . . . . . . . . . . . . . . . . . . . . . 376 org .apache. struts. action.ActionMessages