INTRODUCTION TO GRAILS

tastelessbeachInternet and Web Development

Nov 12, 2013 (3 years and 9 months ago)

73 views

IN
TRODUCTION TO GRAILS

Christopher Bartling


WHAT IS GRAILS?

Grails is a next
-
generation Java web development framework that generates great
developer productivity gains through the confluenc
e of a dynamic language, a Con
vention
over Configuration
philosophy, powerfully pragmatic supporting tools, and an agile
perspective drawn from the best emerging web development paradigms.



WHY GRAILS?

Things like Convention over Configuration, scaffolding, code templates, and easy database
integration
make

bo
otstrapping an application lightning fast.

GROOVY

Grails makes

use of
Groovy,
a dynamic language
for the Java Virtual Machine.

Grails
eases
the tediousness that currently permeates the Java landscape, making web development fun
again
.
Grails possesses a

c
onciseness and expressiveness that makes
development

easier
and mo
re maintainable
.

CONVENTION OVER CONF
IGURATION

Grails makes most of its decisions based on sensible defaults drawn from your source code:



Add a controller class called Shop with an action ca
lled order, and Grails will expose
it as a URL of /yourapp/shop/order.



Place your view files in a directory called /views/shop/order, and Grails will look
after linking everything up for you without a single line of configuration.



Create a new domain class

called Customer, and Grails will automatically create a
table called customer in your database.



Add some fields to your Customer object, and Grails will automatically create the
necessary fields in your customer table on the fly (including the right data
types
based on the validation constraints you place on them). No SQL required.

But as Jason Rudolph is quick to point out, Grails is about Convention over Configuration,
not Convention instead of Configuration. If you need to tweak the defaults, all the po
wer is
there for you to do so. Grails makes overriding the defaults easy, and you still won’t need
any XML. But if you want to use your existing Hibernate configuration XML files in all their
complex glory, Grails won’t stand in your way.


UNDER THE HOOD

G
rails
is

built on Spring, Hibernate, and other libraries already popular in enterprise Java

the stuff developers were already building applications on. It was like turbo
-
charging
existing development practices without sacrificing reliability or proven tech
nologies.

SPRING FRAMEWORK


SPRING MVC


HIBERNATE


SITEMESH


SERVLET API


MODEL
-
VIEW
-
CONTROLLER

MODELS (DOMAIN OBJEC
TS)

Writing the domain model for a Grails application is significantly simpler than with many
other frameworks.

By default, all the fields
in a domain class are persisted to the database.
For simple field types such as Strings and Integers, each field in the class will map to a
column in the database.

In addition to the fields on your domain class, an
id column and a
version column

are added

to the mapped database table
. The
id

is a unique identifier for a
row and Grails uses the
version

column to implement optimistic locking
.


RELATIONSHIPS

Grails provides support for several types of relationships between domain classes. In a one
-
to
-
one re
lationship (the simplest type), each member of the relationship has a reference to
the other.


belongsTo property

The value of the
belongsTo

property in the Engine class is a Map. The key in this map is “car”
and the value associated with that key is the
Car class. This property tells Grails that the Car
is the owning side of this relationship and that an Engine “belongs to” its owning Car. The
key in the map can be named anything

the name does not need to be the same as the
owning
-
class name. However, nam
ing the key that way almost always makes sense. That
key represents the name of a property that will be added to the Engine class, as well as
representing the reference back to

the owner. The Engine class
has a property called
car

of
type Car.

You might en
counter situations where a relationship needs an owning side but the owned
side of the relationship does not need a reference back to its owner. Grails supports this
type of relationship using the same belongsTo property, except that the value is a Class
r
eference instead of a Map.
T
he Engine still belongs to its owning Car, but the Engine has no
reference back to its Car.

One of the implications of having the belongsTo property in place is that Grails will impose
cascaded deletes
. Grails knows that an Engi
ne “belongs to” its owning Car, so any time a Car is
deleted from the database, its Engine will be deleted as well.


hasMany property

One
-
to
-
many relationships are equally simple to represent in Grails domain classes

(e.g. an
Artist and its Albums
,
an Albu
m and its Songs
)
.
The

“has many” relationship is expressed in
a domain class with the
hasMany

property
.
The value of the hasMany property needs to be a
Map. The keys in the map represent the names of collection properties that will be added to
the domain
class, and the values associated with the keys represent the types of objects that
will be stored in the collection property. The Artist class has a domain property called
albums that will be a collection of Album objects. The default collection type that
Grails will
use is a java.util.Set, which is an unordered collection. Where this is the desired behavior,
you don’t need to declare the property explicitly. Grails will inject the property for you. If
you need the collection to be a List or a SortedSet, yo
u must explicitly declare the property
with the appropriate type
.

A domain class might represent the owning side of numerous one
-
to
-
many relationships.
The Map associated with the hasMany property might have any number of entries in it, each
entry repres
enting another one
-
to
-
many
-
relationship.

VALIDATION

A Grails domain class can express domain constraints simply by defining a public static
property named
constraints

that has a closure as a value.

The constraints block in a domain
class will help preven
t invalid data from being saved to the database. When the object is
saved to the database, the domain object will automatically validate against the constraints
before any data is written to the database. If any of the validation constraints fail, the wr
ite
operation is aborted.

All domain classes have an extra transient property on them
--

the
errors

property
. This
property is an instance of the Spring Framework’s
org.springframework.
validation.Errors

interface, which allows advanced querying of validation errors.

This property will contain
all the validation errors after completing the validation step.

All domain objects have a
method called
clearErrors()
, which will clear any errors left over from
a previous validation
attempt.

Grails also provides the ability to build your own validators. In the constraints closure, you
can pass a closure that will perform validation on one or more of the properties of your
domain class. That closure needs to ret
urn a Boolean result or a String result. The String
result maps to a custom error message resource if the validation fails.




CONTROLLERS


VIEWS


LAYOUTS WITH SITEMES
H



GORM

Grails provides a powerful DSL for expressing how a domain model maps to the
database

(the
mapping
closure)
.

Grails provides an ORM DSL for expressing your domain mapping
to help you deal with scenarios in which the Grails defaults will not work for you.

A
common use case for taking advantage of the ORM DSL is when a Grails appli
cation is being
developed on top of an existing schema that is not entirely compatible with Grails’ default
domain
-
class mappings.

T
o take advantage of the ORM DSL, your domain class must
declare a public property called mapping and assign a closure to th
e property
.



GROOVY AS THE GLUE




PLUGINS