An Introduction To Building
Groovy Web Applications
With Grails
Harshdeep Kaur
www.company.com
OVERVIEW
• Grails is a web application framework 
• Friendly for beginners to learn.
• Support a more agile development approach. 
• MVC framework based on Groovy/Java
• Heavily inspired on Rails framework
• But takes advantage of Java technology like
Spring and servlets
Company
LOGO
www.company.com
What is GrailsCompany
LOGO
 Grails is an open source web application framework
Built on the Java Platform
 Groovy as the primary language
Java can also be used and intermixed with Groovy
seamlessly
 Favour's Convention over Configuration
Similar in spirit to Ruby on Rails
 Grails having the following features
Rapid, Robust, Dynamic
 Powerful features
Integrated ORM / NoSQL support, a powerful view technology, plugins, and
Spring-powered dependency injection
 Domain-Specific Languages
Expressive Domain Specific Languages (DSLs) used for validation, querying,
markup rendering etc.
www.company.com
Rapid Development
As Grails is inspired by the RAILS principle, it
provides 52 commands to make your life easier:
grails create-app
grails create-domain-class
grails create-controller
grails install-plugin
grails war
www.company.com
Grails is Robust
Grails builds upon proven technology:
Groovy - open source dynamic language for the Java Virtual Machine
that offers a flexible Java-like syntax
Spring Framework - Dependency injection services
Hibernate - an Object Relational Mapping (ORM) Framework
SiteMesh - interface layout and theming facilities
+=
www.company.com
Why It Is Dynamic
• Changes made in Controllers and Views are instantly
visible - Domain Classes, Bootstrap and Configuration require a
recompile
www.company.com
Why grails
Grails aims to improve productivity over
conventional Java Frameworks by:
eliminating XML configuration - Convention over
Configuration. For example, a class name which ends with Controller (e.g.
BookController) is considered a web controller
Scaffolding: Scaffolding utilities provided by Grails is a great advantage
when building prototypes to validate the idea; or to build Admin interfaces.
ready to use development environment - Grails comes with
a complete development environment which includes a webserver and a
database
www.company.com
Model View Controller?
MVC is a software architectural pattern which dates
back to 1979 and is widely adopted. It separates:
Data - The model manages the behavior and data of the application
domain.
User Interface - The view renders the model into a form suitable for
interaction, typically a user interface element
Application Logic - The controller receives user input and initiates
a response by making calls on model objects.
www.company.com
Grails application built up
www.company.com
Domain
Classes
This will generate Domain Classes
and Unit Tests
This will generate Domain Classes
and Unit Tests
www.company.com
The Controller
This will generate Controller
Classes
www.company.com
The Views
grails generate-views [domain class]
www.company.com
Groovy Language
 Is an agile and dynamic language for the Java Virtual Machine
 Additional power features inspired by languages like Python, Ruby and
Smalltalk
 Everything is an object no primitive types.
 Optional Typing – If not explicitly specified assumed to be java.lang.Object
 Can be compiled to Java byte code
 Groovy classes are Java classes
 Lots of new languages features
GPath, GString, internal iterators, closures, Meta‐
Object Protocol (MOP), and many others
www.company.com
Groovy Syntax
Semicolons- use of semicolons ( ; ) in Groovy is completely optional .
Data type declaration-When creating an object, it’s type doesn’t have to be
defined explicitly. By using the def-keyword, Groovy will automatically detect what object
type has to be used
Groovy String- The Groovy String (Also called “GString”) allows for any type of
logic to be integrated in the String definition. This can be done with the dollar symbol
( $ ) and (optional) braces ( { } ). The difference between a String and a GString is
automatically recognized by Groovy.
www.company.com
CollectionsGroovy Syntax
Collections- Groovy acknowledges three different types of collections
Lists- are no different from the ones used in Java.
Maps- are no different from the ones used in Java.
Ranges- a range defines a start and an end point.
Loops- Because of Groovy’s origin in Java, it natively supports both the
for- and while-loop.
Groovy’s for-each loops though, have a slight difference in syntax
Groovy has some special capabilities for collections- each method
executes a given closure for each of the item the list.
www.company.com
Domain classes are central to a
Grails application
 Located in grails app/domain‐
 All attributes persistent unless
marked in the transients property
No getters and setters necessary
Validators: specified in
constraints
GORM mapping: customized in
the mapping property
Domain Model Object
www.company.com
GROM
Grails Object Relational Mapping
Leverages Hibernate
Automatically maps domain objects to database
Provides query and update facilities
Finding objects in the persistence store (findBy…
methods).
Persistence lifecycle methods (save,update,delete).
Criteria and HQL query facilities.
Declare properties for your domain objects
The id or version properties are injected
automatically.
All properties are not null/required by default
www.company.com
Grails
Object
Relational
Mapping
Grails Object Relational
Mapping
www.company.com
The Controllers
Handles HTTP requests and
determines rendering
responsibility
Many objects are available to
your controller closures log,
params , request , response
session servlet-Context ,
flash
www.company.com
The Controllers
Dynamic Scaffolding
Enable Scaffolding
www.company.com
The Service Layer
Allows for centralization of application
behavior
 Necessary component in any non trivial Grails‐
application
Transactional by default, set through the
transactional property
www.company.com
Errors and Validation
Built upon Spring’s validation framework
org.springframework.validation package
Domain objects have validation baked in
save() and validate() methods trigger validation
Validation errors populate errors property on
domain object Errors
Interrogate the domain object instance hasErrors() method and error property
def user = new User(params)
if (user.validate())
{ // do something with user }
else {
user.errors.allErrors.each { println it } }
www.company.com
Database Connectivity
 JDBC is used for communicating with databases
 HSQLDB: default data source
DataSource.groovy
Configuration of Data Sources for all your environments
www.company.com
Views And Groovy Server Pages
Similar to JSPs
Utilizes GPath and GStrings and other cool Groovy language features
Automatic discovery of Grails taglibs
Support for templates
Small snippets of view code that other views canuse
Useful for AJAX responses
www.company.com
The Views
’’ff’’ff
Form tag Go to mapping register inGo to mapping register in
customer controllercustomer controller
Go to mapping register inGo to mapping register in
customer controllercustomer controller
You can create your own tags by creating
new tag libraries
You can create your own tags by creating
new tag libraries
www.company.com
Demo of grails/groovy
Set configuration for the application through:-
1.BuildConfig.groovy
2.DataSource.groovy
Uncomment this
Add
Dependencies
and plugins
here
www.company.com
Create
Create domain class
Create Controller class
name of controller will be the same as domain class to implement COC
Demo of grails/groovy
www.company.com
Demo of grails/groovy
www.company.com
www.company.com

intoduction to Grails Framework

  • 1.
    An Introduction ToBuilding Groovy Web Applications With Grails Harshdeep Kaur
  • 2.
    www.company.com OVERVIEW • Grails isa web application framework  • Friendly for beginners to learn. • Support a more agile development approach.  • MVC framework based on Groovy/Java • Heavily inspired on Rails framework • But takes advantage of Java technology like Spring and servlets Company LOGO
  • 3.
    www.company.com What is GrailsCompany LOGO Grails is an open source web application framework Built on the Java Platform  Groovy as the primary language Java can also be used and intermixed with Groovy seamlessly  Favour's Convention over Configuration Similar in spirit to Ruby on Rails  Grails having the following features Rapid, Robust, Dynamic  Powerful features Integrated ORM / NoSQL support, a powerful view technology, plugins, and Spring-powered dependency injection  Domain-Specific Languages Expressive Domain Specific Languages (DSLs) used for validation, querying, markup rendering etc.
  • 4.
    www.company.com Rapid Development As Grailsis inspired by the RAILS principle, it provides 52 commands to make your life easier: grails create-app grails create-domain-class grails create-controller grails install-plugin grails war
  • 5.
    www.company.com Grails is Robust Grailsbuilds upon proven technology: Groovy - open source dynamic language for the Java Virtual Machine that offers a flexible Java-like syntax Spring Framework - Dependency injection services Hibernate - an Object Relational Mapping (ORM) Framework SiteMesh - interface layout and theming facilities +=
  • 6.
    www.company.com Why It IsDynamic • Changes made in Controllers and Views are instantly visible - Domain Classes, Bootstrap and Configuration require a recompile
  • 7.
    www.company.com Why grails Grails aimsto improve productivity over conventional Java Frameworks by: eliminating XML configuration - Convention over Configuration. For example, a class name which ends with Controller (e.g. BookController) is considered a web controller Scaffolding: Scaffolding utilities provided by Grails is a great advantage when building prototypes to validate the idea; or to build Admin interfaces. ready to use development environment - Grails comes with a complete development environment which includes a webserver and a database
  • 8.
    www.company.com Model View Controller? MVCis a software architectural pattern which dates back to 1979 and is widely adopted. It separates: Data - The model manages the behavior and data of the application domain. User Interface - The view renders the model into a form suitable for interaction, typically a user interface element Application Logic - The controller receives user input and initiates a response by making calls on model objects.
  • 9.
  • 10.
    www.company.com Domain Classes This will generateDomain Classes and Unit Tests This will generate Domain Classes and Unit Tests
  • 11.
    www.company.com The Controller This willgenerate Controller Classes
  • 12.
  • 13.
    www.company.com Groovy Language  Isan agile and dynamic language for the Java Virtual Machine  Additional power features inspired by languages like Python, Ruby and Smalltalk  Everything is an object no primitive types.  Optional Typing – If not explicitly specified assumed to be java.lang.Object  Can be compiled to Java byte code  Groovy classes are Java classes  Lots of new languages features GPath, GString, internal iterators, closures, Meta‐ Object Protocol (MOP), and many others
  • 14.
    www.company.com Groovy Syntax Semicolons- useof semicolons ( ; ) in Groovy is completely optional . Data type declaration-When creating an object, it’s type doesn’t have to be defined explicitly. By using the def-keyword, Groovy will automatically detect what object type has to be used Groovy String- The Groovy String (Also called “GString”) allows for any type of logic to be integrated in the String definition. This can be done with the dollar symbol ( $ ) and (optional) braces ( { } ). The difference between a String and a GString is automatically recognized by Groovy.
  • 15.
    www.company.com CollectionsGroovy Syntax Collections- Groovyacknowledges three different types of collections Lists- are no different from the ones used in Java. Maps- are no different from the ones used in Java. Ranges- a range defines a start and an end point. Loops- Because of Groovy’s origin in Java, it natively supports both the for- and while-loop. Groovy’s for-each loops though, have a slight difference in syntax Groovy has some special capabilities for collections- each method executes a given closure for each of the item the list.
  • 16.
    www.company.com Domain classes arecentral to a Grails application  Located in grails app/domain‐  All attributes persistent unless marked in the transients property No getters and setters necessary Validators: specified in constraints GORM mapping: customized in the mapping property Domain Model Object
  • 17.
    www.company.com GROM Grails Object RelationalMapping Leverages Hibernate Automatically maps domain objects to database Provides query and update facilities Finding objects in the persistence store (findBy… methods). Persistence lifecycle methods (save,update,delete). Criteria and HQL query facilities. Declare properties for your domain objects The id or version properties are injected automatically. All properties are not null/required by default
  • 18.
  • 19.
    www.company.com The Controllers Handles HTTPrequests and determines rendering responsibility Many objects are available to your controller closures log, params , request , response session servlet-Context , flash
  • 20.
  • 21.
    www.company.com The Service Layer Allowsfor centralization of application behavior  Necessary component in any non trivial Grails‐ application Transactional by default, set through the transactional property
  • 22.
    www.company.com Errors and Validation Builtupon Spring’s validation framework org.springframework.validation package Domain objects have validation baked in save() and validate() methods trigger validation Validation errors populate errors property on domain object Errors Interrogate the domain object instance hasErrors() method and error property def user = new User(params) if (user.validate()) { // do something with user } else { user.errors.allErrors.each { println it } }
  • 23.
    www.company.com Database Connectivity  JDBCis used for communicating with databases  HSQLDB: default data source DataSource.groovy Configuration of Data Sources for all your environments
  • 24.
    www.company.com Views And GroovyServer Pages Similar to JSPs Utilizes GPath and GStrings and other cool Groovy language features Automatic discovery of Grails taglibs Support for templates Small snippets of view code that other views canuse Useful for AJAX responses
  • 25.
    www.company.com The Views ’’ff’’ff Form tagGo to mapping register inGo to mapping register in customer controllercustomer controller Go to mapping register inGo to mapping register in customer controllercustomer controller You can create your own tags by creating new tag libraries You can create your own tags by creating new tag libraries
  • 26.
    www.company.com Demo of grails/groovy Setconfiguration for the application through:- 1.BuildConfig.groovy 2.DataSource.groovy Uncomment this Add Dependencies and plugins here
  • 27.
    www.company.com Create Create domain class CreateController class name of controller will be the same as domain class to implement COC Demo of grails/groovy
  • 28.
  • 29.
  • 30.