Grails (Framework)
Encyclopedia
Grails is an open source
web application framework
which uses the Groovy programming language (which is in turn based on the Java platform). It is intended to be a high-productivity framework by following the "coding by convention"
paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the developer.
Grails was previously known as 'Groovy on Rails'; in March 2006 that name was dropped in response to a request by David Heinemeier Hansson
, founder of the Ruby on Rails
framework. Work began in July 2005, with the 0.1 release on March 29, 2006 and the 1.0 release announced on February 18, 2008.
G2One - The Groovy Grails Company - was acquired by SpringSource
in November, 2008, and it was later acquired by VMware
.
Grails was designed to be easy to learn, easy to develop applications and extensible. It attempts to offer the right balance between consistency and powerful features.
XML was initially welcomed as it provided greater consistency to configure applications. In recent years however it has become apparent that although XML is great for configuration it can be tedious to set up an environment. This may take away productivity as developers spend time understanding and maintaining framework configuration as the application grows. Adding or changing functionality in applications which use XML configuration adds an extra step to the change process next to writing application code which slows down productivity and may take away the agility of the entire process.
Grails takes away the need to add configuration in XML files. Instead the framework uses a set of rules or conventions while inspecting the code of Grails-based applications. For example, a class name which ends with
These dynamic methods allow developers to perform operations without having to implement interfaces or extend base classes. Grails provides dynamic methods based on the type of class. For example domain classes have methods to automate persistence operations like save, delete and find.
paradigm.
The controller above has a
containing all books in the database. To create this controller the
grails create-controller
This command requests the controller name and creates a class in the
and GSP. The example below shows a view written in GSP which lists the books in the model prepared by the controller above:
This view should be saved as
There is also a GSP tag reference available.
libraries including OpenRico
, Prototype
, Dojo
, YUI and ZKGrails. You can use existing tag libraries which create HTML with Ajax
code. You can also easily create your own tag libraries.
The
Below is a snippet from a GSP file which uses the
To use a dynamic tag library in a GSP no import tags have to be used. Dynamic tag libraries can also be used in JSP
files although this requires a little more work. http://grails.org/Dynamic+Tag+Libraries
grails create-domain-class
This command requests the domain class name and creates the appropriate file. Below the code of the
Creating this class is all that is required to have it managed for persistence by Grails. With Grails 0.3, GORM has been improved and e.g. adds the properties id and version itself to the domain class if they are not present.
The
The
The
The
The
Note that the query syntax is Hibernate HQL.
The
The
The
Also:
The
The
to support CRUD operations (Create, Read, Update, Delete). Any domain class can be scaffolded by creating a scaffolding controller as shown below:
By creating this class you can perform CRUD operations on
. As such, legacy databases may be mapped to GORM classes using standard Hibernate mapping files.
Grails provides support scripts to create and execute projects as follows:
grails create-app
This command will request the name of the project and creates a project directory with the same name. Further
The command
ORM
framework. This means existing applications which use Hibernate can use Grails without recompiling the code or reconfiguring the Hibernate classes while using the dynamic persistence methods discussed above. http://grails.org/Hibernate+Integration
One consequence of this is that scaffolding can be configured for Java classes mapped with Hibernate. Another consequence is that the capabilities of the Grails web framework are fully available for these classes and the applications which use them.
Open source
The term open source describes practices in production and development that promote access to the end product's source materials. Some consider open source a philosophy, others consider it a pragmatic methodology...
web application framework
Web application framework
A web application framework is a software framework that is designed to support the development of dynamic websites, web applications and web services. The framework aims to alleviate the overhead associated with common activities performed in Web development...
which uses the Groovy programming language (which is in turn based on the Java platform). It is intended to be a high-productivity framework by following the "coding by convention"
Convention over Configuration
Convention over configuration is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility....
paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the developer.
Grails was previously known as 'Groovy on Rails'; in March 2006 that name was dropped in response to a request by David Heinemeier Hansson
David Heinemeier Hansson
David Heinemeier Hansson is a Danish programmer and the creator of the popular Ruby on Rails web development framework and the Instiki wiki...
, founder of the Ruby on Rails
Ruby on Rails
Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language.-History:...
framework. Work began in July 2005, with the 0.1 release on March 29, 2006 and the 1.0 release announced on February 18, 2008.
G2One - The Groovy Grails Company - was acquired by SpringSource
SpringSource
SpringSource is a division of VMware that provides a suite of software products that accelerate the entire enterprise Java application life cycle of build, run, and manage. SpringSource employs open source leaders who created and drive innovation for Spring, a programming model for enterprise Java...
in November, 2008, and it was later acquired by VMware
VMware
VMware, Inc. is a company providing virtualization software founded in 1998 and based in Palo Alto, California, USA. The company was acquired by EMC Corporation in 2004, and operates as a separate software subsidiary ....
.
Overview
Grails was developed with a number of goals:- Provide a high-productivity web framework for the Java platform.
- Re-use proven Java technologies such as HibernateHibernate (Java)Hibernate is an object-relational mapping library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database...
and Spring under a simple, consistent interface - Offer a consistent framework which reduces confusion and is easy to learn.
- Offer documentation for those parts of the framework which matter for its users.
- Provide what users expect in areas which are often complex and inconsistent:
- Powerful and consistent persistencePersistence (computer science)Persistence in computer science refers to the characteristic of state that outlives the process that created it. Without this capability, state would only exist in RAM, and would be lost when this RAM loses power, such as a computer shutdown....
framework. - Powerful and easy-to-use view templates using GSP (Groovy Server Pages).
- Dynamic tag libraries to easily create web page components.
- Good AjaxAjax (programming)Ajax is a group of interrelated web development methods used on the client-side to create asynchronous web applications...
support which is easy to extend and customize.
- Powerful and consistent persistence
- Provide sample applications which demonstrate the power of the framework.
- Provide a complete development mode, including web server and automatic reload of resources.
Grails was designed to be easy to learn, easy to develop applications and extensible. It attempts to offer the right balance between consistency and powerful features.
High productivity
Grails has three properties which attempt to increase productivity when compared to traditional Java web frameworks:- No XMLXMLExtensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
configuration - Ready-to-use development environment
- Functionality available through mixins
No XML configuration
Creating web applications in Java traditionally involves configuring environments and frameworks at the start and during development. This configuration is very often externalized in XML files to ease configuration and avoid embedding configuration in application code.XML was initially welcomed as it provided greater consistency to configure applications. In recent years however it has become apparent that although XML is great for configuration it can be tedious to set up an environment. This may take away productivity as developers spend time understanding and maintaining framework configuration as the application grows. Adding or changing functionality in applications which use XML configuration adds an extra step to the change process next to writing application code which slows down productivity and may take away the agility of the entire process.
Grails takes away the need to add configuration in XML files. Instead the framework uses a set of rules or conventions while inspecting the code of Grails-based applications. For example, a class name which ends with
Controller
(for example BookController
) is considered a web controller.Ready-to-use development environment
When using traditional Java web toolkits, it's up to developers to assemble development units, which can be tedious. Grails comes with a complete development environment which includes a web server to get developers started right away. All required libraries are part of the Grails distribution and Grails prepares the Java web environment for deployment automatically.Functionality available through mixins
Grails features dynamic methods on several classes through mixins. A mixin is a method which is added to a class dynamically as if the functionality was compiled in the program.These dynamic methods allow developers to perform operations without having to implement interfaces or extend base classes. Grails provides dynamic methods based on the type of class. For example domain classes have methods to automate persistence operations like save, delete and find.
Web framework
The Grails web framework has been designed according to the MVCModel-view-controller
Model–view–controller is a software architecture, currently considered an architectural pattern used in software engineering. The pattern isolates "domain logic" from the user interface , permitting independent development, testing and maintenance of each .Model View Controller...
paradigm.
Controllers
Grails uses controllers to implement the behaviour of web pages. Below is an example of a controller:The controller above has a
list
action which returns a modelComputer simulation
A computer simulation, a computer model, or a computational model is a computer program, or network of computers, that attempts to simulate an abstract model of a particular system...
containing all books in the database. To create this controller the
grails
command is used, as shown below:grails create-controller
This command requests the controller name and creates a class in the
grails-app/controller
directory of the Grails project. Creating the controller class is sufficient to have it recognized by Grails. The list
action maps to http://localhost:8080/book/list
in development mode.Views
Grails supports JSPJavaServer Pages
JavaServer Pages is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML, or other document types...
and GSP. The example below shows a view written in GSP which lists the books in the model prepared by the controller above:
This view should be saved as
grails-app/views/book/list.gsp
of the Grails project. This location maps to the BookController
and list
action. Placing the file in this location is sufficient to have it recognized by Grails.There is also a GSP tag reference available.
Ajax support
Grails supports several AjaxAjax (programming)
Ajax is a group of interrelated web development methods used on the client-side to create asynchronous web applications...
libraries including OpenRico
Rico (Ajax)
Rico is an open source JavaScript library for developing rich Internet applications that use Ajax.Rico uses the Prototype Javascript Framework library and the JSON standard.- Features :...
, Prototype
Prototype Javascript Framework
The Prototype JavaScript Framework is a JavaScript framework created by Sam Stephenson in February 2005 as part of the foundation for Ajax support in Ruby on Rails. It is implemented as a single file of JavaScript code, usually named prototype.js...
, Dojo
Dojo Toolkit
Dojo Toolkit is an open source modular JavaScript library designed to ease the rapid development of cross-platform, JavaScript/Ajax-based applications and web sites. It was started by Alex Russell, Dylan Schiemann, David Schontzler, and others in 2004 and is dual-licensed under the modified BSD...
, YUI and ZKGrails. You can use existing tag libraries which create HTML with Ajax
Ajax (programming)
Ajax is a group of interrelated web development methods used on the client-side to create asynchronous web applications...
code. You can also easily create your own tag libraries.
Dynamic tag libraries
Grails provides a large number of tag libraries out of the box. However you can also create and reuse your own tag libraries easily:The
formatDate
tag library above formats a object to a . This tag library should be added to the grails-app/taglib/ApplicationTagLib.groovy
file or a file ending with TagLib.groovy
in the grails-app/taglib
directory.Below is a snippet from a GSP file which uses the
formatDate
tag library:To use a dynamic tag library in a GSP no import tags have to be used. Dynamic tag libraries can also be used in JSP
JavaServer Pages
JavaServer Pages is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML, or other document types...
files although this requires a little more work. http://grails.org/Dynamic+Tag+Libraries
Model
The domain model in Grails is persisted to the database using GORM (Grails Object Relational Mapping). Domain classes are saved in thegrails-app/domain
directory and can be created using the grails
command as shown below:grails create-domain-class
This command requests the domain class name and creates the appropriate file. Below the code of the
Book
class is shown:Creating this class is all that is required to have it managed for persistence by Grails. With Grails 0.3, GORM has been improved and e.g. adds the properties id and version itself to the domain class if they are not present.
Methods
The domain classes managed by GORM have 'magic' dynamic and static methods to perform persistence operations on these classes and its objects. http://grails.org/DomainClass+Dynamic+MethodsDynamic Instance Methods
Thesave
method saves an object to the database:The
delete
method deletes an object from the database:The
refresh
method refreshes the state of an object from the database:The
ident
method retrieves the object's identity assigned from the database:Dynamic Static (Class) methods
Thecount
method returns the number of records in the database for a given class:The
exists
method returns true if an object exists in the database with a given identifier:The
find
method returns the first object from the database based on an object query statement:Note that the query syntax is Hibernate HQL.
The
findAll
method returns all objects existing in the database:The
findAll
method can also take an object query statement for returning a list of objects:The
findBy*
methods return the first object from the database which matches a specific pattern:Also:
The
findAllBy*
methods return a list of objects from the database which match a specific pattern:The
findWhere*
methods return the first object from the database which matches a set of named parameters:Scaffolding
Grails supports scaffoldingScaffold (programming)
Scaffolding is a meta-programming method of building database-backend software applications. It is a technique supported by some model-view-controller frameworks, in which the programmer may write a specification that describes how the application database may be used...
to support CRUD operations (Create, Read, Update, Delete). Any domain class can be scaffolded by creating a scaffolding controller as shown below:
By creating this class you can perform CRUD operations on
http://localhost:8080/book
. Currently Grails does not provide scaffolding for associations.Legacy Database Models
The persistence mechanism in GORM is implemented via HibernateHibernate (Java)
Hibernate is an object-relational mapping library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database...
. As such, legacy databases may be mapped to GORM classes using standard Hibernate mapping files.
Creating a Grails project
Download and installation guidelines for Grails are available on the Grails web site.Grails provides support scripts to create and execute projects as follows:
- Grails will create a complete outline application in response to the command
grails create-app
This command will request the name of the project and creates a project directory with the same name. Further
grails
commands can be issued in this directory to create the classes and web pages of the application.The command
grails run-app
will run the application on a web server at the URL http://localhost:8080/
.Target audience
The target audience for Grails is:- Java developers who are looking for an integrated development environment to create web based applications.
- Developers without Java experience looking for a high-productivity environment to build web based applications.
Integration with the Java platform
Grails is built on top of and is part of the Java platform meaning it's very easy to integrate with Java libraries, frameworks and existing code bases. The most prominent feature Grails offers in this area is the transparent integration of classes which are mapped with the HibernateHibernate (Java)
Hibernate is an object-relational mapping library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database...
ORM
Object-relational mapping
Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...
framework. This means existing applications which use Hibernate can use Grails without recompiling the code or reconfiguring the Hibernate classes while using the dynamic persistence methods discussed above. http://grails.org/Hibernate+Integration
One consequence of this is that scaffolding can be configured for Java classes mapped with Hibernate. Another consequence is that the capabilities of the Grails web framework are fully available for these classes and the applications which use them.
Companies providing Grails development services
- SpringSource provides consulting services being the main developer.
- There is a list of companies doing grails development maintained on the home website.
See also
- Groovy (programming language)
- JRubyJRubyJRuby is a Java implementation of the Ruby programming language, being developed by the JRuby team. It is free software released under a three-way CPL/GPL/LGPL license...
- Griffon (framework)Griffon (framework)Griffon is an open source Rich Client Platform framework which uses the Groovy programming language . Griffon is intended to be a high-productivity framework by rewarding use of the Model-View-Controller paradigm, providing a stand-alone development environment and hiding much of the configuration...
- A Desktop framework inspired by Grails
External links
- Grails home page
- Groovy home page
- Introduction To Grails, by Jeff Brown, Principal Software Engineer, Object Computing, Inc. (OCI)
- Getting Started with Grails, Second Edition, by Scott Davis and Jason Rudolph, as part of the InfoQ Enterprise Development series of books - available as a free PDF download to registered users
- Grails Tutorial Gentle introduction to Grails (Broken link)
- Mastering Grails