CYAN YELLOW MAGENTA BLACK PANTONE 123 CV EMPOWERING PRODUCTIVITY FOR THE JAVA™ DEVELOPER THE EXPERT’S VOICE® IN JAVA™ TECHNOLOGY Companion eBook Available IInncclluuddeess Pro Wicket SSpprriinngg aanndd AAjjaaxx P Dear Reader, Welcome to Wicket, an open source, lightweight, component- or POJOs-based r Java™web framework that brings the Java Swing event-based programming Pro model to web development. Component-based web frameworks are being o touted as the future of Java web development, and Wicket is easily one of the leading implementations in this area. Wicket strives for a clean separation of the roles of HTML page designer and Java developer by supporting plain vanilla W HTML templates that can be mocked up, previewed, and later revised using standard WYSIWYG HTML design tools. Wicket counters the statelessness of HTTP by providing stateful components, thereby improving productivity. If you are looking to hone your object-oriented Wicket programming skills, Wicket fits well in that regard, since it has an architecture i and rich component suite that encourages clean object-oriented design. c Pro Wicketaims to get you up and running quickly with this framework. You will learn how to configure Wicket and then gradually gain exposure to the k “Wicket way” of addressing web development requirements. You will learn about important techniques of working with Wicket through simple examples. People have come to expect a few things from a modern web framework— e Spring framework integration and baked-in Ajax support are probably at the top of that list. I have taken care to address these aspects of Wicket in the book. t You will learn to integrate Wicket and the EJB™3 API using the services of Spring 2, for example. There is also a separate chapter dedicated to Wicket’s integration with Ajax. I have been having a great time with Wicket since day one of my adoption. I wrote this book to let you know how Wicket, in addition to being a robust web application framework, succeeds in bringing back the fun that has been missing in the Java web development space. Join online discussions: Karthik Gurumurthy Explore this leading open source,lightweight, forums.apress.com FOR PROFESSIONALS component-based POJO web development framework BY PROFESSIONALS™ THE APRESS JAVA™ ROADMAP Companion eBook Pro Apache Geronimo Beginning Hibernate Pro Hibernate 3 Beginning POJOs Beginning Spring 2 Pro Spring See last page for details on $10 eBook version G Pro Wicket u r u Karthik Gurumurthy m ISBN 1-59059-722-2 u SOURCECODEONLINE r www.apress.com 90000 th y Shelve in Java Programming User level: Intermediate 6 89253 59722 4 9 781590 597224 this print for content only—size & color not accurate 7" x 9-1/4" / CASEBOUND / MALLOY (0.8125 INCH BULK -- 328 pages -- 60# Thor) 7222FM.qxd 8/8/06 11:20 AM Page i Pro Wicket Karthik Gurumurthy 7222FM.qxd 8/8/06 11:20 AM Page ii Pro Wicket Copyright © 2006 by Karthik Gurumurthy 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: 978-1-59059-722-4 ISBN-10: 1-59059-722-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication data is available upon request. 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 US 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 Reviewers: David Heffelfinger, Igor Vaynberg 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: Kylie Johnston Copy Edit Manager: Nicole LeClerc Copy Editor: Ami Knox Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Dina Quan Proofreader: Lori Bring Indexers: Toma Mulligan, Carol Burbo 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 indi- rectly 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 section. 7222FM.qxd 8/8/06 11:20 AM Page iii To Amma and Appa for everything! And to my wonderful wife,Gayathri 7222FM.qxd 8/8/06 11:20 AM Page iv 7222FM.qxd 8/8/06 11:20 AM Page v Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■ CHAPTER 1 Wicket:The First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 Validation with Wicket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 ■ CHAPTER 3 Developing a Simple Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ■ CHAPTER 4 Providing a Common Layout to Wicket Pages . . . . . . . . . . . . . . . . . 117 ■ CHAPTER 5 Integration with Other Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 ■ CHAPTER 6 Localization Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 ■ CHAPTER 7 Custom Wicket Components and Wicket Extensions . . . . . . . . . . . 199 ■ CHAPTER 8 Wicket and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 ■ CHAPTER 9 Additional Wicket Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 v 7222FM.qxd 8/8/06 11:20 AM Page vi 7222FM.qxd 8/8/06 11:20 AM Page vii Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix ■ CHAPTER 1 Wicket: The First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is Wicket? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Obtaining and Setting Up Wicket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Eclipse Development Environment Setup Using Quick Start . . . . . . . . . . . . 2 Running the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 How to Alter the Jetty Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The web.xml for Wicket Web Development . . . . . . . . . . . . . . . . . . . . . 4 Developing a Simple Sign-in Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Wicket Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 What Happened on Form Submit? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 How Does PropertyModel Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 How to Specify a CompoundPropertyModel for a Page . . . . . . . . . . . . . . . 16 Development vs.Deployment Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Displaying the Welcome Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Adding a Link to the Welcome Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Adding Basic Authentication to the Login Page . . . . . . . . . . . . . . . . . . . . . . 32 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 ■ CHAPTER 2 Validation with Wicket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Providing User Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 More Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Using Wicket Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Writing Custom Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Globally Registering a Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Registering String Converters Globally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 vii 7222FM.qxd 8/8/06 11:20 AM Page viii viii ■ CONTENTS How Wicket’s FormValidator Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 How to Set Session-Level Feedback Messages . . . . . . . . . . . . . . . . . . . . . 60 Changing Feedback Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 How the ListView Components Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 ■ CHAPTER 3 Developing a Simple Application . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Securing Wicket Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Nice Wicket URLs and Mounted Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Accessing Wicket Application Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Developing an Online Bookstore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Where to Store Global Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Books on Display at the Online Bookstore . . . . . . . . . . . . . . . . . . . . . . . . . . 78 How IDataProvider Allows for Pagination of Data . . . . . . . . . . . . . . . 79 What Is AbstractDetachableModel? . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 What Is LoadableDetachableModel? . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Wicket Pages and User Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Using Wicket Behaviors to Add HTML Attributes to the Table Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Implementing the Checkout Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Implementing the Remove Book Functionality . . . . . . . . . . . . . . . . 107 Checkout Confirmation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 IAuthorizationStrategy and Conditional Component Instantiation . . . . . 112 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 ■ CHAPTER 4 Providing a Common Layout to Wicket Pages . . . . . . . . . . . 117 Adding “Books,”“Promotions,”and “Articles”Links to the Bookstore Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Providing a Common Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Getting the Pages to Display Corresponding Titles . . . . . . . . . . . . . . . . . . 125 Separating Navigation Links and the Associated Page Content Through Border Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Disabling Links to the Page Currently Being Displayed . . . . . . . . . . . . . . 135 Employing wicket:link to Generate Links . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Borders Are Not Just About Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 7222FM.qxd 8/8/06 11:20 AM Page ix ■ CONTENTS ix ■ CHAPTER 5 Integration with Other Frameworks . . . . . . . . . . . . . . . . . . . . . . 143 Wicket and Velocity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Wicket and FreeMarker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 The Spring Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Difficulties in Spring Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Wicket Is an Unmanaged Framework . . . . . . . . . . . . . . . . . . . . . . . . 150 DI Issue Due to Wicket Model and Component Serialization . . . . . 151 Accessing the Spring ApplicationContext Through the WebApplication Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Configuring Injection Through an IComponentInstantiationListener Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Specifying Target Field for Dependency Injection . . . . . . . . . . . . . . . . . . . 159 Specifying Spring Dependency Through Java 5 Annotation . . . . . . . . . . 160 Spring Integration Through Commons Attributes . . . . . . . . . . . . . . . . . . . 164 How Wicket Integrates with EJB 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 A Quick EJB 3 Entity Bean Refresher . . . . . . . . . . . . . . . . . . . . . . . . 165 Choosing an EJB3 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Defining the persistence.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 How Spring 2.0 Further Simplifies EJB 3 Programming . . . . . . . . . . . . . 172 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 ■ CHAPTER 6 Localization Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Localization Through the <wicket:message> Tag . . . . . . . . . . . . . . . . . . 177 Sources for Localized Messages and Their Search Order . . . . . . . . . . . . 179 How to Switch the Locale Programmatically . . . . . . . . . . . . . . . . . . . . . . . 183 How to Localize Validation and Error Messages . . . . . . . . . . . . . . . . . . . . 186 Putting Wicket’s StringResourceModel to Work . . . . . . . . . . . . . . . . 191 Locale-Specific Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Support for Skinning and Variation in Addition to Locale-Specific Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Loading Messages from a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Description: