Rapid Web Application Development with Grails (2006)
Download
Report
Transcript Rapid Web Application Development with Grails (2006)
Rapid Web Application
Development with Grails
Graeme Rocher
Managing Director
Agilize it
http://www.agilizeit.com
Session ID# BOF-2521
2006 JavaOneSM Conference | Session BOF-2521 |
Goal of this Talk
Rapid Web Application Development with Grails
Learn how to rapidly create web
applications using the agile web
application framework Grails
2006 JavaOneSM Conference | Session BOF-2521 |
2
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages (GSP)
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
3
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages (GSP)
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
4
Groovy & Grails
●
Grails: MVC web framework inspired by:
●
●
●
●
Built on solid foundations:
●
●
●
●
Convention over Configuration
Don’t Repeat Yourself (DRY)
Ruby on Rails
Spring IoC, MVC and WebFlow
Hibernate
SiteMesh
Why Groovy?
●
Meta-Programming
Closure Support
Syntactically Expressive
●
Java Integration
●
●
Source: Please add the source of your data here
2006 JavaOneSM Conference | Session BOF-2521 |
5
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages (GSP)
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
6
Getting Started
●
●
●
Grails available from http://grails.org
Stable & Development snapshots available
Simple installation:
●
●
●
●
Download & extract zip
Set GRAILS_HOME variable
Add $GRAILS_HOME\bin to PATH variable
Run “grails create-app”
Source: Please add the source of your data here
2006 JavaOneSM Conference | Session BOF-2521 |
7
Project Infrastructure
+ PROJECT_HOME
Main Grails resources
+ grails-app
+ conf
+ controllers
+ domain
+ i18n
+ services
Jar archive libraries
+ taglib
+ views
Additional Spring configuration
+ lib
+ spring
+ hibernate
Additional Hibernate mapping
+ src
Java sources
+ web-app
Web resources e.g. CSS, JavaScript etc.
2006 JavaOneSM Conference | Session BOF-2521 |
8
Command Line Targets
●
●
Apache Ant bundled with Grails
Many useful targets available:
●
●
●
●
●
●
create-* (for creating Grails artifacts)
generate-controller
generate-views
run-app
test-app
run-webtest
Source: Please add the source of your data here
2006 JavaOneSM Conference | Session BOF-2521 |
9
The Data Source
// data source located in grails-app/conf
Class ApplicationDataSource {
Whether connection
Pooling is enabled
@Property pooled = false
DB Auto creation with
hbm2ddl
url = “jdbc:hsqldb:mem:testDb”
driverClassName = “org.hsqldb.jdbcDriver”
username = “sa”
password = “sa”
@Property dbCreate = “create-drop”
@Property
@Property
@Property
@Property
}
Remaining connection
settings
2006 JavaOneSM Conference | Session BOF-2521 |
10
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages (GSP)
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
11
The Application Domain
●
●
●
●
Domain classes hold state and implement
behaviour
They are linked together via relationships (e.g.
one-to-many)
In Java domain classes have traditionally been
handled by Object-Relational Mapping (ORM)
Grails provides simple ORM built on Hibernate
Source: Please add the source of your data here
2006 JavaOneSM Conference | Session BOF-2521 |
12
Grails ORM (GORM)
●
Extremely simple. No special class to extend or file to configure!
class ExpenseReport {
@Property Long id
@Property Long version
Each domain class
has an ‘id’ and ‘version’
@Property relatesToMany = [items:ExpenseItem]
@Property Set items
@Property Date submissionDate
@Property String employeeName
Defines one-to-many
relationship
to ExpenseItem
}
2006 JavaOneSM Conference | Session BOF-2521 |
13
Grails ORM (GORM)
●
We’ve got this far, so lets define the other side!
class ExpenseItem {
@Property Long id
@Property Long version
Defines the owning
side of the relationship
@Property belongsTo = ExpenseReport
@Property String type
@Property Currency currency
@Property Integer amount
Each property maps
To a column
}
2006 JavaOneSM Conference | Session BOF-2521 |
14
Grails Constraints
●
Validation constraints can be defined using the
‘constraints’ property
class ExpenseItem {
…
@Property constraints = {
type(inList:['travel', 'accomodation'])
Each node relates to amount(range:1..999)
a property
}
Ensures the ‘type’ property
Is one of the values in the list
}
‘amount’ must be
in a range greater than 0
but less than 1000
2006 JavaOneSM Conference | Session BOF-2521 |
15
Dynamic Methods & Properties
●
Grails injects methods and properties into domain
classes at runtime:
def r = ExpenseReport.findByEmployeeName('fred')
def r = ExpenseReport
.findBySubmissionDateGreaterThan(lastMonth)
def reports = ExpenseReport.findAll()
assert ! (new ExpenseItem().validate())
def er = new ExpenseReport(employeeName: 'Edgar')
.save()
2006 JavaOneSM Conference | Session BOF-2521 |
16
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages (GSP)
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
17
Controllers
●
●
●
●
Controllers handle requests and prepare
responses
Response created by either delegating to a view
or writing to the response
A controller is a class containing closure
properties that act on requests
The convention used for the name of the
controller and the actions within map to URIs.
Source: Please add the source of your data here
2006 JavaOneSM Conference | Session BOF-2521 |
18
The Controller
●
The controller and action name map to the URI:
/expenseReport/list The name of the
class is the first
token in the URI
class ExpenseReportController {
@Property list = {
Each action is a
closure property
[expenseReports :
ExpenseReport.list()]
}
}
An optional model
is returned as a
map
2006 JavaOneSM Conference | Session BOF-2521 |
19
Data Binding & Flow Control Dynamic get method
Auto-type conversion
To id type
// save action
@Property save = {
def e = ExpenseItem.get(params.id)
e.properties = params
Auto-type conversion if(e.save()){
from request
redirect(action:show,id:e.id)
parameters
} else {
Example flow
control
via render and
redirect
methods
render( view: 'create',
model:[expenseItem:e] )
}
}
2006 JavaOneSM Conference | Session BOF-2521 |
20
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages (GSP)
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
21
Groovy Server Pages
●
●
●
●
A view technology very similar to JSP, but with
Groovy as the primary language
More expressive and concise with support for
embedded GStrings & Tags
Layout support through integration with
SiteMesh
Ability to define dynamic tag libraries
Source: Please add the source of your data here
2006 JavaOneSM Conference | Session BOF-2521 |
22
A GSP Example
●
The GSP for the list action is named according to
convention: grails-app/views/expenseItem/list.gsp
<html>
<body>
<g:each in="${expenseItems}">
References the model
returned by the
controller
<p>${it.type} – amount: ${it.amount}</p>
</g:each>
</body>
</html>
Embedded GString
expressions
2006 JavaOneSM Conference | Session BOF-2521 |
23
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
24
Dynamic Tag Libraries
●
Easy definition of simple, logical and iterative
The body argument
tags:
is a closure that
can be invoked
The name of the
tag
class ExpenseTagLib {
@Property dateFormat = { attrs,body ->
out << new SimpleDateFormat(attrs.format)
.format(attrs.date)
}
}
The attributes are
passed as a map
2006 JavaOneSM Conference | Session BOF-2521 |
25
Dynamic Tag Libraries
●
Using the tag requires no imports or configuration and
can be reloaded at runtime!:
Tag called by name
<p>Submitted:
with the “g:” prefix
<g:dateFormat
date="${report.submissionDate}"
format="DD-MM-YYYY" />
</p>
Tag can also be
<p><input type="hidden"
called as a regular
name="submissionDate"
method!
value="${dateFormat(
date:report.submissionDate,
format:'DD-MM-YYYY')}" />
</p>
2006 JavaOneSM Conference | Session BOF-2521 |
26
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages
Tag Libraries
Ajax Support
Scaffolding
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
27
AJAX Support
●
●
●
●
●
Built in “adaptive” tag library for Ajax
Supports Prototype, Rico, and Yahoo (Dojo
coming soon)
Tags for remote linking, asynchronous form
submission etc.
Dynamic “render” method available for
rendering XML snippets, or partial templates
Save/Reload and dynamic tag libraries make
Ajax even easier
2006 JavaOneSM Conference | Session BOF-2521 |
28
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages
Tag Libraries
Scaffolding
Ajax Support
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
29
DEMO
Scaffolding
2006 JavaOneSM Conference | Session BOF-2521 |
30
Agenda
Groovy & Grails
Getting Started
The Application Domain
Controllers
Groovy Servers Pages
Tag Libraries
Scaffolding
Ajax Support
Java Integration
2006 JavaOneSM Conference | Session BOF-2521 |
31
Java Integration
●
●
●
●
●
●
Now for the important bit, with Groovy and
Grails you can:
Call any existing Java library seamlessly
Deploy as a WAR onto any JEE application
server
Write your domain model in Java, mapped with
Hibernate, and still use dynamic methods!
Take advantage of Hibernate’s power by
mapping onto legacy systems.
Use Spring’s dependency injection to integrate
Controllers with existing services
2006 JavaOneSM Conference | Session BOF-2521 |
32
DEMO
ECLIPSE INTEGRATION
2006 JavaOneSM Conference | Session BOF-2521 |
33
Summary
●
●
●
●
●
With the advent on Web 2.0 agility is key
Dynamic frameworks (Grails, Rails, Django etc.)
provide this through quick iterative development
with a clear productivity gain
However, for large scale applications statictyping and IDE support is crucial
Grails provides the ability to use a blended
approach
And most importantly it runs on the JVM!
2006 JavaOneSM Conference | Session BOF-2521 |
34
For More Information
●
●
●
●
●
Groovy website – http://groovy.codehaus.org
Grails website – http://grails.org
Mailing lists – http://grails.org/Mailing+lists
Graeme’s Blog – http://graemerocher.blogspot.com
Upcoming books ‘The Definitive Guide to Grails’ by
Apress and ‘Groovy in Action’ by Manning
2006 JavaOneSM Conference | Session BOF-2521 |
35
Q&A
2006 JavaOneSM Conference | Session BOF-2521 |
36