JavaData & JavaRAD Application Development with Grails

RAD Application Development with Grails content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Grails envisioned easing Web application development to the next level of innovation. Web development inherently is a byproduct of varied technologies working together in harmony. It’s a daunting task behind the scenes, if you haven’t realized. For example, a browser knows HTML best, but business modules speak, say, Java. A database is too procedural to comply with object requests. The scenario requires wiring up complex configurations. Here, XML and frameworks play a crucial role in easing the tension. But still, it cannot escape being complex with each level of abstraction.  What sets Grails apart from other frameworks is that it allayed the fragmented approach and wrapped them in a layer of abstraction via the Groovy language. So, technically it’s a stack of framework or meta-framework. The article explores some of its features that make the Grails platform suitable for a rapid Web application development.

Grails Features

  • This is a request-based MVC framework that encompasses several idea into the ecosystem, such as Groovy as a language, Spring MVC as a framework, an ORM called Grails’ object relational mapping (GORM), Scaffolding scripts, and, last but not least, CoC (Convention over configuration).
  • Groovy is a general purpose language that is used to bind the assorted frameworks and helps in interfacing with a common language. Groovy is very similar to Java. It’s derived from Java 5 grammars, so apart from same underlying API used by both the language, any syntactically correct Java code is also valid Groovy code.
  • Grails is powered by some of the mature open source technologies such as Spring, Tomcat, Hibernate, Site Mesh, and H2.
  • GORM basically provides a layer of abstraction above SQL and serves in mapping objects to relational database tables.

Figure 1: A minimal Grails stack

Convention over Configurations

To put the paradigm simply: While following a conventional approach of application development, one need not worry about the XML configuration because Grails would implicitly do the job except for those unconventional cases. According to the Groovy class created, Grails would wire it to either a Spring or Hibernate entity. For example, if a domain class is created with the name Employee, Grails automatically creates a table named Employee into the database. This is a unique ability to envisage the type of component by its name and location in the directory structure. This speeds up development because developers no longer need to configure different aspects of a component as long as they following standard procedure. However, if it deviates from the rule, which may not occur very often, a direct intervention to the configuration file is required.

Scaffolding in Grails

The Scaffolding feature of Grails helps in auto generating CRUD functionality in an application. It takes help of the domain classes and regenerates database schema and tables. Regeneration occurs either statically or dynamically, and consists of controller and GSP views along with the domain classes.

Setting up Grails

  • Java Development Kit (download and install).
  • Set JAVA_HOME pointing to the location of the installation
  • Download Grails; extract the ZIP file in any location.
  • Set GRAILS_HOME environment variable to the path where Grails is extracted.
  • On Windows: My Computer –> Advanced –> Environment Variables
  • On Linux: export GRAILS_HOME=/path/where/grails/extracted
  • Set the bin directory to the PATH environment variable.
  • On Windows: modify PATH in My Computer –> Advanced –> Environment Variables
  • On Linux: export PATH=”$PATH:$GRAILS_HOME/bin”
  • Optionally, Eclipse with Grails (GGTS) support can be installed; this simplifies the preceding setup steps and configures it on behalf of the user automatically. But, here we shall use a command line approach rather than having a smooth sail with an IDE.

Create a Simple Grails Application

Creating a simple application will give an idea of how things work in Grails. It’s a bare bone Web application just to display a message in the browser.

  1. Open terminal/command and check that the Grails and Java installation and environment setup works with the following command. This will show Grails and the JDK version, respectively:
grails -version
javac -version
  1. First, we need to create a project workspace with the following command.
grails create-app mygrailsapp
  1. This will create the directory structure of the application project in the current location. Navigate to the project folder and change the directory with the following command:
cd mygrailsapp
  1. Now, Grails would download resources and dependencies, so the Internet should be on during this step.
  1. The MVC pattern is central to a Grails application. It separates concern for model, view, and controller neatly so that a minor change in a layer has a lower ripple effect. The controller, specifically, adds action to a page. That means it marshals the request/response mechanism and delegates it to views. Controller classes are written in the Groovy language and files created have the extension <filename>.groovy. The command to create the controller is:
    grails>create-controller greet

    This will create a greet.groovy file. This is basically a text file and can be opened in any editor or IDE to modify the content.

  1. Open the file, and then modify the content as follows:
package mygrailsapp
class GreetController{
   def index(){
      render "This is my first grails app"
  1. That’s it. Now, to run the application, type the following command:

    This will start the embedded server (that comes with Grails) on port 8080. The application URL will be: http://localhost:8080/mygrailsapp/. The resulting Web page will have a link to the controller in the Available Controllers list. Clicking it will display the message.

  1. Finish.


The power of Grails lies in providing a platform to re-use mature existing Java technologies under a single hood. In doing so, it also took up the responsibility of configuring requirements so that the developer focuses on business requirement rather than being tangled up in the XML files. Grails befits a development project that has standard requirements and “plays by the book.” Perhaps the greatest advantage of Grails also seems to be its greatest fault. Grails makes one oblivious of the underlying principle of functioning in view of minimizing configuration problems. This can have an unintended consequence when a non-standard (as understood by Grails) solution to a problem is solicited. Grails is not very supportive in handling such a situation that demands high flexibility. If you like the idea “taken care of” and seek to develop a quick solution to your Web problems, Grails is undoubtedly right for you. If you seek to have control over even the minutest detail, Grails is a strict no-no.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories