HOME

TheInfoList



OR:

Grails is an
open source Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use and view the source code, design documents, or content of the product. The open source model is a decentrali ...
web application framework A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build and ...
that uses the
Apache Groovy Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming l ...
programming language (which is in turn based on the
Java platform Java is a set of computer software and specifications that provides a software platform for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms fr ...
). 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 David Heinemeier Hansson, also known by his initials DHH, is a Danish software engineer, programmer, writer, entrepreneur, and racing driver. He is the creator of Ruby on Rails, a web framework written in Ruby. He is also a partner and chief t ...
, founder of the
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pa ...
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.


Overview

Grails was developed to address a number of goals: * Provide a web framework for the Java platform. * Re-use existing Java technologies such as
Hibernate Hibernation is a state of minimal activity and metabolic reduction entered by some animal species. Hibernation is a seasonal heterothermy characterized by low body-temperature, slow breathing and heart-rate, and low metabolic rate. It is most ...
and Spring under a single interface * Offer a consistent development framework. * Offer documentation for key portions of the framework: ** The
Persistence Persistence or Persist may refer to: Math and computers * Image persistence, in LCD monitors * Persistence (computer science), the characteristic of data that outlives the execution of the program that created it * Persistence of a number, a ma ...
framework. ** Templates using GSP (Groovy Server Pages). ** Dynamic tag libraries for creating web page components. ** Customizable and extensible
Ajax Ajax may refer to: Greek mythology and tragedy * Ajax the Great, a Greek mythological hero, son of King Telamon and Periboea * Ajax the Lesser, a Greek mythological hero, son of Oileus, the king of Locris * Ajax (play), ''Ajax'' (play), by the an ...
support. * Provide sample applications that demonstrate the framework. * Provide a complete development mode, including a web server and automatic reload of resources.


Marketing

Grails has three properties that differentiate it from traditional Java web frameworks: * No
XML Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
configuration * Ready-to-use development environment * Functionality available through
mixin In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depe ...
s


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. 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 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 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.


Functionality available through mixins

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


Web framework

The Grails web framework has been designed according to the MVC paradigm.


Controllers

Grails uses controllers to implement the behavior of web pages. Below is an example of a controller: class BookController The controller above has a list action which returns a
model A model is an informative representation of an object, person, or system. The term originally denoted the plans of a building in late 16th-century English, and derived via French and Italian ultimately from Latin , . Models can be divided in ...
containing all books in the database. To create this controller the grails command is used, as shown below: grails create-controller Book This command 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 JSP and GSP. The example below shows a view written in GSP which lists the books in the model prepared by the controller above: Our books 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
GSP tag reference
available.


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: class ApplicationTagLib 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 files although this requires a little more work


Persistence


Model

The domain model in Grails is persisted to the database usin
GORM
(Grails Object Relational Mapping). Domain classes are saved in the grails-app/domain directory and can be created using the grails 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 Book class is shown: class Book 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.


Methods

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


Dynamic Instance Methods

The save() method saves an object to the database: def book = new Book(title:"The Da Vinci Code", author:Author.findByName("Dan Brown")) book.save() The delete() method deletes an object from the database: def book = Book.findByTitle("The Da Vinci Code") book.delete() The refresh() method refreshes the state of an object from the database: def book = Book.findByTitle("The Da Vinci Code") book.refresh() The ident() method retrieves the object's identity assigned from the database: def book = Book.findByTitle("The Da Vinci Code") def id = book.ident()


Dynamic Static (Class) methods

The count() method returns the number of records in the database for a given class: def bookCount = Book.count() The exists() method returns true if an object exists in the database with a given identifier: def bookExists = Book.exists(1) The find() method returns the first object from the database based on an object query statement: def book = Book.find("from Book b where b.title = ?", 'The Da Vinci Code' Note that the query syntax is Hibernate HQL. The findAll() method returns all objects existing in the database: def books = Book.findAll() The findAll() method can also take an object query statement for returning a list of objects: def books = Book.findAll("from Book") The findBy*() methods return the first object from the database which matches a specific pattern: def book = Book.findByTitle("The Da Vinci Code") Also: def book = Book.findByTitleLike("%Da Vinci%") The findAllBy*() methods return a list of objects from the database which match a specific pattern: def books = Book.findAllByTitleLike("The%") The findWhere*() methods return the first object from the database which matches a set of named parameters: def book = Book.findWhere(title:"The Da Vinci Code")


Scaffolding

Grails supports
scaffolding Scaffolding, also called scaffold or staging, is a temporary structure used to support a work crew and materials to aid in the construction, maintenance and repair of buildings, bridges and all other human-made structures. Scaffolds are widely u ...
to support
CRUD In computer programming, create, read, update, and delete (CRUD) are the four basic operations (actions) of persistent storage. CRUD is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing info ...
operations (Create, Read, Update, Delete). Any domain class can be scaffolded by creating a scaffolding controller as shown below: class BookController By creating this class you can perform CRUD operations on http://localhost:8080/book. 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: class SomeController Currently Grails does not provide scaffolding for associations.


Legacy database models

The persistence mechanism in GORM is implemented via
Hibernate Hibernation is a state of minimal activity and metabolic reduction entered by some animal species. Hibernation is a seasonal heterothermy characterized by low body-temperature, slow breathing and heart-rate, and low metabolic rate. It is most ...
. As such, legacy databases may be mapped to GORM classes using standar
Hibernate mapping
files.


Target audience

The target audience for Grails is: * Java or Groovy 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 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 Hibernation is a state of minimal activity and metabolic reduction entered by some animal species. Hibernation is a seasonal heterothermy characterized by low body-temperature, slow breathing and heart-rate, and low metabolic rate. It is most ...
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

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 A Jakarta Servlet, formerly Java Servlet is a Java software component that extends the capabilities of a server. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web applicat ...
container or
Java EE Jakarta EE, formerly Java Platform, Enterprise Edition (Java EE) and Java 2 Platform, Enterprise Edition (J2EE), is a set of specifications, extending Java SE with specifications for enterprise features such as distributed computing and web serv ...
application servers.


See also

*
Groovy (programming language) Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform. It is both a static and dynamic language with features similar to those of Python, Ruby, and Smalltalk. It can be used as both a programming l ...
*
JRuby JRuby is an implementation of the Ruby programming language atop the Java Virtual Machine, written largely in Java. It is free software released under a three-way EPL/ GPL/LGPL license. JRuby is tightly integrated with Java to allow the embeddi ...
* Griffon (framework), a desktop framework inspired by Grails *
Spring Roo Spring Roo is a Open-source software, open-source software tool that uses convention over configuration, convention-over-configuration principles to provide rapid application development of Java (programming language), Java-based enterprise softwa ...
* Comparison of web frameworks


References


Further reading

* * * * * * *


External links


Official website

Mastering Grails
An 18-part on-line tutorial provided by
IBM International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
(from 2008)


Books

* * {{DEFAULTSORT:Grails (Framework) Java platform Web frameworks