Your SlideShare is downloading. ×
Introduction To Grails
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Introduction To Grails


Published on

Intro to Grails presentation that I gave at the Twin Cities Code Camp conference April 9, 2011.

Intro to Grails presentation that I gave at the Twin Cities Code Camp conference April 9, 2011.

Published in: Technology, Education

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Introduction to Grails
    • Christopher Bartling
  • 2. Topics
    • Decomposing Grails
      • Major components
      • Extending Grails with plugins
    • Demostrations
      • Basic Grails commands
      • Book Club
    • Questions
  • 3. What is Grails?
    • Next generation web framework for the JVM
    • Focus on developer productivity
      • Dynamic language - Groovy
      • Convention over Configuration
      • Pragmatic tooling promotes agility
        • DSLs, code generators, dependency resolution, multi-environment configuration
  • 4. Under the hood
    • Grails utilizes a lot of existing Java frameworks
      • Spring Framework : Dependency injection, AOP, core abstractions
      • Spring MVC : Web application framework
      • Hibernate : Relational database persistence
      • SiteMesh : Web layout and templating
      • Servlet API : Core web application infrastructure
  • 5. Decomposing Grails
    • Grails scripts and Gant
    • Model-View-Controller (MVC) design
    • Grails Object Relational Mapping (GORM)
    • Services
    • Dependency resolution and configuration
    • Plugins
  • 6. Grails scripts
    • Generate various parts of the Grails application
      • create-app : Creates a blank Grails application
      • create-domain-class : Creates a new domain object
      • generate-all : Generates the controller and associated views for a domain object
      • run-app : Runs the Grails application in the bundled Tomcat engine
  • 7. Gant
    • Grails build system
    • Thin DSL wrapper around Apache Ant
    • Gant scripts reside in grails-app/scripts directory
    • Create a new script:
      • grails create-script [scriptName]
  • 8. Domain objects (Model)
    • Automatically mapped to database schema
      • Can tweak mapping through DSL on domain class
    • Object relationship management
      • DSL on domain class
    • Attribute and object validation
      • Configured through DSL on domain class
  • 9. Domain class example
    • class Book { String title String author String description Date reviewDate static hasMany = [comments: Comment] static constraints = { title nullable: false, blank: false, size: 1..1024 author nullable: false, blank: false, size: 2..255 description nullable:true, blank: true, maxSize: 1000000 reviewDate nullable: true } }
  • 10. GORM
    • The default Grails persistence layer
    • Functionality for reading, listing, sorting, counting, saving, and deleting objects
    • Relationship management
    • Dynamic finder and counting methods
    • Criteria, HQL, and SQL queries
    • Pagination support
  • 11. Domain class dynamic runtime methods
    • Groovy provides method interception support
    • Grails injects several methods on all domain objects at runtime
        • get(id) , getAll([id, id, id, …]) , list() , listOrderBy*()
        • findBy*() , findAllBy*() , count() , countBy*()
        • save() , delete() , validate() , hasErrors
        • withTransaction, withCriteria
  • 12. Synthesized domain methods
    • The countBy*() and findBy*() methods are synthesized from domain class properties and the following comparators:
      • Equal, NotEqual, LessThan, LessThanEquals, GreaterThan, GreaterThanEquals, Like, like (case insensitive), InList, Between, IsNull, IsNotNull
    • This support works for up to two properties with optional comparators and logical operator
  • 13. Domain class constraint validation
    • The domain class’s save() and validate() methods will trigger data validation
      • Domain object maintains validation errors collection
    • 17 built-in constraints
    • The validator constraint supports custom validation scenarios
    • Many of the domain constraints influence database schema generation
  • 14. Domain classes: One-to-one relationships
    • One class references another
      • Declared by a property of the type of referenced domain class
    • Bi-directional one-to-one: Each class has a property to the other domain class
    • Ownership and cascading updates are declared with the static belongsTo property
  • 15. Domain classes: One-to-many relationships
    • Uni-directional one-to-many relationship uses the static hasMany property
      • The static belongsTo property can also be used in this relationship scenario
    • Bi-directional one-to-many relationship is as easy as adding a reference for the parent object reference and using the static belongsTo property
  • 16. Domain classes: Many-to-many relationships
    • Declared by adding a static hasMany property to each class participating in the relationship
    • One of the classes must declare a static belongsTo property to properly set ownership of the relationship
  • 17. Controllers
    • Groovy class with name ending in “Controller”
    • Resides in grails-app/controllers directory
    • Dynamically receive methods and properties at runtime
    • Any closure defined as a property of the controller becomes an action reachable by URL
      • book-club/book/add
  • 18. Controller responses
    • Return a map of data or nothing
      • Controller forwards execution control to view of the same name as action
    • Redirect to another action or URL
    • Render output directly
      • Rendering can be a view, template, JSON, XML, binary
      • JSON and XML converters part of Grails
  • 19. UrlMappings.groovy
    • Resides in grails-app/conf directory
    • DSL for customizing the mapping of the URIs to controller actions
    • Use this file to implement RESTful web service URIs
    • Error status codes can also be mapped to custom error pages
    • Each controller will have a corresponding directory under grails-app/views for views
      • e.g. grails-app/views/book
    • Groovy Server Pages (GSPs) reside here
    • View rendering convention: Forward to view GSP named the same as controller action
      • This is very customizable
  • 21. Groovy Server Pages (GSPs)
    • View rendering
    • Reside in the grails-app/views directory
    • Grails provides about 50 tags for use in GSPs
      • Logical, looping, link creation, forms and form inputs, JavaScript and other resource management tags
      • Custom taglibs are easily created for use in GSPs
  • 22. Tag libraries
    • Reside in grails-app/taglib directory
    • Groovy class with name ending in “ TagLib ”
    • Taglib actions are just closure properties
    • Write output to OutputStream out
      • Automatically injected into your taglib instance
    • TagLib attributes become attributes on taglib XML
  • 23. SiteMesh
    • Web page layout and decorator framework
    • Uses HTML meta tags to decorate your web page with standardized layout
    • Implemented as a page filter
    • Layout reside in grails-app/views/layouts
      • The main.gsp is your default layout master page
  • 24. Services
    • Reside in grails-app/services directory
    • Automatically have transaction support
      • static transactional = true | false
    • Automatically hosted by Spring DI container as singleton-scoped beans by default
      • Services can be injected into controllers, other services, domain classes
  • 25. Dependency resolution
    • Spring Framework is used to provide dependency injection
    • Spring bean configuration in resources.groovy
      • Spring DSL
    • Services will be autowired in controllers automatically
  • 26. Extending Grails with plugins
    • At its heart, Grails is really just a plugin management system
      • Most of the major pieces of Grails are implemented as plugins
    • / hosts Grails plugins
      • Currently about 575 plugins available!
    • One of Grails big selling points
  • 27. Finding and installing Grails plugins
    • Listing plug-ins
      • grails list-plugins
    • Installing a plugin
        • grails install-plugin [plugin-name] [version]
    • Grails takes care of downloading, installing, and managing the plugin
  • 28. Testing in Grails
    • Grails creates unit test stubs
    • Several GroovyTestCase subclasses specialized for testing Grails components
    • Great mock object support
      • mockDomain , mockForConstraintsTest , mockController , mockTagLib , mockLogging
    • Integration testing supported with separate test environment configuration
  • 29. Grails demonstrations
  • 30. Questions?
  • 31. Resources available online
    • Presentation and demo available on
    • presentations
    • Need Mercurial client to clone the repository or pull a zip archive snapshot of the repository
  • 32. Contact information
    • Christopher Bartling
      • [email_address]
      • Twitter: @cbartling
      • Blog: