ebook img

Apress Pro Apache Struts with Ajax Oct 2006 PDF

525 Pages·2006·7.83 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Apress Pro Apache Struts with Ajax Oct 2006

CYAN YELLOW MAGENTA BLACK PANTONE 123 CV EMPOWERING PRODUCTIVITY FOR THE JAVA™ DEVELOPER THE EXPERT’S VOICE® IN JAVA™ TECHNOLOGY Companion eBook Available Pro Apache Struts with Ajax Pro Dear Reader, Open source has had a huge impact on the ability of software developers to Pro Apache quickly build and deploy applications. One of the most vibrant areas of open A source development is that of Java™development frameworks. In this book, we present the open source Apache Struts framework, which provides the infra- p structure code that developers otherwise would need to write to build enterprise John Carnell,coauthor of web-based applications. Struts a Pro Jakarta Struts We explore Struts by looking at these key topics: • Learning the core features of the Apache Struts framework, starting c with the architecture of Struts through to setting up and configuring web applications using Struts h • Building each tier of a sample application, starting with the presentation tier and diving through all of the tiers in a web-based application e • Leveraging the Struts JSP™tag libraries and their templating capabilities Rob Harrop,coauthor of to simplify the development of web application screens Ajax Pro Jakarta Struts • Using Struts and industry accepted J2EE™design patterns to build S business logic that can be easily re-used and maintainable across multiple Struts and non-Struts applications t • Integrating open source Object Relational mapping tools such as Apache’s r ObjectRelationalBridge in the data access tier with u Throughout the book, we also look at how to integrate Struts with powerful open source tools such as Lucene, Velocity, and XDoclet, so you can add a sig- t Kunal Mittal,author of nificant amount of functionality to your web applications. We show you the Pro Apache Beehive mechanics of building a Struts application, and we also demonstrate proven s BEA WebLogic Server 8.1 techniques for building multitier, web-based applications. Our aim is to enable Unleashed you to build on our own experience working with Struts over the course of our w careers. Join online discussions: i forums.apress.com Sincerely, t Architect,build,and configure multitier web applications h FOR PROFESSIONALS BY PROFESSIONALS™ John Carnell and Rob Harrop using the Apache Struts framework along with some Ajax. A Companion eBook THE APRESS JAVA™ ROADMAP j Pro Apache Ant a Enterprise Java™ Beginning Apache Struts Pro Apache Struts with Ajax See last page for details Development on a Budget x on $10 eBook version Pro Apache Tomcat 6 John Carnell C a SOURCECODEONLINE ISBN 1-59059-738-9 r n with Rob Harrop www.apress.com 54499 e l l Edited by Kunal Mittal US $44.99 Shelve in Java Programming User level: Intermediate–Advanced 6 89253 59738 5 9 781590 597385 this print for content only—size & color not accurate spine = 0.998" 528 page count Ch00_7389_CMP3 9/28/06 8:37 PM Page i Pro Apache Struts with Ajax John Carnell with Rob Harrop, Edited by Kunal Mittal Ch00_7389_CMP3 9/28/06 8:37 PM Page ii Pro Apache Struts with Ajax Copyright © 2006 by John Carnell,Rob Harrop,Kunal Mittal 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-738-5 ISBN-10 (pbk): 1-59059-738-9 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. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Technical Reviewer: John Fallows Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Managers: Beth Christmas, Elizabeth Seymour Copy Edit Manager: Nicole Flores Copy Editors: Ami Knox, Bill McManus Assistant Production Director: Kari Brooks-Copony Production Editor: Lori Bring Compositor: Diana Van Winkle, Van Winkle Design Proofreader: April Eddy Indexer: Michael Brinkman 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 [email protected], 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 [email protected], 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.comin the Source Code/ Download section. Ch00_7389_CMP3 9/28/06 8:37 PM Page iii To my wife,Janet:Thank you for the love,the patience,and the time I needed to complete this book (and every other book I have worked on).Without your love and wisdom,my life would be a shadow of what it is now.You are everything to me. To my son,Christopher:Every experience I have had or will have will never compare with the first time I held you in my arms.Everyday,I revel in the miracle that you are. —John Carnell This book is dedicated to my secondary school English teacher,Neville McGraw, for sparking my abiding interest in literature and teaching me the importance of the written word. —Rob Harrop Ch00_7389_CMP3 9/28/06 8:37 PM Page iv Ch00_7389_CMP3 9/28/06 8:37 PM Page v Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Preface for This Edition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Preface from Previous Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx ■ CHAPTER 1 What We Do Wrong:Web Antipatterns Explained . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 Struts Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 ■ CHAPTER 3 Form Presentation and Validation with Struts . . . . . . . . . . . . . . . . . . 75 ■ CHAPTER 4 Managing Business Logic with Struts . . . . . . . . . . . . . . . . . . . . . . . . . 123 ■ CHAPTER 5 Architecting the Data Access Tier with ObjectRelationalBridge. . . 173 ■ CHAPTER 6 Building Flexible Front-Ends with the Tiles Framework . . . . . . . . 225 ■ CHAPTER 7 Dynamic Forms and the Struts Validator Framework . . . . . . . . . . . 255 ■ CHAPTER 8 Speeding Struts Development with XDoclet . . . . . . . . . . . . . . . . . . . 289 ■ CHAPTER 9 Logging and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 ■ CHAPTER 10 Velocity Template Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 ■ CHAPTER 11 Extending the Struts Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 ■ CHAPTER 12 Struts and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 ■ APPENDIX A JavaEdge Setup and Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 ■ APPENDIX B Struts Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 ■ APPENDIX C Struts and Strecks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 v Ch00_7389_CMP3 9/28/06 8:37 PM Page vi Ch00_7389_CMP3 9/28/06 8:37 PM Page vii Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Preface for This Edition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Preface from Previous Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx ■ CHAPTER 1 What We Do Wrong: Web Antipatterns Explained. . . . . . . . . . 1 What This Book Is About . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What This Chapter Is About . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Challenges of Web Application Development. . . . . . . . . . . . . . . . . . . . . . . . . 4 Enterprise Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Application Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 An Introduction to Patterns and Antipatterns . . . . . . . . . . . . . . . . . . . . . . . . . 8 Web Application Antipatterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Concern Slush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Tier Leakage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Hardwired . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Validation Confusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Tight-Skins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Data Madness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Antipatterns,JOS Frameworks,and Economics . . . . . . . . . . . . . . . . . . . . . 24 The JavaEdge Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 ■ CHAPTER 2 Struts Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 The JavaEdge Application Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Using Struts to Implement the MVC Pattern . . . . . . . . . . . . . . . . . . . . 34 Getting Started:The JavaEdge Source Tree . . . . . . . . . . . . . . . . . . . . 37 The Power of the Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . 48 vii Ch00_7389_CMP3 9/28/06 8:37 PM Page viii viii ■ CONTENTS Constructing the Presentation Tier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 The JavaEdge Home Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Bean Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Logic Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Iteration Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Conditional Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Movement Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 ■ CHAPTER 3 Form Presentation and Validation with Struts. . . . . . . . . . . . 75 Problems with Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Using Struts for Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Implementing Form Validation with Struts . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The struts-config.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Struts ActionForm Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Prepopulating an ActionForm with Data . . . . . . . . . . . . . . . . . . . . . . . 87 Another Technique for Prepopulation . . . . . . . . . . . . . . . . . . . . . . . . . 91 Prepopulating a Form the Correct Way . . . . . . . . . . . . . . . . . . . . . . . . 92 Validating the Form Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 The Struts HTML Tag Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Setting Up a Struts HTML Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Using Text and TextArea Input Fields . . . . . . . . . . . . . . . . . . . . . . . . . 107 Drop-Down Lists,Checkboxes,and Radio Buttons . . . . . . . . . . . . . 108 Building More Dynamic ActionForms . . . . . . . . . . . . . . . . . . . . . . . . 111 ActionForms and Business Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 ■ CHAPTER 4 Managing Business Logic with Struts. . . . . . . . . . . . . . . . . . . . 123 Business Logic Antipatterns and Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Concern Slush and Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Tier Leakage and Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Separating Business Logic from Struts . . . . . . . . . . . . . . . . . . . . . . . 132 Implementing the Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Implementing the Business Delegate Pattern . . . . . . . . . . . . . . . . . 135 Implementing the Service Locator Pattern . . . . . . . . . . . . . . . . . . . . 141 The Service Locator Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 EJBs and Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Handling Exceptions in the Action Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Exception Handling in Struts 1.0.x . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Ch00_7389_CMP3 9/28/06 8:37 PM Page ix ■ CONTENTS ix Exception Handling in Struts 1.1 and Later . . . . . . . . . . . . . . . . . . . 163 Rewriting the ApplicationException Class . . . . . . . . . . . . . . . . . . . . . 164 Setting Up the struts-config.xml File . . . . . . . . . . . . . . . . . . . . . . . . . 164 Writing a Custom ExceptionHandler . . . . . . . . . . . . . . . . . . . . . . . . . 167 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 ■ CHAPTER 5 Architecting the Data Access Tier with ObjectRelationalBridge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Developing a Data Access Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 The JavaEdge Data Access Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Value Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 The JavaEdge Value Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Using an O/R Mapping Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Setting Up the Object/Relational Mappings . . . . . . . . . . . . . . . . . . . 190 Which Sequence Manager to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . 201 OJB in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Retrieving Data:A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Retrieving Data:A More Complicated Example . . . . . . . . . . . . . . . . 215 Storing Data Using OJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Deleting Data with OJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Bringing It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 ■ CHAPTER 6 Building Flexible Front-Ends with the Tiles Framework. . 225 What Is the Tiles Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Enabling Struts Version 1.1 to Use Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Configuring the Tiles Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 The tiles-defs.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Adding the Tiles TLDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Your First Tiles Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 What Are Tiles Definitions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Tiles Definitions:A JSP-Based Approach . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Overriding the Attribute Values in a Tiles Definition . . . . . . . . . . . . 238 Using Dummy Values in Your Tiles Definition . . . . . . . . . . . . . . . . . . 239 Disadvantages of JSP Tiles Definitions . . . . . . . . . . . . . . . . . . . . . . . 239 Anatomy of the tiles-defs.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Inheritance Using Tiles Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Extending a Tiles Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Modifying the template.jsp File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

Description:
using the Apache Struts framework along with some Ajax. Assistant Production Director: Kari Brooks-Copony worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, Coauthor, Professional Oracle 8i Application Programming with Java, PL/SQL, and XML. (Wrox Press, 2001).
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.