CYAN YELLOW MAGENTA BLACK PANTONE 123 C EMPOWERING PRODUCTIVITY FOR THE JAVA™ DEVELOPER THE EXPERT’s VOICE® IN JAVA™ TECHNOLOGY Companion eBook Available Practical Apache Struts2 Web 2.0 Projects Practical Dear reader, A Apache Struts2 is the first major release of the most popular open source Java™ p web application framework ever. This is an important milestone for the frame- a Practical Apache work in terms of functionality but also for the improvements that have been c made to increase developer productivity. By decreasing coupling within the h framework, reducing configuration and proving default and different config- e uration options (via annotations), and providing a plug-in mechanism to easily Author of S Struts2 extend the base features, Struts2 is providing a platform that can be built upon Starting Struts2 for the next generation of web applications. t My goal in writing this book is to introduce all the features of Struts2 to you r in a practical and useful manner. I believe this approach benefits both new- comers to web development (as you will learn all the necessary steps to build u an application) and those transitioning from another technology (who will be able to see the differences between application feature implementations). t Developing a Web 2.0 application from start to finish will give you a deep s understanding of Struts2: the core architectural elements, the interactions 2 between elements, and how each element is configured. Through a hands-on Web 2.0 Projects approach, you will learn about the features of Struts2 as a complete Web 2.0 application is developed from start to finish. You will learn about the basic features W important to all web applications: data input, validation, data conversion, e internationalization, workflows/wizards, and security. Additionally, advanced b features that are the cornerstones of Web 2.0 applications—syndication, web services, and AJAX user interfaces/integration—are explained in detail. 2 I hope you enjoy developing with the Struts2 framework as much as I do. . 0 Ian Roughley P r o Learn and immediately apply the new agile open source j e THE APRESS JAVA™ ROADMAP Apache Struts2 web framework to your projects. c Companion eBook The Definitive Guide t to Grails s Practical Apache Struts2 Beginning POJOs Web 2.0 Projects See last page for details on $10 eBook version Practical JRuby on Rails Web 2.0 Projects R o Ian Roughley SOURCE CODE ONLINE ISBN-13: 978-1-59059-903-7 u www.apress.com ISBN-10: 1-59059-903-9 g h 53999 l Foreword by Patrick Lightbody, e y US $39.99 Struts2 co-creator Shelve in Java Programming User level: 9 781590 599037 Intermediate–Advanced this print for content only—size & color not accurate spine = 0.838" 360 page count 9039fm.qxd 10/29/07 3:23 PM Page i Practical Apache Struts2 Web 2.0 Projects Ian Roughley 9039fm.qxd 10/29/07 3:23 PM Page ii Practical Apache Struts2 Web 2.0 Projects Copyright © 2007 by Ian Roughley 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-903-7 ISBN-10 (pbk): 1-59059-903-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 US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was writ- ten without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Technical Reviewer: Frank Zammetti Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Candace English Copy Editor: Julie McNamee Associate Production Director: Kari Brooks-Copony Production Editor: Candace English Compositor: Linda Weidemann, Wolf Creek Press Proofreader: Lisa Hamilton Indexer: Broccoli Information Management 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], orvisit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. 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 pre- caution 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. 9039fm.qxd 10/29/07 3:23 PM Page iii For Skooter. 9039fm.qxd 10/29/07 3:23 PM Page iv 9039fm.qxd 10/29/07 3:23 PM Page v Contents at a Glance Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix nCHAPTER 1 Web 2.0 and Struts2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 nCHAPTER 2 Getting Up and Running. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 nCHAPTER 3 Framework Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 nCHAPTER 4 Application Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 nCHAPTER 5 Data Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 nCHAPTER 6 Wizards and Workflows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 nCHAPTER 7 Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 nCHAPTER 8 Searching and Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 nCHAPTER 9 Syndication and Integration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 nCHAPTER 10 AJAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 nINDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 v 9039fm.qxd 10/29/07 3:23 PM Page vi 9039fm.qxd 10/29/07 3:23 PM Page vii Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix nCHAPTER 1 Web 2.0 and Struts2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is Web 2.0? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Web Application Development 2.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Web Framework Agility with Struts2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Using this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 nCHAPTER 2 Getting Up and Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 The Build Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Maven2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 The Struts2 Starter Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The Maven2-Generated Directory and File Structure . . . . . . . . . . . . 15 The Maven2 Configuration File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Starter Application Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 nCHAPTER 3 Framework Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Walking Through a Request-Response. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The Request Initiation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 The Struts2 Servlet Filter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 The Action Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 The Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Interceptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 The Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 vii 9039fm.qxd 10/29/07 3:23 PM Page viii viii nCONTENTS Exploring the Core Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Actions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Interceptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Custom Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 The Value Stack and OGNL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Results and Result Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Tag Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Configuring the Elements of the Framework . . . . . . . . . . . . . . . . . . . . . . . . 52 The web.xml File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Zero Configuration Annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 The struts.xmlFile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Configuring the Execution Environment. . . . . . . . . . . . . . . . . . . . . . . . 65 Extending the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 nCHAPTER 4 Application Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 The Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Use Cases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Integration Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The Domain Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 An Agile Development Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Continuous Integration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Integrating the Persistence Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Configuring the Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Configuring Hibernate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Using Hibernate to Create Data Access Objects. . . . . . . . . . . . . . . . . 85 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 nCHAPTER 5 Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 The Use Case. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 CRUD Functionality. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 The Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Model-Driven Actions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Setup Code and Data Prepopulation. . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Description: