Grails Guide

Tutorials And Examples on Grails

Grails Tutorial - Introduction to the Grails Web Framework

Grails is an open source web application framework that uses the Groovy programming language and runs on the Java platform. It is a high productivity framework that utilize well-known software engineering patterns, such as convention over configuration and don't repeat yourself.

Grails is easy to learn and beginner friendly

Grails is a simplified version of the many available Java web application frameworks. It is a full stack framework for database driven web applications and it supports features such as: object relational mapping, model-view-controller, internationalization, transaction management, and others.
Writing Java-based web applications usually requires a steep learning curve because of the need to study numerous concepts and technologies. Grails addressed this by embracing modern software engineering principles to dramatically reduce the complexity of implementation. Developers can usually create something out from Grails within the first hour of trying it.

Core Principles

One of the main focus of Grails is to simplify coding - making developers happy and productive.
  • Don't Repeat Yourself (DRY)
    This principle is centered around the concept of reducing repetition of all kinds of information. This is by having a singular representation for everything in your application. The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system". Grails implements the DRY principle to lower the amount of maintenance needed and to decrease code complexity.
  • Convention over Configuration (CoC)
    As implied by the name, this is a principle that promotes following a convention rather than maintaining numerous configuration in an application. This just simply mean that the framework has default assumptions on the behavior of the application, so that the only time to write a configuration is when the desired behavior deviates from the standard convention.
    Grails enforces standard naming conventions to support this principle. This makes the framework to have minimal configuration without sacrificing flexibility.
  • Model–View–Controller (MVC)
    MVC pattern is a design pattern that encourages separation of concerns, such that:
    • Model – handles data representation and business operations. Classes that represents the data model and business logic collectively represents the Model.
    • View – handles how data is seen by the user. For example, data can be viewed as table listing, or a graph.
    • Controller – handle how a user interacts to the system. It receives events from the user, invoke the proper business logic, and then return the appropriate view to match the user action.
  • Object-relational mapping (ORM)
    ORM frameworks enables a developer to manipulate objects instead of working with a relational database directly. The ORM handles the conversion from the object paradigm to the relational database, and vice versa.

    For example, instead of writing a code to execute the ff.SQL:

    select * from person where id = 1000 ;
    insert into person (first_name, last_name) values ( 'John', 'Doe' );

    In ORM, your code will look like:

    Person jane = Jane.get ( 1000);
    Person john = new Person();

Grails is built on top of the Java platform

One of the many attraction of Grails is that it is based on the Java platform.
  • Grails is robust
    Java has been trusted for so many years for critical and complex enterprise systems, due to the robustness and stability. Grails inherits this advantage.
  • Grails can run on any Java application server
    Many organizations has invested on software and expertise on specific Java application server. They could easily adapt to Grails as it could be treated as any other normal Java web application.
  • Grails can leverage on the many technologies available in the Java ecosystem
    Grails does not need to reinvent the wheel. Since Grails is based on Java, numerous technologies and open source libraries can be utilized and integrated into a Grails application.

Groovy languange

Grails uses the Groovy language to make the framework even more powerful and easy to use:
  • Groovy is a superset of the Java language. This makes any valid Java code a valid Groovy code also.
  • Groovy compiles to bytecode and can run on the JVM
  • Groovy supports modern programming features that are intuitive and easy to learn:
    • dynamic typing where type declaration is optional.
    • Closures
    • native syntax for lists, maps and regular expressions
    • safe xpath natigation
    • and many more shortcuts to common operations
  • Groovy syntax is compact and easy to read and maintain. This also increases developer productivity.
  • Groovy can use any available Java libraries
  • Supports unit testing and mocking

Hello World

Although many concepts are used in Grails, it is still easy to grasp and very intuitive. Here is a quick walk through on making a Hello World Program.

Create a Project
To create a new project, just invoke the "create-app" command from the console:

$ grails create-app sample

This will create a project named "sample", together with base skeleton code.
Create a Controller
Create a controller named GreetingController.groovy under grails-app/controller folder, with the following code:
class GreetingController {
	def index() { 
		render "Hello World"

Run the application and accessing it

Grails has a built in web server for running it in development mode. Run the following command on a console:

$ grails run-app

Access the URL: http://localhost:8080/sample/greetings and the page with the text "Hello World" will appear.
Tags: coc, dry, mvc, orm