Beginning Groovy, Grails and Griffon Vishal Layka Christopher M. Judd Joseph Faisal Nusairat Jim Shingler
Contents J About the Authors About the Technical Reviewer Acknowledgments xv xvii xix Chapter 1: Introduction to Groovy 1 Installation 1 Groovy by Example 2 Converting Java to Groovy 3 Converting a JavaBean to a GroovyBean 4 Simplifying the Code 5 Using Groovy Collection Notation and Closure 5 Getting Rid of maino 6 Groovy Language Key Features 7 Assertion 7 Annotations for AST Transformation 7 Builder 8 Closure 8 GDK 8 Metaprogramming 8 Native Support for Lists and Maps 10 Native Support for Regular Expression 10 Object Orientation: Everything is an Object in Groovy 10 String interpolation 10 Static and Dynamic Typing 11 Static Type Checking 11 V
il CONTENTS Static Compilation 12 Optional Syntax 12 Summary 14 Chapter 2: Groovy Basics 15 Scripts 15 Using Script Functions 16 Compiling Groovy 16 Running Groovy 17 Assertions 21 Power Asserts 22 Strings 22 GStrings 23 Single Line Strings 23 Multiline Strings 24 Slashy Strings 25 Multiline Slashy Strings 25 Dollar Slashy Strings 26 Regular Expressions, 28 Groovy Regular Expression Operators 30 Common Uses of Regular Expressions 32 Collective Datatypes 33 Arrays 33 Lists 34 Maps 35 Ranges 36 Sets 37 Control Structure 38 Groovy Truth 38 Logical Branching 39 Looping 39 Exception Handling 40 vi
CONTENTS Methods 41 Closures 41 Implicit Variables 44 Explicit Declaration of Closure 46 Reusing the Method as a Closure 46 Closures and Collection 47 Closures as Map Keys and Values 48 Currying Closure 49 Closure Trampoline 50 Closure Memoization 50 Operators 51 Operator Overloading 51 Specialized Operators 52 Summary 56 Chapter 3: More Advanced Groovy 57 Object Orientation in Groovy 57 Classes and Scripts 57 Groovy Constructors 59 Inheritance 61 Polymorphism 64 Groovy Unit Testing 64 Working with XML 66 Writing XML with Java 66 Groovy Builders 68 Writing XML with Groovy MarkupBuilder 69 Reading XML with XmlSlurper 69 Native JSON support 69 Reading JS0N 70 JsonBuilder 70 Prettyprinting JSON Content 70 vii
CONTENTS Generating Text with Templates 71 Runtime Metaprogramming 73 Metaobject Protocol 73 Metaclass 73 Using invokemethodo and get/setpropertyo 74 Using methodmissingo 75 Expandos 76 ExpandoMetaClass 76 Categories 79 Runtime Mixins 80 Compile-time Metaprogramming 83 AST Transformations 83 Built-in AST Transformations 83 Domain-Specific Languages 88 Summary 88 Chapter 4: Introduction to Grails 89 What Is Grails? 89 Grails Features, 90 Integrated Open Source 91 Grails Architecture 93 Installing Grails 94 Collab-Todo Application 94 Creating Collab-Todo application 97 Running the Application 99 Creating the Controller 101 Testing the Controller 105 Scaffolding 107 Creating a Domain Class 107 Dynamic Scaffolding 109 Static Scaffolding 113 Creating Domain Relationships 116 viii
CONTENTS H2 Console 119 Grails Plugins 121 Grails Interactive Mode 123 Summary 124 Chapter 5: Building the User Interface 125 Starting with the End in Mind 125 Creating the Footer 126 Creating the Topbar 128 Adding More Look and Feel 130 Grails Tags 133 Static Resources 137 Making the Topbar Functional 138 The Login View, 138 The login Action 140 Handling the Login and Logout Actions 140 Externalizing Strings 141 Errors and Validation 143 Flash and Flash Messages 145 Creating an Audit Log Using Action Interceptors 149 Using Filters 151 Summary 154 Chapter 6: Building Domains and Services 155 GORM 155 Collab-Todo's Domain 156 Creating Domain Objects 157 Basic Domain Creation 158 Creating Relationships 160 IX
CONTENTS Overwriting Default Settings 163 Adjusting the Mappings 163 Validation 170 Constraints 171 Calling the Validator 174 Validation Messages 174 Querying the Database 175 GORM's CRUD Support 175 Creating Queries 176 Services 185 Creating a Service 185 Calling the Service 185 Injecting into the Service 186 Initializing the Service 186 Setting a Bean to Be Transactional 187 Service Context Available in the Service 187 Summary 188 Chapter 7: Security in Grails 189 What Is Security? 189 Authentication 190 Access Control 191 An Overview of Grails Security Solutions 193 Custom Security Implementation 194 Registering a User 195 Logging In and Out 200 Securing the Controllers 202 CAS 204 CAS Installation 205 CAS Configuration 205 CAS Usage 206 X
II CONTENTS Spring Security 207 Security Domain Classes 207 Securing URLs 212 SecurityTagLib 214 Summary 215 Chapter 8: Web 2.0 Ajax and Friends 217 Advanced Presentation Components 217 Adding RichText Capabilities 217 Adding Search Capabilities 219 Allowing File Uploads 221 Tag Libraries 228 Creating the Tag Library 228 Referencing the Tag Library 230 Ajax in Grails 230 Using Ajax Frameworks in Grails 231 Dynamic Rendering of Data 234 RSS Feeds 237 Summary 240 Chapter 9: Web Services 241 RESTful Web Services 241 RESTful in Grails 245 URL Mappings 245 Content Negotiation 247 RestController 249 Summary 255 Chapter 10: Reporting 257 The Report 257 Reporting Tools 259 Overview 259 Installing JasperReports and ireports 260 xi
1 CONTENTS Creating the Todo Report 261 Defining the Data Source 261 Using ireports 262 Enhancing the Report 265 Compiling the Report 266 The Report Tag 267 Tag Library Overview 267 Creating the Tag 267 The ReportController and the ReportService 269 Tying It All Together 274 Gathering the Report Data 274 Adding the Report Tag to the Application 274 The Report List 275 An Alternate Approach 276 Summary 277 Chapter 11: Batch Processing 279 Installing the Quartz Plugin 279 Creating a Job 280 Building a Batch-Reporting Facility 282 Creating a Nightly Reporting Job 283 Retrieving the User's Todos 285 Invoking the ReportService 287 Invoking the E-Mail Service 288 Summary 289 Chapter 12: Deploying and Upgrading Grails Applications 291 Deploying Grails Applications 291 Using Environments 291 Understanding Grails Configurations 293 Packaging the Application for Deployment 298 Deploying to an Application Server 298 xii
CONTENTS Upgrading Grails Applications 301 Configuration Changes 302 Command Line Changes 302 H2 302 jquery 302 Controller Public Methods 303 Unit Test Framework 303 Summary 303 Chapter 13: Introduction to Griffon 305 Installing Griffon 305 Creating Your First Application 306 Directory Structure 307 Griffon Application Life Cycle 315 Initialization 315 Startup 316 Ready 316 Shutdown 316 Stop 316 Views 317 Builder Overview 317 Griffon View 319 Special Nodes 323 Models and Bindings 325 Controllers 328 Injected Properties 328 Injected Methods 328 Initialization and Destruction Hook 330 Actions 331 Summary 331 Index 333 xiii