You are on page 1of 71

Agile Web Development with

Groovy & Grails


Groovy & Grails
Groovy
Groovy

Why?

© 2008 BearingPoint, Inc. 3


Groovy

Java Jobs
© 2008 BearingPoint, Inc. 4
Groovy

Java EE Jobs
© 2008 BearingPoint, Inc. 5
Groovy

Ruby On Rails Jobs


© 2008 BearingPoint, Inc. 6
Groovy

JSR 241

© 2008 BearingPoint, Inc. 7


Groovy

„Java-stylish with a Ruby-esque feeling“

© 2008 BearingPoint, Inc. 8


Groovy

 „Groovy is not a replacement for the Java programming


language; it‘s a complement to that language“
Richard Monson-Haefel, award winning author & technical analyst

© 2008 BearingPoint, Inc. 9


Groovy

GroovyBeans
class GroovyBean {
String name
}

© 2008 BearingPoint, Inc. 10


Groovy

Autoboxing
def b = new GroovyBean()
b.name = “groovy bean“

© 2008 BearingPoint, Inc. 11


Groovy

Duck Typing
 „When I see a bird that walks like a
duck and swims like a duck and
quacks like a duck, I call that bird a
duck.“
Poem by James Whitcomb Riley

© 2008 BearingPoint, Inc. 12


Groovy

Expressiveness

© 2008 BearingPoint, Inc. 13


Groovy

Java:
java.net.URLEncoder.encode(“a b“);

Groovy:
URLEncoder.encode ‘a b‘

© 2008 BearingPoint, Inc. 14


Groovy

GStrings

© 2008 BearingPoint, Inc. 15


Groovy

def name = “world“


def message = “Hello ${name}“

© 2008 BearingPoint, Inc. 16


Groovy

def text = “““


Ein längerer String
mit Zeilenumbrüchen
“““

© 2008 BearingPoint, Inc. 17


Groovy

Native Lists, Maps, Regex

© 2008 BearingPoint, Inc. 18


Groovy

Lists
def aList = []

def myList = [22,55,33]

© 2008 BearingPoint, Inc. 19


Groovy

Maps
def emptyMap = [:]

def myMap = [“name“: “Max“,


“age“ : “33“ ]

© 2008 BearingPoint, Inc. 20


Groovy

Maps
myMap.name == “Max“

myMap[“age“] == “33“

© 2008 BearingPoint, Inc. 21


Groovy

Regular Expressions
“cheesecheese“ =~ “cheese“
“cheesecheese“ =~ /cheese/

“potatoe“ ==~ /potatoe?/


“potato“ ==~ /potatoe?/

© 2008 BearingPoint, Inc. 22


Groovy

Loops
for (int i=0;i<6;i++) {}

def list = [1, 2, 3]


for (i in list) {}

for (i in 0..9) {}

10.times {}

© 2008 BearingPoint, Inc. 23


Groovy

Closures
def myClosure = {
/* do something */
}

© 2008 BearingPoint, Inc. 24


Groovy

[3,1,2].each {
print “value:“+it
}

© 2008 BearingPoint, Inc. 25


Groovy

Builders
def xml = new groovy.xml.MarkupBuilder()
xml.authors {
author(name:‘Stephen King‘) {
book(title:‘The Shining‘)
book(title:‘The Stand‘)
}
author(name:‘James Patterson‘) {
book(title:‘Along Came a Spider‘)
}
}

© 2008 BearingPoint, Inc. 26


Groovy

Builders
<authors>
<author name=“Stephen King“>
<book title=“The Shining“ />
<book title=“The Stand“ />
</author>
<author name=“James Patterson“>
<book title=“Along Came a Spider“ />
<author>
<authors>

© 2008 BearingPoint, Inc. 27


Groovy

The Groovy Truth


if (myString != null && myString.length() > 0) {
myMethod(myString);
}

if (myString) myMethod(myString)

© 2008 BearingPoint, Inc. 28


Groovy

Save Dereferencing
Long id=null;
if (session.getAttribute(“user“) != null) {
id = ((User) session.getAttribute(“user“)).getId();
}

def id = session.user?.id

© 2008 BearingPoint, Inc. 29


Groovy

Meta Class Concept

© 2008 BearingPoint, Inc. 30


Groovy

IDE Integration

© 2008 BearingPoint, Inc. 31


Groovy

Domain Specific Language


(DSL)

© 2008 BearingPoint, Inc. 32


Groovy

Java:
public class RecursiveFileReading {

public static void main(String[] args) {


printAllFiles(new File("."));
}

public static final void printAllFiles(File root) {


File[] allChildren = root.listFiles();
for (File child : allChildren) {
if (child.isDirectory()) {
RecursiveFileReading.printAllFiles(child);
}
else {
System.out.println(child.toString());
}
}
}
}

© 2008 BearingPoint, Inc. 33


Groovy

Groovy:
groovy -e “new File(‘.‘).eachFileRecurse { println it }“

© 2008 BearingPoint, Inc. 34


Groovy

 „There is a value for learning Groovy for its own sake. It


will open your mind...“

© 2008 BearingPoint, Inc. 35


Grails
Grails

Rapid Application Develoment


(RAD)

© 2008 BearingPoint, Inc. 37


Grails

Full Stack Framework

© 2008 BearingPoint, Inc. 38


Grails

Convention Over Configuration


(CoC)

© 2008 BearingPoint, Inc. 39


Grails

RESTfull
Representational State Transfer

© 2008 BearingPoint, Inc. 40


Grails

GET

© 2008 BearingPoint, Inc. 41


Grails

POST

© 2008 BearingPoint, Inc. 42


Grails

PUT

© 2008 BearingPoint, Inc. 43


Grails

DELETE

© 2008 BearingPoint, Inc. 44


Grails

HEAD

© 2008 BearingPoint, Inc. 45


Grails

OPTIONS

© 2008 BearingPoint, Inc. 46


Grails

RESTfull
http://localhost:8080/application/user/profile/123

Controller Action ID

© 2008 BearingPoint, Inc. 47


Grails

DRY Paradigm
(Don‘t Repeat Yourself)

© 2008 BearingPoint, Inc. 48


Grails

 Leverages existing hardware


 Leverages existing Java/Framework knowledge

© 2008 BearingPoint, Inc. 49


Grails

Components

Grails

Java EE

Java Language JDK

Java Virtual Machine

© 2008 BearingPoint, Inc. 50


Grails

UI, AJAX & RIA Frameworks

YUI GWT

© 2008 BearingPoint, Inc. 51


Grails

Plugin API
 JSF
 Struts
 JMS
 ACEGI/Spring Security, JSecurity
 OpenID
 Functional Testing (Canoo WebTest)
 Apache Axis 2, XFire

© 2008 BearingPoint, Inc. 52


Grails

GORM
Groovy Object-Relational Mapping

© 2008 BearingPoint, Inc. 53


Grails

GORM

class Book {
String title
}

© 2008 BearingPoint, Inc. 54


Grails

GORM
class Book {String author
String title
Date releaseDate

static constraints = {
author(blank:false, size:2..30)
releaseDate(nullable: true) }
}
}
}

© 2008 BearingPoint, Inc. 55


Grails

GORM

def book = new Book()

book.author = “author A“
book.title = “title 1“

book.save()

© 2008 BearingPoint, Inc. 56


Grails

GORM
def book = new Book()

book.author = “author A“
book.title = “title 1“

if (book.save()) {
/* successfull flow */
} else {
/* error flow */
}

© 2008 BearingPoint, Inc. 57


Grails

(Dynamic) Finders
def macBook = Book.get(1)

def macBook = Book.findByTitle(“MacBook“)

def macBook = Book.findByTitleAndAuthor(“MacBook“, “Apple“)

© 2008 BearingPoint, Inc. 58


Grails

Groovy Server Pages (GSP)


<h1>Book list</h1><table> <tr>
<th>Title</th> <th>Author</th>
</tr><g:each in="${books}">
<tr>
<td>${it.title}</td>
<td>${it.author}</td>
</tr>
</g:each>
</table>

© 2008 BearingPoint, Inc. 59


Grails

Groovy Server Pages (GSP)

<g:if test="${name == 'fred'}">


Hello Fred!</g:if><g:else>
Hello ${name}! Do I know
you?</g:else>

© 2008 BearingPoint, Inc. 60


Grails

Groovy Server Pages (GSP)

<g:datePicker name="myDate" value="${new Date()}"


precision="day" years="${1930..1970}"/>

© 2008 BearingPoint, Inc. 61


Grails

Scaffolding
 Dynamic Scaffolding

 Static Scaffolding

© 2008 BearingPoint, Inc. 62


Grails

How does it look like?

© 2008 BearingPoint, Inc. 63


Grails

Success Stories?

© 2008 BearingPoint, Inc. 64


Grails

© 2008 BearingPoint, Inc. 65


Grails

© 2008 BearingPoint, Inc. 66


Grails

© 2008 BearingPoint, Inc. 67


Grails

© 2008 BearingPoint, Inc. 68


Grails

http://groovy.codehaus.org/

http://www.grails.org

© 2008 BearingPoint, Inc. 69


Grails

Dirk König - Groovy In Action


Manning, 2007

Graeme Keith Rocher - The Definitive Guide To Grails


Apress, 2006

© 2008 BearingPoint, Inc. 70

You might also like