SVCC Intro to Grails


Published on

Given at Silicon Valley Code Camp

Published in: Technology, Education
1 Like
  • Be the first to comment

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

No notes for slide

SVCC Intro to Grails

  1. 1. Introduction to Grails James Williams
  2. 2. Introduction <ul><li>James Williams </li></ul><ul><li>Groovy committer </li></ul><ul><li>Author of Google charts plugin </li></ul><ul><li>Griffon co-creator </li></ul><ul><li>Creator of SwingXBuilder </li></ul>
  3. 3. Agenda <ul><li>What is Grails? </li></ul><ul><li>Domain Classes </li></ul><ul><li>Controllers </li></ul><ul><li>GSP/Taglibs </li></ul><ul><li>Plugins </li></ul><ul><li>Web Services </li></ul><ul><li>Conclusion </li></ul>
  4. 4. What is Grails? <ul><li>Rapid web application development </li></ul><ul><li>Full-stack MVC framework </li></ul><ul><li>Open source </li></ul><ul><li>Runs on the JVM </li></ul>
  5. 5. What is Grails? (cont'd) <ul><li>Integrates best of breed tools... </li></ul><ul><li>And many more... </li></ul>
  6. 6. Grails Quickstart <ul><li>Install Grails </li></ul><ul><li>Run grails create-app <name> </li></ul><ul><li>Run grails run-app </li></ul>
  7. 7. Domain Classes <ul><li>Core objects that make up a Grails app </li></ul><ul><li>Are seamlessly persisted to storage </li></ul><ul><li>Can have constraints on values </li></ul><ul><li>Can have relationships with other domain classes </li></ul>
  8. 8. Constraints <ul><li>Determine the validity of member fields </li></ul><ul><li>blank </li></ul><ul><li>creditCard </li></ul><ul><li>email </li></ul><ul><li>inList </li></ul><ul><li>matches </li></ul><ul><li>max </li></ul><ul><li>maxSize </li></ul><ul><li>min </li></ul><ul><li>minSize </li></ul><ul><li>notEqual </li></ul><ul><li>nullable </li></ul><ul><li>range </li></ul><ul><li>scale </li></ul><ul><li>size </li></ul><ul><li>unique </li></ul><ul><li>url </li></ul><ul><li>validator </li></ul>
  9. 9. Grails ORM (GORM) <ul><li>No hassle database persistence </li></ul><ul><li>Leverages Hibernate but minus .hbm files and other messy configuration files </li></ul><ul><li>Provides dynamic domain class methods like... </li></ul><ul><li>create </li></ul><ul><li>save </li></ul><ul><li>delete </li></ul><ul><li>update </li></ul><ul><li>get </li></ul><ul><li>find </li></ul><ul><li>findAll </li></ul><ul><li>addTo </li></ul><ul><li>count </li></ul><ul><li>.... </li></ul>
  10. 10. Relationships <ul><li>One to one </li></ul><ul><li>One to many (hasMany) </li></ul><ul><li>Many to many </li></ul>
  11. 11. Demo <ul><li>Domain Classes Demo </li></ul>
  12. 12. Controllers <ul><li>Manages requests and response for domain objects </li></ul><ul><li>Unless overridden, maps requests to a REST-like structure /<domain class>/action_name </li></ul><ul><li>It's actions can be generated on the fly by the application </li></ul>
  13. 13. Groovy Server Pages <ul><li>Provide views for domain objects </li></ul><ul><li>Like JSP, GSPs can be a mix of markup, Groovy code and GSP tags* </li></ul><ul><li>Generally correspond to an associated action in the controller </li></ul><ul><li>Code fragments use the <% %> and <%= %> forms, though this is discouraged </li></ul>
  14. 14. Typical GSP <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><% [1,2,3,4].each { num -> %> </li></ul><ul><li><p><%=&quot;Hello ${num}!&quot; %></p> </li></ul><ul><li><%}%> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  15. 15. Taglibs <ul><li>Allow the developer to simplify blocks of code </li></ul><ul><li>Similar to JSP tag libs class SimpleTagLib { def emoticon = { attrs, body -> out << body() << attrs.happy == 'true' ? &quot; :-)&quot; : &quot; :-(&quot; } } </li></ul><ul><li>Can be declared at an application or plugin level (e.g. Grails Chart plugin) </li></ul>
  16. 16. Taglibs in the wild <ul><li><html> </li></ul><ul><ul><li><body> </li></ul></ul><ul><ul><ul><li><g:emoticon happy='true'> </li></ul></ul></ul><ul><ul><li></body> </li></ul></ul><ul><li></html> </li></ul>
  17. 17. Demo <ul><li>Controllers and GSP Demo </li></ul>
  18. 18. Databases <ul><li>Grails supports almost all JDBC compliant databases </li></ul><ul><li>grails-app/conf/Datasources.groovy allows you to specify datasources for different env's </li></ul><ul><li>environments { </li></ul><ul><ul><li>development { </li></ul></ul><ul><ul><ul><li>dataSource { </li></ul></ul></ul><ul><ul><ul><ul><li>dbCreate = &quot;update&quot; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>url = &quot;jdbc:hsqldb:mem:devDB&quot; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>pooled = true </li></ul></ul></ul></ul><ul><ul><ul><ul><li>driverClassName = &quot;org.hsqldb.jdbcDriver&quot; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>username = &quot;sa&quot; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>password = &quot;&quot; </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>} </li></ul>
  19. 19. Plugins <ul><li>Grails applications in and of themselves </li></ul><ul><li>Can </li></ul><ul><ul><li>provide shortcuts to core tasks </li></ul></ul><ul><ul><li>encapsulate and integrate external tools </li></ul></ul><ul><li>Benefit from the huge library of J2EE tools and libraries </li></ul><ul><li>Over 50 hosted at </li></ul>
  20. 20. Web Services <ul><li>REST </li></ul><ul><li>SOAP (requires XFire plugin) class BookService { </li></ul><ul><ul><li>static expose=['xfire'] </li></ul></ul><ul><ul><li>Book[] getBooks(){ Book.list() as Book[] } </li></ul></ul><ul><li>} </li></ul><ul><li>XML Marshaling as XML </li></ul><ul><li>JSON Marshaling as JSON </li></ul>
  21. 21. Demo <ul><li>Plugins and Web services Demo </li></ul>
  22. 22. Resources <ul><li>Websites </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>Books </li></ul>
  23. 23. Questions? <ul><li>Contact me </li></ul><ul><li> </li></ul><ul><li>@ecspike </li></ul>
  1. A particular slide catching your eye?

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