CYAN YELLOW MAGENTA BLACK PANTONE 123 CV EMPOWERING JAVA DEVELOPERS WITH AJAX TECHNIQUES THE EXPERT’S VOICE® IN WEB DEVELOPMENT Companion eBook Available Pro Ajax and Java P Dear Reader, r If you are a professional Java developer and you want to know how best to go about adding Ajax functionality to your web applications to enrich your users’ o experience, then Pro Ajax and Javais the right book for you. Ajax allows us to Nathaniel T. Schutta, build highly interactive, desktop-like web applications that can be deployed Pro Ajax coauthor of through any modern web browser without the need for special plug-ins. Though A Foundations of Ajax Ajax is server-side agnostic, it certainly plays well within the rich Java ecosystem. This book teaches you what you need to know by using tightly focused exam- j ples that aren’t cluttered with extraneous details, putting you on the fast track to a using Ajax in your own Java applications. You already know how to develop web applications, so this book focuses on teaching the killer techniques you’ll need x to bring your applications to life with Ajax, including autocomplete, form vali- dation, dynamic visual effects, and much, much more. We start off by providing a brief summary of the subject area, followed by a a guide to creating the perfect development environment for developing Ajax Java n in Java, including code editing and formatting, DOM inspection, debugging, Ryan Asleson,coauthor of testing, and logging tools. d Next, we look in depth at the most popular Ajax libraries and toolkits avail- Foundations of Ajax and able, and what they can do to simplify and speed up our Ajax development— Prototype, script.aculo.us, Dojo, DWR, AjaxTags, and Taconite. In the final J RELATED TITLE section, we explore Ajax development within four of the most popular Java frameworks—Spring, Struts, Tapestry, and JSF. a Practical Ajax Projects with Java Thanks for picking up this book. We hope you’ll have as much fun reading it as we had writing it! v Nate Schutta and Ryan Asleson a Join online discussions: forums.apress.com FOR PROFESSIONALS BY PROFESSIONALS™ THE APRESS PRO AJAX AND JAVA ROADMAP Covers everything Java developers need Companion eBook Beginning JavaScript with to know to add Ajax to their websites! Ajax Patterns DOM Scripting and Ajax: and Best Practices From Novice to Professional Pro JavaScript Techniques Beginning Pro JSF and Ajax: CSS Web Development Building Rich From Novice to Professional Internet Components See last page for details Foundations of Ajax on $10 eBook version Beginning POJOs Pro Ajax and Java Nathaniel T. Schutta Ryan Asleson AS and SOURCECODEONLINE sc www.apress.com ISBN 1-59059-677-3 leh su 54999 ott na US $49.99 , Shelve in Java Programming User level: Intermediate–Advanced 6 89253 59677 7 9 781590 596777 this print for content only—size & color not accurate spine = 0.784" 336 page count Asleson _677-3FRONT.fm Page i Tuesday, June 20, 2006 6:58 AM Pro Ajax and Java ■ ■ ■ Nathaniel T. Schutta and Ryan Asleson Asleson _677-3FRONT.fm Page ii Tuesday, June 20, 2006 6:58 AM Pro Ajax and Java Copyright © 2006 by Nathaniel T. Schutta and Ryan Asleson 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-677-7 ISBN-10 (pbk): 1-59059-677-3 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: Chris Mills Technical Reviewer: John R. 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 Manager: Richard Dal Porto Copy Edit Manager: Nicole LeClerc Copy Editor: Hastings Hart Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Susan Glinert Proofreader: April Eddy Indexer: Lucie Haskins Artist: Susan Glinert 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.com in the Source Code section. Asleson _677-3FRONT.fm Page iii Tuesday, June 20, 2006 6:58 AM To Christine, for always believing. —Nathaniel T. Schutta For Sara, the love of my life, and Adam, my favorite playmate. —Ryan Asleson Asleson _677-3FRONT.fm Page iv Tuesday, June 20, 2006 6:58 AM Asleson _677-3FRONT.fm Page v Tuesday, June 20, 2006 6:58 AM Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii PART 1 Introducing Ajax ■ ■ ■ ■ CHAPTER 1 What Is Ajax? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■ CHAPTER 2 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 PART 2 Libraries and Toolkits ■ ■ ■ ■ CHAPTER 3 Java-Agnostic Toolkits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 ■ CHAPTER 4 Java-Specific Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 PART 3 Web Frameworks ■ ■ ■ ■ CHAPTER 5 Struts and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 ■ CHAPTER 6 Tapestry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 ■ CHAPTER 7 Spring and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 ■ CHAPTER 8 JavaServer Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 v Asleson _677-3FRONT.fm Page vi Tuesday, June 20, 2006 6:58 AM Asleson _677-3FRONT.fm Page vii Tuesday, June 20, 2006 6:58 AM CCoonntteennttss About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii PART 1 Introducing Ajax ■ ■ ■ ■ CHAPTER 1 What Is Ajax? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Rise of the Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 And Then There Was Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The XMLHttpRequest Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Methods and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 An Example Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Avoiding Common Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Ajax Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 The Fade Anything Technique (FAT). . . . . . . . . . . . . . . . . . . . . . . . . . 21 Auto Refresh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Partial Page Paint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Draggable DOM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 ■ CHAPTER 2 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 JavaScript Source Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 JSEclipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 NetBeans JavaScript Plug-in. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 JavaScript Compression and Obfuscation . . . . . . . . . . . . . . . . . . . . . . . . . 35 The Dojo Toolkit’s JavaScript Compressor . . . . . . . . . . . . . . . . . . . . 37 Inspecting a DOM Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Mouseover DOM Inspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Debugging Ajax Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 NetBeans HTTP Monitor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Firefox FireBug Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 vii Asleson _677-3FRONT.fm Page viii Tuesday, June 20, 2006 6:58 AM viii ■ CONTENTS JavaScript Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Log4JS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Lumberjack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 JavaScript Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Using Venkman. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Testing Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 JsUnit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Selenium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 PART 2 Libraries and Toolkits ■ ■ ■ ■ CHAPTER 3 Java-Agnostic Toolkits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 $(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Working with Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Manipulating the DOM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Try: Simplified Browser Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Ajax Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 script.aculo.us . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Autocomplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Dojo Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 dojo.io.bind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Taconite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Taconite on the Client Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Taconite on the Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Getting Started with Taconite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 ■ CHAPTER 4 Java-Specific Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Installation Verification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 JavaScript Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Asleson _677-3FRONT.fm Page ix Tuesday, June 20, 2006 6:58 AM ■ CONTENTS ix AjaxTags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 The Ajax “Killer Application”. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 AjaxTags Autocomplete Component. . . . . . . . . . . . . . . . . . . . . . . . . 142 Other Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 PART 3 Web Frameworks ■ ■ ■ ■ CHAPTER 5 Struts and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Struts Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Ajax Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Struts Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Struts and Ajax Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Ajax-Powered Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Implementing Struts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Struts and Ajax Design Considerations . . . . . . . . . . . . . . . . . . . . . . 179 The Future of Struts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Struts 1.3 and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Struts Shale. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Struts Ti. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 ■ CHAPTER 6 Tapestry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 What Is Tapestry? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Calling the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Tapestry Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Tapestry Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Tapestry and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Tacos Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Setting Up Tacos. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Using a Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Enabling Debug Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Modifying the Form Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Description: