Grails is an open source web application framework that uses the Apache 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.
Grails was developed to address a number of goals:
Grails has three properties that differentiate it from traditional Java web frameworks:
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. However, in recent years, it has become apparent that although XML is great for configuration, it can be tedious to set up an environment. This may reduce productivity as developers spend time understanding and maintaining framework configuration as the application grows. Adding or changing functionality in applications that use XML configuration adds an extra step to the change process, which slows down productivity and may diminish the agility of the entire process.
Grails removes 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 that ends with <code>Controller</code> (for example <code>BookController</code>) is considered a web controller.
When using traditional Java web toolkits, it's up to developers to assemble development units, which can be tedious. Grails provides a development environment that 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.
Grails features dynamic methods on several classes through mixins. A mixin is a method that is added to a class dynamically, as if the functionality had been compiled into 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
The Grails web framework has been designed according to the MVC paradigm.
Grails uses controllers to implement the behavior of web pages. Below is an example of a controller:
The controller above has a <code>list</code> action which returns a model containing all books in the database. To create this controller the <code>grails</code> command is used, as shown below:
grails create-controller Book
This command creates a class in the <code>grails-app/controller</code> directory of the Grails project. Creating the controller class is sufficient to have it recognized by Grails. The <code>list</code> action maps to <code><nowiki>http://localhost:8080/book/list</nowiki></code> in development mode.
Grails supports JSP 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 <code>grails-app/views/book/list.gsp</code> of the Grails project. This location maps to the <code>BookController</code> and <code>list</code> action. Placing the file in this location is sufficient to have it recognized by Grails.
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 <code>formatDate</code> tag library above formats a object to a . This tag library should be added to the <code>grails-app/taglib/ApplicationTagLib.groovy</code> file or a file ending with <code>TagLib.groovy</code> in the <code>grails-app/taglib</code> directory.
Below is a snippet from a GSP file which uses the <code>formatDate</code> tag library:
<pre> <g:formatDate format="yyyyMMdd" date="${myDate}"/> </pre>
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
The domain model in Grails is GORM (Grails Object Relational Mapping). Domain classes are saved in the <code>grails-app/domain</code> directory and can be created using the <code>grails</code> command as shown below:
grails create-domain-class Book
This command requests the domain class name and creates the appropriate file. Below the code of the <code>Book</code> 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. The id property is used as the primary key of the corresponding table. The version property is used for optimistic locking.
When a class is defined as a domain class, that is, one managed by GORM, methods are dynamically added to aid in persisting the class's instances. http://grails.org/DomainClass+Dynamic+Methods
The <code>save()</code> method saves an object to the database:
The <code>delete()</code> method deletes an object from the database:
The <code>refresh()</code> method refreshes the state of an object from the database:
The <code>ident()</code> method retrieves the object's identity assigned from the database:
The <code>count()</code> method returns the number of records in the database for a given class:
The <code>exists()</code> method returns true if an object exists in the database with a given identifier:
The <code>find()</code> method returns the first object from the database based on an object query statement:
Note that the query syntax is Hibernate HQL.
The <code>findAll()</code> method returns all objects existing in the database:
The <code>findAll()</code> method can also take an object query statement for returning a list of objects:
The <code>findBy*()</code> methods return the first object from the database which matches a specific pattern:
Also:
The <code>findAllBy*()</code> methods return a list of objects from the database which match a specific pattern:
The <code>findWhere*()</code> methods return the first object from the database which matches a set of named parameters:
Grails supports scaffolding 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 <code><nowiki>http://localhost:8080/book</nowiki></code>. This works because the BookController follows the same naming convention as the Book domain class. To scaffold a specific domain class we could reference the class directly in the scaffold property:
Currently Grails does not provide scaffolding for associations.
The persistence mechanism in GORM is implemented via Hibernate. As such, legacy databases may be mapped to GORM classes using standard Hibernate mapping files.
The target audience for Grails is:
Grails is built on top of and is part of the Java platform meaning that it is very easy to integrate with Java libraries, frameworks and existing code bases. Grails offers transparent integration of classes which are mapped with the Hibernate 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.
Grails also makes use of the Spring Inversion of Control Framework; Grails is actually a Spring MVC application under the hood. The Spring framework can be used to provision additional Spring beans and introduce them into the context of the application. The SiteMesh framework is used to manage the presentation layer, simplifying the development of pages via a robust templating system.
Grails applications are packaged as war artifacts that can be deployed to any servlet container or Java EE application servers.