300 CHAPTER 7: Rapid Web Development with Grails
and it includes a web container, database, build system, and test harness that exploit the dynamism
of the Groovy language.
Figure 7-1. Grails platform
Grails provides best practices, such as convention over configuration, and unit testing using
frameworks such as Spring, Hibernate, and SiteMesh, to name a few. This section will highlight
some of the important best practices.
Convention over Configuration
Rather than configuration, Grails gives precedence to convention. Convention over configuration, in
simple terms, means writing configuration code only when you deviate from the convention. These
ingenious conventions correspond to the directory structure; Grails brings into play the name and
location of the files instead of relying on explicit configuration via the wiring of XML configuration files.
This means if you create a class following the Grails conventions, Grails will wire it into Spring or treat
it as a Hibernate entity. If you create a new domain class called Book, Grails will automatically create
a table called book in the database. By using the convention-over-configuration paradigm, Grails
can envisage a component from its name and its location in the directory structure. One immediate
consequence of this, other than speeding up application development, is that you have to configure a
particular aspect of a component only when that configuration deviates from the standard.
Scaffolding
The Grails scaffolding generates an application’s CRUD functionality from the domain classes, at
either runtime or development time. The generated application consists of the controller and GSP
views associated with the domain class. The scaffolding also generates the database schema,
including tables for each of the domain classes.
Object-Relational Mapping
Grails includes a powerful object-relational mapping (ORM) framework called Grails Object Relational
Mapping (GORM). Like most ORM frameworks, GORM maps objects to relational databases; but
unlike other ORM frameworks, GORM is based on a dynamic language. Therefore GORM can inject
the CRUD methods right into the class without having to implement them or inherit them from
persistent superclasses.