The Grails Framework - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.1.10
Total Page:16
File Type:pdf, Size:1020Kb
See the light - agile, industrial strength, rapid web application development made easy The Grails Framework - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.1.10 Table of Contents 1 Introduction 1.1 What's new in Grails 3.1? 1.1.1 Improvements to Grails 3 Profiles 1.1.2 REST API and AngularJS Profiles 1.1.3 GORM 5 Suite 1.1.4 Plugin Publishing Plugins 1.2 What's new in Grails 3.0? 1.2.1 Core Features 1.2.2 Web Features 1.2.3 Development Environment Features 1.2.4 Testing Features 2 Getting Started 2.1 Installation Requirements 2.2 Downloading and Installing 2.3 Creating an Application 2.4 A Hello World Example 2.5 Using Interactive Mode 2.6 Getting Set Up in an IDE 2.7 Convention over Configuration 2.8 Running and Debugging an Application 2.9 Testing an Application 2.10 Deploying an Application 2.11 Supported Java EE Containers 2.12 Creating Artefacts 2.13 Generating an Application 3 Upgrading from previous versions of Grails 1 3.1 Upgrading from Grails 3.0 3.2 Upgrading from Grails 2.x 3.2.1 Upgrading Plugins 3.2.2 Upgrading Applications 4 Configuration 4.1 Basic Configuration 4.1.1 Options for the yml format Config 4.1.2 Built in options 4.1.3 Logging 4.1.4 GORM 4.2 The Application Class 4.2.1 Executing the Application Class 4.2.2 Customizing the Application Class 4.2.3 The Application LifeCycle 4.3 Environments 4.4 The DataSource 4.4.1 DataSources and Environments 4.4.2 Automatic Database Migration 4.4.3 Transaction-aware DataSource Proxy 4.4.4 Database Console 4.4.5 Multiple Datasources 4.5 Versioning 4.6 Project Documentation 4.7 Dependency Resolution 5 The Command Line 5.1 Interactive Mode 5.2 Creating Custom Scripts 5.3 Re-using Grails scripts 5.4 Building with Gradle 5.4.1 Defining Dependencies with Gradle 5.4.2 Working with Gradle Tasks 5.4.3 Grails plugins for Gradle 6 Application Profiles 6.1 Creating Profiles 6.2 Profile Inheritance 6.3 Publishing Profiles 6.4 Understanding Profiles 2 6.5 Creating Profile Commands 6.6 Creating Profile Features 7 Object Relational Mapping (GORM) 7.1 Quick Start Guide 7.1.1 Basic CRUD 7.2 Domain Modelling in GORM 7.2.1 Association in GORM 7.2.1.1 Many-to-one and one-to-one 7.2.1.2 One-to-many 7.2.1.3 Many-to-many 7.2.1.4 Basic Collection Types 7.2.2 Composition in GORM 7.2.3 Inheritance in GORM 7.2.4 Sets, Lists and Maps 7.3 Persistence Basics 7.3.1 Saving and Updating 7.3.2 Deleting Objects 7.3.3 Understanding Cascading Updates and Deletes 7.3.4 Eager and Lazy Fetching 7.3.5 Pessimistic and Optimistic Locking 7.3.6 Modification Checking 7.4 Querying with GORM 7.4.1 Dynamic Finders 7.4.2 Where Queries 7.4.3 Criteria 7.4.4 Detached Criteria 7.4.5 Hibernate Query Language (HQL) 7.5 Advanced GORM Features 7.5.1 Events and Auto Timestamping 7.5.2 Custom ORM Mapping 7.5.2.1 Table and Column Names 7.5.2.2 Caching Strategy 7.5.2.3 Inheritance Strategies 7.5.2.4 Custom Database Identity 7.5.2.5 Composite Primary Keys 7.5.2.6 Database Indices 7.5.2.7 Optimistic Locking and Versioning 3 7.5.2.8 Eager and Lazy Fetching 7.5.2.9 Custom Cascade Behaviour 7.5.2.10 Custom Hibernate Types 7.5.2.11 Derived Properties 7.5.2.12 Custom Naming Strategy 7.5.3 Default Sort Order 7.6 Programmatic Transactions 7.7 GORM and Constraints 8 The Web Layer 8.1 Controllers 8.1.1 Understanding Controllers and Actions 8.1.2 Controllers and Scopes 8.1.3 Models and Views 8.1.4 Redirects and Chaining 8.1.5 Data Binding 8.1.6 XML and JSON Responses 8.1.7 More on JSONBuilder 8.1.8 Uploading Files 8.1.9 Command Objects 8.1.10 Handling Duplicate Form Submissions 8.1.11 Simple Type Converters 8.1.12 Declarative Controller Exception Handling 8.2 Groovy Server Pages 8.2.1 GSP Basics 8.2.1.1 Variables and Scopes 8.2.1.2 Logic and Iteration 8.2.1.3 Page Directives 8.2.1.4 Expressions 8.2.2 GSP Tags 8.2.2.1 Variables and Scopes 8.2.2.2 Logic and Iteration 8.2.2.3 Search and Filtering 8.2.2.4 Links and Resources 8.2.2.5 Forms and Fields 8.2.2.6 Tags as Method Calls 8.2.3 Views and Templates 8.2.4 Layouts with Sitemesh 4 8.2.5 Static Resources 8.2.6 Sitemesh Content Blocks 8.2.7 Making Changes to a Deployed Application 8.2.8 GSP Debugging 8.3 Tag Libraries 8.3.1 Variables and Scopes 8.3.2 Simple Tags 8.3.3 Logical Tags 8.3.4 Iterative Tags 8.3.5 Tag Namespaces 8.3.6 Using JSP Tag Libraries 8.3.7 Tag return value 8.4 URL Mappings 8.4.1 Mapping to Controllers and Actions 8.4.2 Mapping to REST resources 8.4.3 Redirects In URL Mappings 8.4.4 Embedded Variables 8.4.5 Mapping to Views 8.4.6 Mapping to Response Codes 8.4.7 Mapping to HTTP methods 8.4.8 Mapping Wildcards 8.4.9 Automatic Link Re-Writing 8.4.10 Applying Constraints 8.4.11 Named URL Mappings 8.4.12 Customizing URL Formats 8.4.13 Namespaced Controllers 8.5 Interceptors 8.5.1 Defining Interceptors 8.5.2 Matching Requests with Inteceptors 8.5.3 Ordering Interceptor Execution 8.6 Content Negotiation 9 Traits 9.1 Traits Provided by Grails 9.1.1 WebAttributes Trait Example 10 Web Services 10.1 REST 10.1.1 Domain classes as REST resources 5 10.1.2 Mapping to REST resources 10.1.3 Linking to REST resources from GSP pages 10.1.4 Versioning REST resources 10.1.5 Implementing REST controllers 10.1.5.1 Extending the RestfulController super class 10.1.5.2 Implementing REST Controllers Step by Step 10.1.5.3 Generating a REST controller using scaffolding 10.1.6 The REST Profile 10.1.7 The Angular Profile 10.1.8 JSON Views 10.1.8.1 Getting Started 10.1.8.2 Creating JSON Views 10.1.8.3 JSON View Templates 10.1.8.4 Rendering Domain Classes with JSON Views 10.1.8.5 JSON Views by Convention 10.1.9 Customizing Response Rendering 10.1.9.1 Customizing the Default Renderers 10.1.9.2 Implementing a Custom Renderer 10.1.9.3 Using GSP to Customize Rendering 10.1.10 Hypermedia as the Engine of Application State 10.1.10.1 HAL Support 10.1.10.2 Atom Support 10.1.10.3 Vnd.Error Support 10.1.11 Customizing Binding of Resources 10.2 RSS and Atom 11 Asynchronous Programming 11.1 Promises 11.2 Events 11.2.1 Consuming Events 11.2.2 Event Notification 11.2.3 Reactor Spring Annotations 11.2.4 Events from GORM 11.2.5 Events from Spring 11.3 Asynchronous GORM 11.4 Asynchronous Request Handling 11.5 Servlet 3.0 Async 12 Validation 6 12.1 Declaring Constraints 12.2 Validating Constraints 12.3 Sharing Constraints Between Classes 12.4 Validation on the Client 12.5 Validation and Internationalization 12.6 Applying Validation to Other Classes 13 The Service Layer 13.1 Declarative Transactions 13.1.1 Transactions Rollback and the Session 13.2 Scoped Services 13.3 Dependency Injection and Services 14 Static Type Checking And Compilation 14.1 The GrailsCompileStatic Annotation 14.2 The GrailsTypeChecked Annotation 15 Testing 15.1 Unit Testing 15.1.1 Unit Testing Controllers 15.1.2 Unit Testing Tag Libraries 15.1.3 Unit Testing Domains 15.1.4 Unit Testing Filters 15.1.5 Unit Testing URL Mappings 15.1.6 Mocking Collaborators 15.1.7 Mocking Codecs 15.1.8 Unit Test Metaprogramming 15.2 Integration Testing 15.3 Functional Testing 16 Internationalization 16.1 Understanding Message Bundles 16.2 Changing Locales 16.3 Reading Messages 16.4 Scaffolding and i18n 17 Security 17.1 Securing Against Attacks 17.2 Cross Site Scripting (XSS) Prevention 17.3 Encoding and Decoding Objects 17.4 Authentication 17.5 Security Plugins 7 17.5.1 Spring Security 17.5.2 Shiro 18 Plugins 18.1 Creating and Installing Plugins 18.2 Plugin Repositories 18.3 Providing Basic Artefacts 18.4 Evaluating Conventions 18.5 Hooking into Runtime Configuration 18.6 Adding Methods at Compile Time 18.7 Adding Dynamic Methods at Runtime 18.8 Participating in Auto Reload Events 18.9 Understanding Plugin Load Order 18.10 The Artefact API 18.10.1 Asking About Available Artefacts 18.10.2 Adding Your Own Artefact Types 19 Grails and Spring 19.1 The Underpinnings of Grails 19.2 Configuring Additional Beans 19.3 Runtime Spring with the Beans DSL 19.4 The BeanBuilder DSL Explained 19.5 Property Placeholder Configuration 19.6 Property Override Configuration 20 Grails and Hibernate 20.1 Using Hibernate XML Mapping Files 20.2 Mapping with Hibernate Annotations 20.3 Adding Constraints 21 Scaffolding 22 Deployment 22.1 Standalone 22.2 Container Deployment (e.g.