0
Rapid Web Application Development with Grails Graeme Rocher Managing Director Agilize it http://www.agilizeit.com Session ...
Goal of this Talk <ul><ul><li>Learn how to rapidly create web applications using the agile web application framework Grail...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
Groovy & Grails <ul><li>Grails: MVC web framework inspired by: </li></ul><ul><ul><ul><li>Convention over Configuration </l...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
Getting Started <ul><li>Grails available from  http://grails.org </li></ul><ul><li>Stable & Development snapshots availabl...
Project Infrastructure <ul><ul><ul><ul><li>+ PROJECT_HOME </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ grails-app </li><...
Command Line Targets <ul><li>Apache Ant bundled with Grails </li></ul><ul><li>Many useful targets available: </li></ul><ul...
The Data Source // data source located in grails-app/conf Class ApplicationDataSource { @Property pooled = false @Property...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
The Application Domain <ul><li>Domain classes hold  state  and implement  behaviour </li></ul><ul><li>They are linked toge...
Grails ORM (GORM) <ul><li>Extremely simple. No special class to extend or file to configure! </li></ul><ul><li>class Expen...
Grails ORM (GORM) <ul><li>We’ve got this far, so lets define the other side! </li></ul><ul><li>class ExpenseItem { </li></...
Grails Constraints <ul><li>Validation constraints can be defined using the ‘constraints’ property </li></ul><ul><ul><ul><u...
Dynamic Methods & Properties <ul><li>Grails injects methods and properties into domain classes at runtime: </li></ul><ul><...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
Controllers <ul><li>Controllers handle  requests  and prepare  responses  </li></ul><ul><li>Response created by either  de...
The Controller <ul><li>The controller and action name map to the URI:  /expenseReport/list </li></ul><ul><li>class  Expens...
Data Binding & Flow Control <ul><li>// save action </li></ul><ul><li>@Property  save  = { </li></ul><ul><li>  def e = Expe...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
Groovy Server Pages <ul><li>A view technology very similar to JSP, but with  Groovy  as the primary language   </li></ul><...
A GSP Example <ul><li>The GSP for the  list  action is named according to convention:  grails-app/views/expenseItem/list.g...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
Dynamic Tag Libraries <ul><li>Easy definition of simple, logical and iterative tags: </li></ul><ul><li>class Expense TagLi...
Dynamic Tag Libraries <ul><li>Using the tag requires no imports or configuration and can be reloaded at runtime!: </li></u...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
AJAX Support <ul><li>Built in “ adaptive ” tag library for Ajax </li></ul><ul><li>Supports Prototype, Rico, and Yahoo (Doj...
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
DEMO <ul><li>Scaffolding </li></ul>
Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><l...
Java Integration <ul><li>Now for the important bit, with Groovy and Grails you can: </li></ul><ul><li>Call any existing Ja...
DEMO <ul><li>ECLIPSE INTEGRATION </li></ul>
Summary <ul><li>With the advent on Web 2.0  agility  is key </li></ul><ul><li>Dynamic frameworks (Grails, Rails, Django et...
For More Information <ul><ul><li>Groovy website –  http://groovy.codehaus.org </li></ul></ul><ul><ul><li>Grails website – ...
Q&A
Upcoming SlideShare
Loading in...5
×

Grails presentations

672

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
672
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Grails presentations"

  1. 1. Rapid Web Application Development with Grails Graeme Rocher Managing Director Agilize it http://www.agilizeit.com Session ID# BOF-2521
  2. 2. Goal of this Talk <ul><ul><li>Learn how to rapidly create web applications using the agile web application framework Grails </li></ul></ul>Rapid Web Application Development with Grails
  3. 3. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages (GSP) </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  4. 4. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages (GSP) </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  5. 5. Groovy & Grails <ul><li>Grails: MVC web framework inspired by: </li></ul><ul><ul><ul><li>Convention over Configuration </li></ul></ul></ul><ul><ul><ul><li>Don’t Repeat Yourself (DRY) </li></ul></ul></ul><ul><ul><ul><li>Ruby on Rails </li></ul></ul></ul><ul><li>Built on solid foundations: </li></ul><ul><ul><ul><li>Spring IoC, MVC and WebFlow </li></ul></ul></ul><ul><ul><ul><li>Hibernate </li></ul></ul></ul><ul><ul><ul><li>SiteMesh </li></ul></ul></ul><ul><li>Why Groovy? </li></ul><ul><ul><ul><li>Meta-Programming </li></ul></ul></ul><ul><ul><ul><li>Closure Support </li></ul></ul></ul><ul><ul><ul><li>Syntactically Expressive </li></ul></ul></ul><ul><ul><ul><li>Java Integration </li></ul></ul></ul>Source: Please add the source of your data here
  6. 6. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages (GSP) </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  7. 7. Getting Started <ul><li>Grails available from http://grails.org </li></ul><ul><li>Stable & Development snapshots available </li></ul><ul><li>Simple installation: </li></ul><ul><ul><li>Download & extract zip </li></ul></ul><ul><ul><li>Set GRAILS_HOME variable </li></ul></ul><ul><ul><li>Add $GRAILS_HOMEin to PATH variable </li></ul></ul><ul><li>Run “grails create-app ” </li></ul>Source: Please add the source of your data here
  8. 8. Project Infrastructure <ul><ul><ul><ul><li>+ PROJECT_HOME </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ grails-app </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ conf </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ controllers </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ domain </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ i18n </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ services </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ taglib </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ views </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ lib </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ spring </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ hibernate </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ src </li></ul></ul></ul></ul><ul><ul><ul><ul><li>+ web-app </li></ul></ul></ul></ul>Main Grails resources Additional Spring configuration Additional Hibernate mapping Web resources e.g. CSS, JavaScript etc. Java sources Jar archive libraries
  9. 9. Command Line Targets <ul><li>Apache Ant bundled with Grails </li></ul><ul><li>Many useful targets available: </li></ul><ul><ul><li>create-* (for creating Grails artifacts) </li></ul></ul><ul><ul><li>generate-controller </li></ul></ul><ul><ul><li>generate-views </li></ul></ul><ul><ul><li>run-app </li></ul></ul><ul><ul><li>test-app </li></ul></ul><ul><ul><li>run-webtest </li></ul></ul>Source: Please add the source of your data here
  10. 10. The Data Source // data source located in grails-app/conf Class ApplicationDataSource { @Property pooled = false @Property dbCreate = “ create-drop ” @Property url = “jdbc:hsqldb:mem:testDb” @Property driverClassName = “org.hsqldb.jdbcDriver” @Property username = “sa” @Property password = “sa” } Whether connection Pooling is enabled DB Auto creation with hbm2ddl Remaining connection settings
  11. 11. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages (GSP) </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  12. 12. The Application Domain <ul><li>Domain classes hold state and implement behaviour </li></ul><ul><li>They are linked together via relationships (e.g. one-to-many) </li></ul><ul><li>In Java domain classes have traditionally been handled by Object-Relational Mapping (ORM) </li></ul><ul><li>Grails provides simple ORM built on Hibernate </li></ul>Source: Please add the source of your data here
  13. 13. Grails ORM (GORM) <ul><li>Extremely simple. No special class to extend or file to configure! </li></ul><ul><li>class ExpenseReport { </li></ul><ul><li>@Property Long id </li></ul><ul><li>@Property Long version </li></ul><ul><li>@Property relatesToMany = [ items :ExpenseItem] </li></ul><ul><li>@Property Set items </li></ul><ul><li>@Property Date submissionDate </li></ul><ul><li>@Property String employeeName </li></ul><ul><li>} </li></ul>Each domain class has an ‘id’ and ‘version’ Defines one-to-many relationship to ExpenseItem
  14. 14. Grails ORM (GORM) <ul><li>We’ve got this far, so lets define the other side! </li></ul><ul><li>class ExpenseItem { </li></ul><ul><li>@Property Long id </li></ul><ul><li>@Property Long version </li></ul><ul><li>@Property belongsTo = ExpenseReport </li></ul><ul><li>@Property String type </li></ul><ul><li>@Property Currency currency </li></ul><ul><li>@Property Integer amount </li></ul><ul><li>} </li></ul>Defines the owning side of the relationship Each property maps To a column
  15. 15. Grails Constraints <ul><li>Validation constraints can be defined using the ‘constraints’ property </li></ul><ul><ul><ul><ul><li>class ExpenseItem { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>… </li></ul></ul></ul></ul><ul><ul><ul><ul><li>@Property constraints = { type (inList:[ ' travel ' , ' accomodation ' ]) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>amount (range:1..999) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul>Each node relates to a property Ensures the ‘type’ property Is one of the values in the list ‘ amount’ must be in a range greater than 0 but less than 1000
  16. 16. Dynamic Methods & Properties <ul><li>Grails injects methods and properties into domain classes at runtime: </li></ul><ul><li>def r = ExpenseReport. findBy EmployeeName ( ' fred ' ) </li></ul><ul><li>def r = ExpenseReport </li></ul><ul><li>. findBy SubmissionDate GreaterThan (lastMonth) </li></ul><ul><li>def reports = ExpenseReport. findAll () </li></ul><ul><li>assert ! (new ExpenseItem(). validate ()) </li></ul><ul><li>def er = new ExpenseReport(employeeName: ' Edgar ' ) </li></ul><ul><li>. save () </li></ul>
  17. 17. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages (GSP) </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  18. 18. Controllers <ul><li>Controllers handle requests and prepare responses </li></ul><ul><li>Response created by either delegating to a view or writing to the response </li></ul><ul><li>A controller is a class containing closure properties that act on requests </li></ul><ul><li>The convention used for the name of the controller and the actions within map to URIs. </li></ul>Source: Please add the source of your data here
  19. 19. The Controller <ul><li>The controller and action name map to the URI: /expenseReport/list </li></ul><ul><li>class ExpenseReport Controller { </li></ul><ul><li>@Property list = { </li></ul><ul><li> [ expenseReports : ExpenseReport.list()] </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>The name of the class is the first token in the URI Each action is a closure property An optional model is returned as a map
  20. 20. Data Binding & Flow Control <ul><li>// save action </li></ul><ul><li>@Property save = { </li></ul><ul><li> def e = ExpenseItem.get(params.id) </li></ul><ul><li>e.properties = params </li></ul><ul><li>if(e. save ()){ redirect(action:show,id:e.id) </li></ul><ul><li>} else { </li></ul><ul><li>render( view: ' create ' , </li></ul><ul><li> model:[expenseItem:e] ) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Auto-type conversion from request parameters Dynamic get method Auto-type conversion To id type Example flow control via render and redirect methods
  21. 21. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages (GSP) </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  22. 22. Groovy Server Pages <ul><li>A view technology very similar to JSP, but with Groovy as the primary language </li></ul><ul><li>More expressive and concise with support for embedded GStrings & Tags </li></ul><ul><li>Layout support through integration with SiteMesh </li></ul><ul><li>Ability to define dynamic tag libraries </li></ul>Source: Please add the source of your data here
  23. 23. A GSP Example <ul><li>The GSP for the list action is named according to convention: grails-app/views/expenseItem/list.gsp </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><g:each in= &quot; ${ expenseItems } &quot; > </li></ul><ul><li><p> ${it.type} – amount: ${it.amount} </p> </li></ul><ul><li></g:each> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>References the model returned by the controller Embedded GString expressions
  24. 24. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  25. 25. Dynamic Tag Libraries <ul><li>Easy definition of simple, logical and iterative tags: </li></ul><ul><li>class Expense TagLib { </li></ul><ul><li>@Property dateFormat = { attrs,body -> </li></ul><ul><li>out << new SimpleDateFormat( attrs .format) </li></ul><ul><li>.format( attrs .date) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>The name of the tag The body argument is a closure that can be invoked The attributes are passed as a map
  26. 26. Dynamic Tag Libraries <ul><li>Using the tag requires no imports or configuration and can be reloaded at runtime!: </li></ul><ul><li><p>Submitted: </li></ul><ul><li><g: dateFormat date = &quot; ${report.submissionDate} &quot; </li></ul><ul><li>format = &quot; DD-MM-YYYY &quot; /> </li></ul><ul><li></p> </li></ul><ul><li><p><input type= &quot; hidden &quot; name= &quot; submissionDate &quot; </li></ul><ul><li>value= &quot; ${ dateFormat ( </li></ul><ul><li>date :report.submissionDate, </li></ul><ul><li>format : ' DD-MM-YYYY ' )} &quot; /> </li></ul><ul><li></p> </li></ul>Tag called by name with the “g:” prefix Tag can also be called as a regular method!
  27. 27. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Ajax Support </li></ul><ul><li>Scaffolding </li></ul><ul><li>Java Integration </li></ul>
  28. 28. AJAX Support <ul><li>Built in “ adaptive ” tag library for Ajax </li></ul><ul><li>Supports Prototype, Rico, and Yahoo (Dojo coming soon) </li></ul><ul><li>Tags for remote linking, asynchronous form submission etc. </li></ul><ul><li>Dynamic “ render ” method available for rendering XML snippets, or partial templates </li></ul><ul><li>Save/Reload and dynamic tag libraries make Ajax even easier </li></ul>
  29. 29. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Scaffolding </li></ul><ul><li>Ajax Support </li></ul><ul><li>Java Integration </li></ul>
  30. 30. DEMO <ul><li>Scaffolding </li></ul>
  31. 31. Agenda <ul><li>Groovy & Grails </li></ul><ul><li>Getting Started </li></ul><ul><li>The Application Domain </li></ul><ul><li>Controllers </li></ul><ul><li>Groovy Servers Pages </li></ul><ul><li>Tag Libraries </li></ul><ul><li>Scaffolding </li></ul><ul><li>Ajax Support </li></ul><ul><li>Java Integration </li></ul>
  32. 32. Java Integration <ul><li>Now for the important bit, with Groovy and Grails you can: </li></ul><ul><li>Call any existing Java library seamlessly </li></ul><ul><li>Deploy as a WAR onto any JEE application server </li></ul><ul><li>Write your domain model in Java , mapped with Hibernate, and still use dynamic methods! </li></ul><ul><li>Take advantage of Hibernate’s power by mapping onto legacy systems. </li></ul><ul><li>Use Spring’s dependency injection to integrate Controllers with existing services </li></ul>
  33. 33. DEMO <ul><li>ECLIPSE INTEGRATION </li></ul>
  34. 34. Summary <ul><li>With the advent on Web 2.0 agility is key </li></ul><ul><li>Dynamic frameworks (Grails, Rails, Django etc.) provide this through quick iterative development with a clear productivity gain </li></ul><ul><li>However, for large scale applications static-typing and IDE support is crucial </li></ul><ul><li>Grails provides the ability to use a blended approach </li></ul><ul><li>And most importantly it runs on the JVM ! </li></ul>
  35. 35. For More Information <ul><ul><li>Groovy website – http://groovy.codehaus.org </li></ul></ul><ul><ul><li>Grails website – http://grails.org </li></ul></ul><ul><ul><li>Mailing lists – http:// grails.org/Mailing+lists </li></ul></ul><ul><ul><li>Graeme’s Blog – http://graemerocher.blogspot.com </li></ul></ul><ul><ul><li>Upcoming books ‘The Definitive Guide to Grails’ by Apress and ‘Groovy in Action’ by Manning </li></ul></ul>
  36. 36. Q&A
  1. A particular slide catching your eye?

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

×