Escolar Documentos
Profissional Documentos
Cultura Documentos
GRAILS
• What is Grails & how do I create my first Grails app?
• What are the powerful cornerstones of Grails?
• Which features does Grails provide out of the box?
Agenda
Grails MVC
• Grails Basics • Grails is an MVC Web Framework
• Grails Foundation – Inspired by Ruby on Rails
• Grails MVC – Uses the powerful Groovy Scripting Language
– Convention Over Configuration
• Grails Features
– Don't Repeat Yourself (DRY)
Spring Hibernate
• Spring Framework is used to hold everything together • Hibernate is the de facto standard for O/R Mapping
• You can specify your own Spring Beans for injection into Controllers, • Grails maps your Domain Classes automatically, even creates and
Services, Jobs in spring/resources.xml exports the schema to the database
• Good to know: a Grails app is an Spring MVC app... Spring Webflow to – 1:n & m:n supported!
be used in future • Great flexibility: use your own HBM files for legacy database schemas!
• SiteMesh is a powerful Layout Framework and integrated into Grails • Reuse (see foundations)
• grails-app/views/layout/main.gsp is the standard layout file • No “reinventing the wheel”
• This meta element in a head of a gsp file links it with the “main” layout: • Coding by Convention
<meta name="layout" content="main" /> – without being locked into a single solution!
• Domain-Centric, not DB-Centric
• DRY
Agenda Model
class Book {
> grails create-domain-class Author author
String title
}
• You will typically use this convenience target to create Domain • 1:1 mapping with Author
Classes • Specify the “owning side” with this
• Will ask you for the name, make it uppercase if you forget
class Book {
def belongsTo = Author
Author author
String title
}
Model Model
class Author {
• m:n possible, too!
def hasMany = [ books : Book ]
String name • BelongsTo defines “owning side”
}
class Author {
author.addBook(new Book(title:'Grails'))
def hasMany = [books:Book]
author.save()
}
Model Model
class DevelopmentDataSource {
• Database configuration is done in boolean pooling = true
grails-app/conf directory // one of 'create', 'create-drop','update'
– DevelopmentDataSource.groovy //String dbCreate = "create-drop"
• “grails war” creates your deployment war file with the production datasource
• BootStrap files are picked up at startup and can be used to create • Groovy Server Pages(GSP) or
initial (test) data JavaServer Pages (JSP)
• TagLibs for both, but GSP is groovin'
• All views are in the grails-app/views directory
• Generate views for your domain class:
class CompanyBootStrap {
“grails generate-views”
def init = { servletContext ->
//Delete all data in the tables that we work on
Company.executeUpdate("delete Company")
//now create the test data
Company c1 = new Company(name:'bmw')
c1.addUser(new User(name:'herbert', admin:true, ...))
.save()
}
...
View View
• TagLib example (“simple” tag) • Logical and iterative tags just as easy.
• You can even call tags as “methods” within GSP – here used as
class MyTagLib { normal tag:
includeJs = { attrs ->
out << "<script src='scripts/${attrs['script']}.js' />"
}
<g:hasErrors bean="${book}" field="title">
}
<span class='label error'>There were errors on the book title</span>
</g:hasErrors>
View Controller
• Creating markup from tags is a piece of cake: • All controllers are mapped to the Grails dispatcher servlet
• Create a new controller via:
“grails create-controller”
def dialog = { attrs, body -> • Or generate the controller based on an existing Domain Class:
mkp { “grails generate-controller”
div('class':'dialog') {
body()
}
}
}
def show = {
[ advertisement : Advertisement.get( params.id ) ]
}
...
Controller Controller
• You can also use dynamic scaffolding: • Grails Services can be used to encapsulate business logic
• Services may be injected into Controllers
class BookController {
def scaffold = Book
• “grails create-service” creates a new service in grails-app/services
}
• list/show/edit/delete/create/save/
update “dynamically” available
• You can still override actions
• “grails generate-all” creates all views and
all controller actions for a domain class
(C) Hochschule für Technik (C) Hochschule für Technik
15/01/2008 Fachhochschule Nordwestschweiz
35 15/01/2008 Fachhochschule Nordwestschweiz
36
Controller About Scaffolding...
• Example Service • Use it to get up to speed, have something to show and work on
• You will not scaffold your complete web application
class CountryService {
def String sayHello(String name) { • Learn from the generated code, tweak it to fit your individual needs
return "hello ${name}"
}
}
• You can put additional Spring configuration in the /spring directory • You can specify your own HBM files for your domain classes. (You
and use it to configure your beans can even use your existing Java classes if you like)
• Automatic DI is available even for these beans (not just Services) • Working with legacy database schemas is getting really simple
• Easily integrate existing (Java) functionality that was configured • Still use all benefits like dynamic finder methods
with Spring
• Grails creates an Eclipse Project file automatically, just run • Grails supports AJAX with different AJAX toolkits, currently
File > Import > Existing Project Prototype, Dojo and Yahoo
• You'll have to tweak some file names if you use the development • Special AJAX tags can be used for asynchronous calls and form
snapshot versions submission
• Be sure to install the Groovy Plugin:
groovy.codehaus.org/Eclipse+Plugin
<div id="message"></div>
<g:remoteLink action="delete" id="1" update="message">Delete
Book</g:remoteLink>
• On the server side, Grails supports AJAX via the render() Method, • Grails makes using Quartz even easier, just create this (in grails-
which makes AJAX responses really easy: app/jobs):
class MyJob {
def time = {
def cronExpression = "0,15,30,45 * * * * ?" //every 15 seconds
render(contentType:'text/xml') {
def execute(){
time(new Date())
println "Running job!"
}
}
}
}
• Render() supports MarkupBuilders for XML,
HTML, JSON, OpenRico
(C) Hochschule für Technik (C) Hochschule für Technik
15/01/2008 Fachhochschule Nordwestschweiz
45 15/01/2008 Fachhochschule Nordwestschweiz
46
• Both unit and functional testing supported • Download, Install, create your first app with the Quickstart Guide
• Functional Testing uses Canoo Webtest http://www.grails.org/Quick+Start
– “grails test-app” runs the unit tests • Check out the Tutorials Section
– “grails run-webtest” http://www.grails.org/Tutorials
• Generate a webtest with
– “grails generate-webtest”