Beginning Groovy and Grails from Novice to Professional
Total Page:16
File Type:pdf, Size:1020Kb
Beginning Groovy and Grails From Novice to Professional Christopher M. Judd, Joseph Faisal Nusairat, and James Shingler Beginning Groovy and Grails: From Novice to Professional Copyright © 2008 by Christopher M. Judd, Joseph Faisal Nusairat, James Shingler 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-4302-1045-0 ISBN-13 (electronic): 978-1-4302-1046-7 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 Editors: Steve Anglin, Matthew Moodie Technical Reviewer: Guilliaume Laforge Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Senior Project Manager: Kylie Johnston Copy Editors: Nicole Abramowitz, Marilyn Smith Associate Production Director: Kari Brooks-Copony Senior Production Editor: Laura Cheu Compositor: Kinetic Publishing Services, LLC Proofreader: Liz Welch Indexer: Julie Grady 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 [email protected], or visit 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. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales—eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion 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. To my supportive wife and best friend, Sue. To my son, Blake, who always makes me laugh. To all the individuals and organizations who have contributed to making Groovy and Grails amazing. And to my Heavenly Father, for all the blessings He has bestowed upon my family and me. —Chris To my family, for their love and support. And to my brother, Specialist Adam Nusairat, who is currently deployed to Afghanistan: stay safe; we miss you. —Joseph To my wonderful wife,Wendy, and my son, Tyler. None of this would have been possible without your love, support, and understanding. I love you! —Jim Contents at a Glance Foreword . xv About the Authors . xvii About the Technical Reviewer . xix Acknowledgments . xxi Introduction . xxiii ■CHAPTER 1 Introduction to Groovy . 1 ■CHAPTER 2 Groovy Basics . 11 ■CHAPTER 3 More Advanced Groovy . 47 ■CHAPTER 4 Introduction to Grails . 63 ■CHAPTER 5 Building the User Interface. 105 ■CHAPTER 6 Building Domains and Services . 165 ■CHAPTER 7 Security in Grails . 215 ■CHAPTER 8 Web 2.0—Ajax and Friends . 257 ■CHAPTER 9 Web Services. 295 ■CHAPTER 10 Reporting . 311 ■CHAPTER 11 Batch Processing . 337 ■CHAPTER 12 Deploying and Upgrading . 353 ■CHAPTER 13 Alternative Clients . 367 ■INDEX . 399 v Contents Foreword . xv About the Authors . xvii About the Technical Reviewer . xix Acknowledgments . xxi Introduction . xxiii ■CHAPTER 1 Introduction to Groovy. 1 Groovy Language Features . 2 Groovy Installation. 2 Groovy by Example . 3 Converting Java to Groovy . 5 Converting a JavaBean to a GroovyBean. 5 Simplifying the Code . 6 Using Groovy Collection Notation and Closure . 8 Getting Rid of Main() . 8 Summary . 9 ■CHAPTER 2 Groovy Basics . 11 Scripts . 11 Using Script Functions . 12 Compiling Groovy. 13 Running Groovy . 14 Assertions . 17 Strings . 18 String Interpolation . 20 Multiline Strings . 21 Slashy Strings. 22 Methods and Closures . 23 Methods. 23 Closures. 24 vii viii ■CONTENTS Collections . 27 Lists . 27 Ranges. 28 Sets . 30 Arrays. 31 Maps . 32 Regular Expressions . 33 Groovy Regular Expression Operators . 36 Common Uses of Regular Expressions. 38 Operators . 40 Operator Overloading . 40 Specialized Operators . 41 Summary . ..