Introduction to Grails Framework
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Introduction to Grails Framework

on

  • 4,114 views

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

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

Statistics

Views

Total Views
4,114
Views on SlideShare
3,931
Embed Views
183

Actions

Likes
4
Downloads
195
Comments
0

3 Embeds 183

http://jug.pt 165
http://ptjug.wordpress.com 14
http://www.slideshare.net 4

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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

Introduction to Grails Framework Presentation Transcript

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