Introduction to Grails

Introduction to Groovy on Grails - Created for Harrisburg Java Users Group 18-Mar-10 »
Gordon Dickens

Rapid Web Development


Gordon Dickens
March, 2010
DEMO 1
Create App
Create Student Domain
Add fields
Create Controller
Scaffold
Run
Demo II
Edit Constraints

field length
blank:false
unique

Run

GORM
Grails Object Relational Mapping

Object Relationships
Persistence Methods
Dynamic Finders
What is Grails?
Rapid web development framework
Convention over Configuration

Open Source
Instant feedback
Fun to work with


Groovy Structures
Common collection types in Brackets

Lists
def myList = ["one", "two", "three"]

Maps
def myMap = [dairy:"cheese", meat:"chicken", shopper:"Mario"]
Groovy for Java Developers
Optional parenthesis
Optional semicolons
Optional try/catch
Built-in getter/setter
Named arguments
Groovy Basics
It is Java... sorta
Everything is an object

Groovy wraps/imports common packages for you:
java.lang
java.util
java.net
java.io
java.math.BigDecimal & BigInteger


What is Grails?
Web Framework
Implements Full Stack  from UI to ORM
Leverages BoB Technologies: Spring, Hibernate Quartz
Extensible plug-in system, over 350 plugins available
Grooviness in XML Processing
def langs = new XmlParser().parse("languages.xml")
println "type = ${langs.attribute("type")}"
langs.language.each{
  println it.text()
}
Groovy Closures
Similar to anonymous inner classes
Code blocks that can accept parameters
Can be assigned to a variable

def myClosure = {x, y -> println "you sent $x and $y" }
myClosure 'Feet', 'Hans'

prints: you sent Feet and Hans
Services Layer
Transactions

Class Level
       static transactional = true

Method Level
       @Transactional(readOnly = true) 

Generating Template Code

grails generate-views
grails generate-controller
grails generate-all
Object Relationships
One to One
One to Many
Many to Many

Course -> hasMany -> Students



Controllers
Control app flow
Based on Spring MVC

Where is the model & view?

3R's - Return, Redirect, Render

Resources
Console
Interactive console allows you to test out GORM functionality
From command prompt:
       grails console

Demo
Add Student
List Students
Persistence Methods
new
get ()
read ()
save ()
delete ()
Dynamic Finders 
findBy...

Boolean And/Or

    * Like
    * Ilike
    * NotEqual
    * Between
    * IsNotNull
    * IsNull

    * InList 
    * LessThan
    * LessThanEquals
    * GreaterThan
    * GreaterThanEquals

Examples:

def student = Student.findByFirstName('Gordon')
def student = Student.findByFirstNameAndLastName('Gordon', 'Dickens')
Invoking setters via dot notation
Rarely write constructors
Can skip "System.out"

Dynamic types
def var = new MyClass()
def var = MyStaticClass.instance
import org.hbgjug.*

def student = new Student (lastName:"Dickens", firstName:"Gordon")
student.save()
import org.hbgjug.*

def student = new Student (lastName:"Dickens", firstName:"Gordon")
student.save()

student.firstName = "Gordo"
println student

student.setFirstName "Goreaux"
println student

student."firstName" = "Stewie"
println student

Groovy Summary

Easy for Java developers
Flexible
Shortcuts for common tasks
Grails Summary

Rapid Web Development
CoC
Validation
GORM - Object Relations
GORM - Dynamic Finders
GORM - Easy Persistence methods

Easy Transactions with Service Objects
Groovy Console

Handy UI utility for testing out Groovy scriptlets

from command prompt:
       groovyconsole
Contacts Info

gdickens@chariotsolutions.com
twitter.com/gdickens
linkedin.com/in/gordondickens
www.gordondickens.com
Spring Roo
Books
The Definitive Guide to Grails 2nd ed
Grails in Action
Grails a Quickstart Guide
Getting Started with Grails, 2nd ed (free pdf)

Groovy in Action
Groovy Recipes
Programming Groovy
Web Sites
Grails - grails.org
Groovy - groovy.codehaus.org

grailsframework.blogspot.com

Gradle - gradle.org
Griffon - griffon.codehaus.org
Gaelyk - gaelyk.appspot.com

What is Groovy?
Dynamic Language for JVM
Compiles to byte code
Can import any java existing java library
Almost no learning curve

techcast.chariotsolutions.com
Interviews with industry leaders in new technology
Weekly news casts
phillyemergingtech.com
Convention over Configuration - Code

Student
StudentController
student/*.gsp
StudentService
StudentTests

Convention over Configuration - Directories

    * grails-app - top level directory for Groovy sources
          o conf - Configuration sources.
          o controllers - Web controllers - The C in MVC.
          o domain - The application domain.
          o i18n - Support for internationalization (i18n).
          o services - The service layer.
          o taglib - Tag libraries.
          o views - Groovy Server Pages.
    * scripts - Gant scripts.
    * src - Supporting sources
          o groovy - Other Groovy sources
          o java - Other Java sources
    * test - Unit and integration tests.

Adding a Service to your Controller

StudentController {
    def studentService
    ...
}
class Course {
  static hasMany = [students: Student]
...
Philly Groovy & Grails User Group
www.phillygroovy.org

Scott Davis (Groovy Recipes and Getting Started with Grails, 2ed.)
Venkat Subramaniam (Programming Groovy)

Loading comments...

Please log in to add your comment.

Report abuse

More presentations by Gordon Dickens