Web Development The Grails Framework
Web Development
The Grails Framework
Grails
Grails is a full-stack web application framework
built on top of such tried and true open source
frameworks as:
Spring, Hibernate, Ant, JUnit, SiteMesh and the
Groovy language.
Full-stack means the framework provides components and
artifacts for all tiers of a web application.
Grails is a good example of software reuse
Full-stack vs. extensible framework
Full-stack means all the components are pre-
integrated into the framework!
- Less flexibility
+ Extremely easy configuration process
On the other hand, an extensible framework lets
you decide on individual components that
would work together in you framework
Apache Struts: http://struts.apache.org/
Technical Info
1. First release: 0.1 on March 29, 2006
2. Stable release: 2.2.1 on February 25, 2013
3. Official web site : http://www.grails.org
4. License: Apache License 2.0
5. Programming language used: Groovy
6. Platform: Cross- platform (JVM)
7. Company: VMware, Inc.
Five Strong Pillars
1. Spring for workflows and dependency
injection
2. Hibernate to manage persistence (ORM)
3. SiteMesh for composition of views
4. Ant for managing the development process
5. Groovy, a dynamic language for the JVM
Grails makes it easy to use these powerful tools
Main Features
• Coding by convention paradigm (also known as “Convention over configuration”)
– Things behave as expected without having to write configuration files
• DRY: Don't repeat yourself!
– “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”(*)
– So, reduce repetition of information of all kinds
(*) The Pragmatic Programmer by Andrew Hunt and David Thomas, 1999.
Main Features (2)
• The MVC pattern: the components of a system
should be organized in three layers, according
to their mission: DATA layer, PRESENTATION
layer and CONTROL layer.
– When working in Grails, we generated
components in each layer and is the environment
that is responsible for connecting with each other
and ensure smooth operation.
The MVC pattern
• Data layer
contains components that represent and manage
data handled by the application
• Presentation layer
contains the components that are responsible for
showing the user the current state of the data
model, and present the different actions available
The MVC pattern (2)
• Control layer
Contains the components that receive user commands,
manage the application of business logic on the data
model, and determine which view must be shown.
In Grails, normally the business logic is
implemented in a fourth layer:
• Service layer
Contains the components responsible for implementing
the business logic of our application.
How it works
Available at: http://www.youtube.com/watch?feature=player_embedded&v=XrDXqoomws4
The Entire Environment
The Entire Environment (2)
The Entire Environment (3)
Basic Grails Requirements
• A Java Development Kit (JDK) installed version
1.6 or above.
java –version
• A valid JAVA_HOME environment variable
pointing to the location of this installation
For example: C:\Program Files\Java\jdk1.7.0
Installing Grails
1. Download the Grails archive from
http://grails.org/download and extract it to a
local directory.
For example: C:\grails\grails-2.2.1
2. Set a GRAILS_HOME environment variable to
that directory
For example: C:\grails\grails-2.2.1\
Installing Grails (2)
3. Add GRAILS_HOME/bin to your path variable
– For example: C:\grails\grails-2.2.1\bin\
4. For checking that the installation process
was successful open the console and type:
grails –version or grails help
You should receive an appropriate response in any
case
Installation Video Tutorials
• Installing Grails on Windowshttp://www.grailsexample.net/installing-a-grails-development-environment-on-windows/
• Installing Grails on Linuxhttp://www.grailsexample.net/installing-a-grails-development-environment-on-linux/
Creating an Application: create-app
• The grails command is the most important
one:
grails [command-name]
• To create a grails application just use the
create-app command:
grails create-app AutoMart
This will create a new directory (AutoMart) inside
the current one that contains the project.
Folders Organization
Grails automatically organizes
source code artifacts
according to the MVC pattern
Placing source files in certain
directories and naming them in
certain ways are examples of the
many conventions in Grails
Path: AutoMart\grails-app
Creating an Application:
create-domain-class
• Change to AutoMart directory and type:
grails create-domain-class Car
• After a moment this command will finish and you will get:
Created file grails-app/domain/automart/Car.groovyCreated file test/unit/automart/CarTests.groovy
Create-app and create-domain-class are Grails scripts. Use grails help to see which other scripts are provided by grails
Creating an Application:
editing a domain class
Just add these
attributes to the Car
class
Creating an Application: generate-all
• To have a complete working web application,
with pages for creating, displaying, editing and
listing Car instances you can use the generate-
all command:
grails generate-all Car
Running the Application
• You can launch the application using the
command run-app:
grails run-app
• Grails runs on port 8080 by default.
• So Navigate to
http://localhost:8080/AutoMart
and look around.
The Application is Running!
Grails Environment
• A web server
• A Java application server
• Database environment
Groovy/Grails Tool Suite
GGTS provides an Eclipse-powered development
environment for building Groovy and Grails applications
Groovy/Grails Tool Suite 3.2.0 release
They still recommend to use the Eclipse 3.8-based version
Where to get GGTS stuff
The search is over! www.grails.org
How it looks
Setting up a Grails Project
Available at: http://www.youtube.com/watch?feature=player_embedded&v=qcZ6FWmIuJc
The Online Course: Part 1
http://www.grailsexample.net/course-outline/
The Online Course: Part 2
http://www.grailsexample.net/course-outline/
An Online Course: Part 3
http://www.grailsexample.net/course-outline/
Books
• There is a list of books at the GRAILS web site: http://grails.org/Documentation
• Christopher M. Judd, Joseph Faisal Nusairat, and James Shingler. Beginning Groovy and Grails. Apress, 2008.
• Nacho Brito. Manual de desarrollo web con GRAILS. ediciones ágiles, 2009.
• Glen Smith and Peter Ledbrook. Grails in Action. Manning Publications Co., 2009.
Books (2)
• Graeme Rocher and Jeff Brown. The Definitive Guide to Grails, 2nd Edition. Apress, 2009.
• Scott Davis and Jason Rudolph. Getting Started with Grails, 2nd Edition. C4Media Inc., 2010
• Dave Klein. Getting Started with Grails. DZone Refcardz#60.
• Dierk König. Groovy. DZone Refcardz #15.
• Tim Downey, Guide to Web Development with Java. Springer-Verlag London Limited, 2012