Getting Started with Grails

Rapid Web Development for the Java Platform

Presented at the Princeton Java Users Group June 5th, 2007 1

Creative Commons License
• •
This presentation is published under the Creative Commons Attribution Noncommercial Share Alike License Version 2.5. (Please see http://creativecommons.org/licenses/by-nc-sa/2.5/ for complete details.) You are free to reuse and modify this presentation under the following conditions. (Thanks to Steven Devijver for providing this excellent summary of the license terms.):

‣ ‣ ‣ ‣ ‣ ‣ ‣ ‣ ‣

You don’t get compensated for providing this presentation (non-commercial). You don’t get compensated for presenting this presentation in front of your audience (noncommercial). You are exempted from this condition if you are tutoring your colleagues. You do not charge members of your audience for a digital copy of this presentation. You are free to charge your audience for printed versions of this presentation or for the media that contains this presentation, but only if you personally actually incur a nonrecoverable cost. You keep and do not modify the content of this license slide in the presentation (share alike). You attribute that Jason Rudolph is the original creator and maintainer of this presentation (attribution). If you modify the content and distribute or modify the content and show this presentation to an audience, you are required to send your modified file to contact@jasonrudolph.com. You have the right to call yourself co-author if you modify the content of this presentation in a non-trivial way. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one (share alike).

2

Introduction
• Jason Rudolph • Grails Committer • Software Consultant • Author

3

Agenda
• Grails? What’s that? • Hello, Productivity • Painless ORM (Seriously) • Validation Made Simple • A New Point of View • Cool (and Groovy) Features • Test, Deploy, Profit!
4

What is Grails?
• Rapid web application development
‣ Getting things done ‣ Having fun doing it

• Full-stack MVC framework • Open source • Runs on the JVM

5

What (else) is Grails?
• Develop your apps in Groovy • Convention over configuration
‣ Opinionated software ‣ Sensible defaults ‣ Everything has a place

• DRY (Don't Repeat Yourself)

6

Rock-Solid Foundation

7

Let's Get Started
• Ready?
‣ Install JDK 1.4 (or higher) ‣ Set JAVA_HOME

• Set
‣ Download latest Grails release ‣ Set GRAILS_HOME ‣ Add $GRAILS_HOME/bin to your PATH

8

Hello, Grails
Demo

9

What’s Inside?

10

Instant Gratification
Demo

11

Grails ORM (GORM)
• Zero-configuration persistence
management
‣ save ‣ delete ‣ get ‣ list

• DAOs Dynamic domain class methods
‣ find ‣ findAll ‣ count ‣ etc.

12

Relationship Counseling

13

1:n Relationships
class Race { //... static hasMany = [registrations:Registration] }

class Registration { //... Race race } static belongsTo = Race

14

Relationship Counseling
Demo

15

Where’s My Data?

16

Be Persistent
Demo

17

Declaring Constraints
• Pre-packaged constraints
‣ blank ‣ creditcard ‣ email ‣ inList ‣ min ‣ max ‣ matches ‣ nullable ‣ range ‣ unique ‣ url ‣ etc.

• Custom constraints
18

Show Some Constraint!
Demo

19

Fun with URLs
http://localhost:8080/racetrack/race/show/2

20

Fun with URLs
http://localhost:8080/racetrack/race/show/2

class RaceController { //... def show = { [ race : Race.get( params.id ) ] } //... }

21

GSP

22

GSP
RaceController.groovy
def show = { [ race : Race.get( params.id ) ] }

views/race/show.gsp
<g:each var="r" in="${race.registrations}"> <li> <g:link controller="registration" action="show" id="${r.id}">${r.name} </g:link> </li> </g:each>
23

Dynamic Finder Methods

Get Dynamic

• What if I need to...
‣ find all races in New Jersey? ‣ count the races longer than 10 km? ‣ get a list of the 10 longest races? ‣ find races matching a certain pattern? ‣ list all races with registrations?

24

Get Dynamic
Demo

25

Putting It to the Test
• Unit testing
‣ GroovyTestCase created for each class ‣ grails test-app executes all test cases

• Functional testing
‣ Canoo Web Test ‣ grails generate-webtest ‣ grails run-webtest

26

Deployment
grails war

27

Beyond the Finish Line
• Services
‣ transactional ‣ non-transactional

• Java Integration (src/java) • Rich (and Friendly) Tag Libraries • Ajax

28

Still More to Explore
• Custom URL Mapping • Alternate Domain Implementations
‣ Using Hibernate XML for legacy schemas ‣ EJB3 entity beans

• Command Objects for Forms • Job Scheduling • Layouts & CSS
29

grails.org/plugins
• Build rich clients • Expose web services • Secure your app • Roll your own scaffolding • Integrate a search engine • Almost limitless possibilities
30

grails.org/roadmap
• Future releases (Pre 1.0)
‣ Support for JSP custom tag libraries in GSP ‣ Spring Web Flow ‣ Domain model generation from database schema ‣ JPA support

31

Important Companies Talking About Grails
• Oracle
http://www.oracle.com/technology/pub/articles/grallgrails.html

• BEA
http://dev2dev.bea.com/pub/a/2006/10/introductiongroovy-grails.html

• Sun (JavaOne 2007)
http://www28.cplan.com/cc158/session_details.jsp? isid=286133&ilocation_id=158-1&ilanguage=english

32

Resources
• grails.org
‣ Downloads
✦ Latest release (0.5) ✦ Development snapshot (0.5.5)

‣ User guide, tutorials, screencasts, etc. ‣ Mailing lists (grails.org/mailing+lists)

33

Resources (cont’d)
aboutGroovy.com

grails.org/grails+podcast

groovyblogs.org

34

2007 No Fluff Just Stuff

Atlantic Northeast Software Symposium August 10-12th Westin Princeton at Forrestal Village nofluffjuststuff.com/sh/2007-08-princeton

35

Grails eXchange 2007

October 17-19th Barbican, London, UK grails-exchange.com
36

Books

37

www.jasonrudolph.com jason@jasonrudolph.com
38