Your SlideShare is downloading. ×
0
Miguel Ping<br />Introduction to Grails Framework<br />18/03/2010<br />
<ul><li>Small Introduction on Groovy
Grails
Grails = Groovy on (G)Rails
Grails is MVC
Powered by Spring and Hibernate
Model/Domain Layer
View Layer
Controllers
Demo</li></ul>Agenda<br />
Groovy<br />+<br />=<br />
Java Class<br />publicclassHelloWorld {privateString name;publicvoidsetName(String name) {this.name = name;  }publicString...
Groovy Class<br />publicclassHelloWorld {privateString name;publicvoidsetName(String name) {this.name = name;  }publicStri...
<ul><li>Multiple classes per file, or even no classes at all (scripts)
Public unless defined otherwise
Getters and Setters by default
String interpolation
Semicolons are optional
Type declarations are optional
Return keyword is optional</li></ul>Groovy Features<br />class HelloWorld {def namedefgreet() { "Hello ${name}" }}helloWor...
<ul><li>Dynamic language (everything happens at runtime)
Dynamically typed (types are “optional”)
Closures
Anonymous Inner Classes (recently)
Meta Object Protocol
Hence, MetaClasses and all the magic
Compiles down to bytecode
Upcoming SlideShare
Loading in...5
×

Introduction to Grails Framework

4,222

Published on

Apresentação de Miguel Ping - 5º encontro PT.JUG.

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,222
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
242
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide
  • Similarities to Jboss SeamWe can see by this slide the commands syntax
  • Some constraints are mapped to DB constraints, although when removing a constraint grails do not remove the correspondent db constraint.
  • Intro to taglib, grails style. Mapping to closure. Almost no need for code in gsp.
  • Explain that Grails is Action-First
  • Talk about maturity
  • Faltou falar de: Ambientes, Testes, Scripts, GORM criteria, DSL para beans spring, Plugin Migrações
  • Configuration Files
  • Transcript of "Introduction to Grails Framework"

    1. 1. Miguel Ping<br />Introduction to Grails Framework<br />18/03/2010<br />
    2. 2. <ul><li>Small Introduction on Groovy
    3. 3. Grails
    4. 4. Grails = Groovy on (G)Rails
    5. 5. Grails is MVC
    6. 6. Powered by Spring and Hibernate
    7. 7. Model/Domain Layer
    8. 8. View Layer
    9. 9. Controllers
    10. 10. Demo</li></ul>Agenda<br />
    11. 11. Groovy<br />+<br />=<br />
    12. 12. Java Class<br />publicclassHelloWorld {privateString name;publicvoidsetName(String name) {this.name = name; }publicStringgetName() {return name; }publicStringgreet() {return"Hello "+ name; }publicstaticvoidmain(String[] args) {HelloWorldhelloWorld=newHelloWorld();helloWorld.setName("Groovy");System.out.println(helloWorld.greet()); }}<br />
    13. 13. Groovy Class<br />publicclassHelloWorld {privateString name;publicvoidsetName(String name) {this.name = name; }publicStringgetName() {return name; }publicStringgreet() {return"Hello "+ name; }publicstaticvoidmain(String[] args) {HelloWorldhelloWorld=newHelloWorld();helloWorld.setName("Groovy");System.out.println(helloWorld.greet()); }}<br />
    14. 14. <ul><li>Multiple classes per file, or even no classes at all (scripts)
    15. 15. Public unless defined otherwise
    16. 16. Getters and Setters by default
    17. 17. String interpolation
    18. 18. Semicolons are optional
    19. 19. Type declarations are optional
    20. 20. Return keyword is optional</li></ul>Groovy Features<br />class HelloWorld {def namedefgreet() { "Hello ${name}" }}helloWorld =newHelloWorld(name: "Groovy")println helloWorld.greet()<br />
    21. 21. <ul><li>Dynamic language (everything happens at runtime)
    22. 22. Dynamically typed (types are “optional”)
    23. 23. Closures
    24. 24. Anonymous Inner Classes (recently)
    25. 25. Meta Object Protocol
    26. 26. Hence, MetaClasses and all the magic
    27. 27. Compiles down to bytecode
    28. 28. Excellent Java Interop (Groovy can call Java and vice-versa)
    29. 29. Lots of synthatic sugar
    30. 30. person?.address
    31. 31. def a = b ?:c</li></ul>Groovy Definition<br />
    32. 32. Grails<br />+<br />=<br />
    33. 33. <ul><li>Spring framework released in 2003
    34. 34. SpringSource founded in 2004
    35. 35. G2One (The Groovy Grails Company) founded in 2007 by the Groovy and Grails project leads
    36. 36. November 2008: G2One was acquired by SpringSource
    37. 37. August 2009: SpringSource was acquired by VMware for $420m
    38. 38. SpringSource is now a division of VMware </li></ul>Overview<br />
    39. 39. <ul><li>Grails is Groovy + Spring + Hibernate + Some nice stuff
    40. 40. Heavily inspired on Rails framework
    41. 41. But takes advantage of Java technology like Spring and servlets
    42. 42. It means good Java (platform) integration
    43. 43. Produces .war artifacts deployable in any servlet container
    44. 44. Has some command-line scripts to ease the pain
    45. 45. > grails generate-all MyClass
    46. 46. > grails test-app -integration
    47. 47. Has a plugin architecture
    48. 48. > grails install-plugin jquery</li></ul>Overview<br />
    49. 49. Grails is a Full Stack<br />Groovy<br />Grails<br />Sitemesh<br />Plugins<br />Hibernate<br />Other Libraries<br />Spring<br />Java Virtual Machine / JDK<br />
    50. 50. Application structure<br /><ul><li>grails-appTop level source folder with Grails artifacts
    51. 51. confConfiguration sources
    52. 52. controlllersController layer
    53. 53. domainModel layer
    54. 54. i18nInternationalized Resource Bundles
    55. 55. servicesService layer
    56. 56. taglibDynamic Tag Libraries
    57. 57. viewsGroovy Server Pages (GSP)
    58. 58. web-appStylesheets, Javascript, etc
    59. 59. scriptsCustom command-line scripts
    60. 60. srcOther project sources
    61. 61. groovyOther Groovy project sources
    62. 62. javaOther Java project sources
    63. 63. lib3th Party Libraries
    64. 64. testUnit, Integration and Functional tests</li></li></ul><li><ul><li>POGO – Plain Old Groovy Objects
    65. 65. Plus a DSL for domain constraints
    66. 66. Constraints/Validations
    67. 67. relationships
    68. 68. belongsTo, hasMany, mappedBy
    69. 69. max, min, blank, nullable, ... , custom
    70. 70. Implies validation
    71. 71. hasErrors(), validate()
    72. 72. DB operations
    73. 73. CRUD: save(), delete(), ...
    74. 74. Dynamic Finders
    75. 75. FindAllBy<Field> - Magick!!!</li></ul>Models<br />
    76. 76. Example of a Model<br /><ul><li>class Book {static belongsTo = [author: Author] //no ID declarationString titleString isbnBigDecimal priceDate dateCreatedDate lastUpdated //CallbacksdefbeforeInsert() { dateCreated =newDate() //not really necessary } // DSL for constraintsstatic constraints = { title(blank: false, maxSize: 100) isbn(blank: false, matches: "[0-9]{13}", minSize: 13, maxSize: 13, unique: true) price(nullable: true, min: 0.0, max: 9999.99, scale: 2) }// DSL for database mappingstatic mapping = { table "books" price column: "sales_price" }}</li></li></ul><li><ul><li>A lot like JSPs
    77. 77. Taglib
    78. 78. Pagination, i18n, ajax, page navigation
    79. 79. Hyperlinks
    80. 80. <g:link controller=“<controller>” action=“<action>” </li></ul> params=“<params>”><br /><ul><li>Controller is the controller name
    81. 81. Action is the closure name
    82. 82. Params are available in the controller’s closure
    83. 83. etc (including custom taglibs)
    84. 84. Partial render/Templates
    85. 85. Reusable, allows composition
    86. 86. g:render
    87. 87. Layouts</li></ul>Views - GSPs<br />
    88. 88. Views - Taglibs<br /><ul><li>Logic and Iteration <g:if><g:else><g:elsif><g:each><g:while>
    89. 89. Links and Resources</li></ul> <g:link action="show" id="${currentBook.id}">${currentBook.name}</g:link><br /><g:link controller="book" action="list">BookList</g:link><br /><ul><li>Forms and Fields</li></ul><g:textField name="title" value="${bookInstance.title}"/><br /><g:select name="author.id" from="${Author.list()}" value="${book.author.id}"/><br /><g:datePicker name="publishDate" value="${bookInstance.publishDate}"/><br /><ul><li>Do Your Own</li></ul>class SimpleTagLib {static namespace ="auth"def isAdmin = { attrs, body ->if (attrs.user.admin) out << body() }}<br /><auth:isAdmin user="${session.user}">// some restricted content</auth:isAdmin><br />
    90. 90. Example of a GSP<br /><ul><li>Embedded Groovy code</li></ul><html><body><% [1, 2, 3, 4].each { num ->if (num >2) { %><p><%="Hello ${num}!"%></p><% } } %></body></html><br /><ul><li>With Tag Libraries</li></ul><html><body><g:each in="${[1, 2, 3, 4]}" var="num"><g:if test="${num > 2}"><p>Number ${num}</p></g:if></g:each></body></html><br />
    91. 91. <ul><li>Handle the web request
    92. 92. Grails is action-first, it means the controller runs before the view
    93. 93. Spring-managed beans
    94. 94. Can hook up with another beans through Spring DI
    95. 95. Idiom</li></ul>params variable<br />holds request parameters<br /><ul><li>render
    96. 96. redirect,
    97. 97. return [beanName: bean]</li></ul>returns some data to the view<br />Controllers<br />
    98. 98. Example of a Controller<br /><ul><li>class BookController { //will match http://myApp:8080/book/static allowedMethods = [save: "POST", update: "POST", delete: "POST"]// default actiondef index = { redirect(action: "list", params: params) }def list = {//by default, we go to the “list.gsp” [bookInstanceList: Book.list(params), bookInstanceTotal: Book.count()] }defedit = { //params is a Map with an ‘id’ keydef bookInstance =Book.get(params.id)return [bookInstance: bookInstance] }def save = {def bookInstance =newBook(params)if (bookInstance.save()) { redirect(action: "show", id: bookInstance.id) }else { render(view: "create", model: [bookInstance: bookInstance]) } }</li></li></ul><li>Spring glues everything<br />Spring IoC / DI<br />We can write custom beans and wire everything together<br />Spring MVC<br />Spring Transactions<br />Spring i18n<br />Spring Glue<br />
    99. 99. <ul><li>Lots and lots of them
    100. 100. Spring Security / Acegi
    101. 101. RIA/AJAX: RichUI & GrailsUI
    102. 102. Testing (eg: Canoo, EasyB, etc)
    103. 103. Debugging plugins (eg: simple dashboard)
    104. 104. Optimization plugins (eg: js minifier)
    105. 105. Deployment plugins (eg: google-app-engine, amazon)
    106. 106. Other
    107. 107. Multi-tenant
    108. 108. Paypal
    109. 109. Openid
    110. 110. SpringWS
    111. 111. GWT</li></ul>Plugins<br />
    112. 112. 2 classes<br />Constraints<br />Relations<br />Scaffolding<br />Dynamic Finders<br />View<br />Some GSP navigation<br />Controllers<br />CRUD<br />AJAX<br />Test Drive<br />
    113. 113. Questions<br />
    114. 114. <ul><li>grails create-app [app name]
    115. 115. grails create-controller [controller name]
    116. 116. grails create-domain-class [class name]
    117. 117. grails create-service [service name]
    118. 118. grails create-unit-test [test name]
    119. 119. grails create-tag-lib [taglib name]
    120. 120. grails generate-all [class name]
    121. 121. grails generate-views [class name]</li></ul>Command line scripts - development<br />
    122. 122. <ul><li>grails clean
    123. 123. grails compile
    124. 124. grails console
    125. 125. grails doc
    126. 126. grails install-plugin
    127. 127. grails run-app
    128. 128. grails war</li></ul>Command line scripts - lifecycle<br />
    129. 129. <ul><li>conf/DataSource.groovy
    130. 130. Database connections
    131. 131. conf/UrlMapping.groovy
    132. 132. Routing
    133. 133. conf/BootStrap.groovy
    134. 134. Bootstrap file
    135. 135. conf/Config.groovy
    136. 136. Configurations (MIME mappings, more…)</li></ul>Important Files<br />
    137. 137. <ul><li>LessThan
    138. 138. LessThanEquals
    139. 139. GreaterThan
    140. 140. GreaterThanEquals
    141. 141. Between
    142. 142. Like
    143. 143. Ilike (i.e. ignorecase like)
    144. 144. IsNotNull
    145. 145. IsNull
    146. 146. Not
    147. 147. Equal
    148. 148. NotEqual
    149. 149. And
    150. 150. Or</li></ul>Dynamic Finders<br />
    1. A particular slide catching your eye?

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

    ×