Introduction to Grails <ul><li>Christopher Bartling </li></ul>
Topics <ul><li>Decomposing Grails </li></ul><ul><ul><li>Major components </li></ul></ul><ul><ul><li>Extending Grails with ...
What is Grails? <ul><li>Next generation web framework for the JVM </li></ul><ul><li>Focus on developer productivity </li><...
Under the hood <ul><li>Grails utilizes a lot of existing Java frameworks  </li></ul><ul><ul><li>Spring Framework : Depende...
Decomposing Grails <ul><li>Grails scripts and Gant </li></ul><ul><li>Model-View-Controller (MVC) design </li></ul><ul><li>...
Grails scripts <ul><li>Generate various parts of the Grails application </li></ul><ul><ul><li>create-app : Creates a blank...
Gant <ul><li>Grails build system </li></ul><ul><li>Thin DSL wrapper around Apache Ant </li></ul><ul><li>Gant scripts resid...
Domain objects (Model) <ul><li>Automatically mapped to database schema </li></ul><ul><ul><li>Can tweak mapping through DSL...
Domain class example <ul><li>class  Book {   String  title   String  author   String  description   Date  reviewDate   sta...
GORM <ul><li>The default Grails persistence layer  </li></ul><ul><li>Functionality for reading, listing, sorting, counting...
Domain class dynamic runtime methods <ul><li>Groovy provides method interception support </li></ul><ul><li>Grails injects ...
Synthesized domain methods <ul><li>The  countBy*()  and  findBy*()  methods are synthesized from domain class properties a...
Domain class constraint validation <ul><li>The domain class’s  save()  and  validate()  methods will trigger data validati...
Domain classes: One-to-one relationships <ul><li>One class references another </li></ul><ul><ul><li>Declared by a property...
Domain classes: One-to-many relationships <ul><li>Uni-directional one-to-many relationship uses the static  hasMany   prop...
Domain classes: Many-to-many relationships <ul><li>Declared by adding a static  hasMany  property to each class participat...
Controllers <ul><li>Groovy class with name ending in “Controller” </li></ul><ul><li>Resides in  grails-app/controllers  di...
Controller responses <ul><li>Return a map of data or nothing </li></ul><ul><ul><li>Controller forwards execution control t...
UrlMappings.groovy <ul><li>Resides in  grails-app/conf  directory </li></ul><ul><li>DSL for customizing the mapping of the...
Views <ul><li>Each controller will have a corresponding directory under  grails-app/views  for views </li></ul><ul><ul><li...
Groovy Server Pages (GSPs) <ul><li>View rendering </li></ul><ul><li>Reside in the grails-app/views directory </li></ul><ul...
Tag libraries <ul><li>Reside in  grails-app/taglib  directory </li></ul><ul><li>Groovy class with name ending in “ TagLib ...
SiteMesh <ul><li>Web page layout and decorator framework </li></ul><ul><li>Uses HTML meta tags to decorate your web page w...
Services <ul><li>Reside in grails-app/services directory </li></ul><ul><li>Automatically have transaction support </li></u...
Dependency resolution <ul><li>Spring Framework is used to provide dependency injection </li></ul><ul><li>Spring bean confi...
Extending Grails with plugins <ul><li>At its heart, Grails is really just a plugin management system </li></ul><ul><ul><li...
Finding and installing Grails plugins <ul><li>Listing plug-ins </li></ul><ul><ul><li>grails list-plugins </li></ul></ul><u...
Testing in Grails <ul><li>Grails creates unit test stubs </li></ul><ul><li>Several  GroovyTestCase  subclasses specialized...
Grails demonstrations
Questions?
Resources available online <ul><li>Presentation and demo available on BitBucket.org </li></ul><ul><li>https://bitbucket.or...
Contact information <ul><li>Christopher Bartling </li></ul><ul><ul><li>[email_address] </li></ul></ul><ul><ul><li>Twitter:...
Upcoming SlideShare
Loading in...5
×

Introduction To Grails

2,405

Published on

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

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,405
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
95
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Introduction To Grails

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

    Clipping is a handy way to collect important slides you want to go back to later.

×