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

Introduction to Grails Framework

on

  • 3,836 views

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

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

Statistics

Views

Total Views
3,836
Views on SlideShare
3,654
Embed Views
182

Actions

Likes
4
Downloads
185
Comments
0

3 Embeds 182

http://jug.pt 164
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 Introduction to Grails Framework Presentation Transcript

  • Miguel Ping
    Introduction to Grails Framework
    18/03/2010
    • 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
    Agenda
  • Groovy
    +
    =
  • 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()); }}
  • 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()); }}
    • 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
    Groovy Features
    class HelloWorld {def namedefgreet() { "Hello ${name}" }}helloWorld =newHelloWorld(name: "Groovy")println helloWorld.greet()
    • 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
    • Excellent Java Interop (Groovy can call Java and vice-versa)
    • Lots of synthatic sugar
    • person?.address
    • def a = b ?:c
    Groovy Definition
  • Grails
    +
    =
    • Spring framework released in 2003
    • SpringSource founded in 2004
    • G2One (The Groovy Grails Company) founded in 2007 by the Groovy and Grails project leads
    • November 2008: G2One was acquired by SpringSource
    • August 2009: SpringSource was acquired by VMware for $420m
    • SpringSource is now a division of VMware
    Overview
    • Grails is Groovy + Spring + Hibernate + Some nice stuff
    • Heavily inspired on Rails framework
    • But takes advantage of Java technology like Spring and servlets
    • It means good Java (platform) integration
    • Produces .war artifacts deployable in any servlet container
    • Has some command-line scripts to ease the pain
    • > grails generate-all MyClass
    • > grails test-app -integration
    • Has a plugin architecture
    • > grails install-plugin jquery
    Overview
  • Grails is a Full Stack
    Groovy
    Grails
    Sitemesh
    Plugins
    Hibernate
    Other Libraries
    Spring
    Java Virtual Machine / JDK
  • Application structure
    • grails-appTop level source folder with Grails artifacts
    • confConfiguration sources
    • controlllersController layer
    • domainModel layer
    • i18nInternationalized Resource Bundles
    • servicesService layer
    • taglibDynamic Tag Libraries
    • viewsGroovy Server Pages (GSP)
    • web-appStylesheets, Javascript, etc
    • scriptsCustom command-line scripts
    • srcOther project sources
    • groovyOther Groovy project sources
    • javaOther Java project sources
    • lib3th Party Libraries
    • testUnit, Integration and Functional tests
    • POGO – Plain Old Groovy Objects
    • Plus a DSL for domain constraints
    • Constraints/Validations
    • relationships
    • belongsTo, hasMany, mappedBy
    • max, min, blank, nullable, ... , custom
    • Implies validation
    • hasErrors(), validate()
    • DB operations
    • CRUD: save(), delete(), ...
    • Dynamic Finders
    • FindAllBy<Field> - Magick!!!
    Models
  • 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
    • Taglib
    • Pagination, i18n, ajax, page navigation
    • Hyperlinks
    • <g:link controller=“<controller>” action=“<action>”
    params=“<params>”>
    • Controller is the controller name
    • Action is the closure name
    • Params are available in the controller’s closure
    • etc (including custom taglibs)
    • Partial render/Templates
    • Reusable, allows composition
    • g:render
    • Layouts
    Views - GSPs
  • Views - Taglibs
    • Logic and Iteration <g:if><g:else><g:elsif><g:each><g:while>
    • 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>
  • 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>
    • Handle the web request
    • Grails is action-first, it means the controller runs before the view
    • Spring-managed beans
    • Can hook up with another beans through Spring DI
    • Idiom
    params variable
    holds request parameters
    • render
    • redirect,
    • return [beanName: bean]
    returns some data to the view
    Controllers
  • 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
    • Lots and lots of them
    • Spring Security / Acegi
    • RIA/AJAX: RichUI & GrailsUI
    • Testing (eg: Canoo, EasyB, etc)
    • Debugging plugins (eg: simple dashboard)
    • Optimization plugins (eg: js minifier)
    • Deployment plugins (eg: google-app-engine, amazon)
    • Other
    • Multi-tenant
    • Paypal
    • Openid
    • SpringWS
    • GWT
    Plugins
  • 2 classes
    Constraints
    Relations
    Scaffolding
    Dynamic Finders
    View
    Some GSP navigation
    Controllers
    CRUD
    AJAX
    Test Drive
  • Questions
    • grails create-app [app name]
    • grails create-controller [controller name]
    • grails create-domain-class [class name]
    • grails create-service [service name]
    • grails create-unit-test [test name]
    • grails create-tag-lib [taglib name]
    • grails generate-all [class name]
    • grails generate-views [class name]
    Command line scripts - development
    • grails clean
    • grails compile
    • grails console
    • grails doc
    • grails install-plugin
    • grails run-app
    • grails war
    Command line scripts - lifecycle
    • conf/DataSource.groovy
    • Database connections
    • conf/UrlMapping.groovy
    • Routing
    • conf/BootStrap.groovy
    • Bootstrap file
    • conf/Config.groovy
    • Configurations (MIME mappings, more…)
    Important Files
    • LessThan
    • LessThanEquals
    • GreaterThan
    • GreaterThanEquals
    • Between
    • Like
    • Ilike (i.e. ignorecase like)
    • IsNotNull
    • IsNull
    • Not
    • Equal
    • NotEqual
    • And
    • Or
    Dynamic Finders