Grails: a Quick-Start Guide
Total Page:16
File Type:pdf, Size:1020Kb
What Readers Are Saying About Grails: A Quick-Start Guide This book, like Grails, is common sense distilled. You’ll be productive in Grails in no time. Scott Davis Founder, ThirstyHead.com This book stands heads and shoulders above other Groovy and Grails books available today. Dave’s practical, hands-on approach will teach you the nuts and bolts of the language and framework and then lead you through a project, step-by-step. This mix of instruction and prac- tice is the perfect introduction to both Groovy and Grails. Jared Richardson Consultant, Agile Artisans.com The Grails web framework is all about productivity, and so is Grails: A Quick-Start Guide. Dave Klein builds a serious application through- out the chapters, as if you were working with a colleague teaching you new technology. This guide will get you productive in hours, not weeks, and thanks to Dave’s humor, you’re really going to enjoy learn- ing Grails. If you need to dive into Grails for your next project, this book is for you! Guillaume Laforge Groovy project manager, SpringSource Dave Klein’s book is an enjoyable read that presents an efficient path to get from Grails novice to productive programmer. Anyone develop- ing a web application to run on a JVM should read this book. Steven Harris Director of engineering, Terracotta This book was an excellent guide for me as a first-time user of Grails as well as Groovy. Building an entire project while learning is a big asset: it is one thing to read and learn; it is another to learn by exam- ple. The book presents the subject matter creatively and simplifies it. It is definitely a recommended guide to those beginners who are ready to take on a challenge with Grails and Groovy. Amer Ghumrawi Programmer/analyst, WinWholesale, Inc. I’ve always believed that a good programmer finds the information they need when they need it. Nothing could be more true to that state- ment than with this book. I am new to Grails development and was looking for a good book/reference guide. I found it in Grails: A Quick- Start Guide. Even after reading it, I found myself referring to it often to help me along. It was not written at a level that assumes the reader is an expert Java developer or familiar with the popular frameworks. I found it easy to understand, and the code examples were excellent in displaying the ease with which a relative newcomer can become a Groovy developer using Grails. I highly recommend this book for any- one who is just starting to develop Grails applications. Doug Burns Programmer/analyst I’ve read several books on the Grails framework, and this is the first that explained things enough that I felt confident building something from scratch. If you know Ruby on Rails, you should definitely look at this framework, and this book really helps you get your feet wet. Brian Hogan Rails consultant and trainer Great book! Dave does a fantastic job of presenting the framework in an easy-to-follow and very accessible way. Excellent! Jeff Brown Core Grails developer Grails A Quick-Start Guide Dave Klein The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2009 Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-46-8 ISBN-13: 978-1-934356-46-3 Printed on acid-free paper. P1.0 printing, October 2009 Version: 2009-10-19 Contents 1 Introduction 10 1.1 Let Me Tell You About Grails. ............. 10 1.2 How Does Grails Do It? ................... 11 1.3 Why This Book? ....................... 12 1.4 Who Should Read This Book ............... 13 1.5 Source Code ......................... 13 1.6 Enough Groovy to Be Dangerous ............. 14 1.7 Groovy Syntax Compared to Java ............ 14 1.8 Groovy Strings ........................ 16 1.9 Groovy Closures ....................... 17 1.10 Groovy Collections ..................... 18 1.11 Where to from Here? .................... 22 1.12 Acknowledgments ...................... 23 2 Our Project 25 2.1 Introducing TekDays.com ................. 26 2.2 Meet Our Customer ..................... 26 2.3 Iteration Zero ........................ 28 2.4 Summary ........................... 34 3 Laying the Foundation 35 3.1 Creating a Domain Class .................. 35 3.2 More About Domain Classes ............... 37 3.3 Testing Our Domain Class ................. 38 3.4 Taking Control of Our Domain .............. 41 3.5 Modifying Code That Doesn’t Exist ............ 42 3.6 Bootstrapping Some Test Data .............. 45 3.7 Summary ........................... 49 CONTENTS 8 4 Building Relationships 53 4.1 The TekUser Domain Class ................ 53 4.2 One-to-One Relationships ................. 55 4.3 One-to-Many Relationships ................ 60 4.4 Collections of Simple Data Types ............. 62 4.5 Adding a Sponsor Class .................. 64 4.6 Many-to-Many Relationships ............... 66 4.7 Finishing Up the Domain Model ............. 71 4.8 Summary ........................... 73 5 Beyond Scaffolding 75 5.1 Generating Scaffolding Code ............... 75 5.2 Anatomy of a Grails Controller .............. 76 5.3 Grails Views with Groovy Server Pages .......... 83 5.4 Configuring a Database .................. 94 5.5 Summary ........................... 97 6 Getting Things Done 98 6.1 Changing All Our Views at Once ............. 98 6.2 Modifying the Scaffolded Views .............. 99 6.3 Event Task List ....................... 106 6.4 Grails Service Classes ................... 108 6.5 Integration Testing ..................... 111 6.6 Modifying the Task Class ................. 113 6.7 Summary ........................... 114 7 Forum Messages and UI Tricks 116 7.1 Restricting Messages to an Event ............. 116 7.2 Of Templates and Ajax ................... 122 7.3 Display Message Threads with a Custom Tag . 128 7.4 Summary ........................... 133 8 Knock, Knock: Who’s There? Grails Security 135 8.1 Grails Security Options .................. 135 8.2 LoggingIn .......................... 136 8.3 Filters ............................. 139 8.4 Logging Out ......................... 142 8.5 Summary ........................... 144 CONTENTS 9 9 Big-Picture Views 146 9.1 Home Page Makeover .................... 146 9.2 Creating a New Controller ................. 149 9.3 Designing the Dashboard View .............. 150 9.4 Adding the Dashboard Action ............... 156 9.5 Adding a Menu ....................... 158 9.6 Linking to the Dashboard ................. 160 9.7 Summary ........................... 162 10 Seek, and You Shall Find 163 10.1 Search Using Dynamic Finders .............. 163 10.2 Hibernate Criteria Builder ................. 166 10.3 The Big Guns: The Searchable Plug-In .......... 170 10.4 Summary ........................... 177 11 Icing on the Cake 178 11.1 The Grails UI Plug-In .................... 178 11.2 The Twitter Plug-In ..................... 183 11.3 Making the Event Page Customizable with the Blurb Plug-In ............................ 189 11.4 User-Friendly URLs ..................... 193 11.5 Summary ........................... 196 12 Deployment and Beyond 198 12.1 Using a JNDI Data Source ................. 198 12.2 Creating and Deploying a WAR .............. 200 12.3 Next Steps .......................... 201 12.4 Parting Thoughts ...................... 202 A Additional CSS Rules 203 B Resources 205 B.1 Online Resources ...................... 205 B.2 Meet the G3 Community .................. 206 B.3 Other Resources ...................... 210 B.4 IDE Support ......................... 211 C Bibliography 213 Index 214 Chapter 1 Introduction 1.1 Let Me Tell You About Grails... Web development is a very rewarding experience. Building an applica- tion that can run from anywhere in the world is pretty awesome. Even in a corporate environment, you can deliver new features to your users, no matter where they are located, without ever touching their computer. It’s a beautiful thing. Consider also what you can build: the potential for creativity on the Web is unlimited. The Java platform brings even more power to the party. The Java Serv- let API and the plethora of libraries and frameworks in the Java ecosys- tem make it possible to include almost any feature you could want in a web application. It is an exciting time to be a web developer. However, it’s not all sweetness and light. With all this power comes a level of complexity that can be daunting. With most