Developing First Grails Application

Grails - Introduction

Grails is a modern web development framework that mixes the features of some important frameworks like Spring (IoC, MVC, AOP) and Hibernate. The Grails uses a Groovy as a programming language that runs on JVM and seamlessly integrates with Java language. The goal of Grails is simplifying Java enterprise web application development.

The important feature of Grails is convention over configuration (Rails developers knows very well), means that your application auto-wires itself based on some common-sense naming schemes, instead of using XML configuration files. 

The scaffolding is an another important feature of Grails, that will automatically creates a basic CURD interface for your domain class.

Grails - Architecture


Installing and configuring Grails

1. Here you can download the binary distribution of the Grails. After downloading just unzip it.
2. Create the GRAILS_HOME environment variable.
3. Add $GRAILS_HOME/bin to the path.

Creating a new Grails application

The command "grails create-app app-name" is used for creating new Grails application. Once you execute this command, the grails automatically creates necessary files and folder structure for you. It will be more easy, If you are using any IDE like GGTS, STS, Eclipse, and IntelliJ. 

The general folder structure of the application is given below. Here I have given application name as "Grails-HelloWorld".


Creating a controller

The command “grails create-controller controller-name” is used for creating a new controller in Grails application. Let us create a controller called "User" by executing a command called "grails create-controller com.pointerunits.User".

Now we can see one new controller class in controllers directory and initial source code of the controller is given below. 
package com.pointerunits

class UserController {
    def index() { }
}

During controller creation, the grails automatically creates a new folder in views directory. The name of the folder is same as controller name. By default, Grails include the "index" action in every controller and by convention it is a default action for all controllers. So if you access a controller without any action, the controller automatically executes the index action.

The Grails automatically creates the unit test class for all controllers, services and domain classes. So we can find a new unit test class for user controller in test/unit directory.


Now let us render some static html message to the browser.

package com.pointerunits

class UserController {

    def index() { 
		render '

Welcome to Grails application development

' } }

Now just run the application by executing a command "grails run-app". If you want to change the port number of the server just include a -Dserver.port attribute like "grails -Dserver.port=8090 run-app".

Now you can access your application by using a url "http://localhost:8080/Grails-HelloWorld". Here "8080" indicate my server port number and "Grails-HelloWorld" indicate my application name.



The above one is default home page of the grails application. We can modify this one according our needs. Now we can access our controller by using a URL "http://localhost:8080/Grails-HelloWorld/user/index". In this URL, the user indicate the controller name and index indicate the action name of the controller that you want to execute. Here index action is not required, because by convention the default action for controller is index.



Creating a view

Instead of rendering some static html from controller, let us rendering some GSP (Groovy Server Page) view. In grails, you have to create a GSP page based on action name. As of now we have only one action called "Index", but now I am going to create one more action called "create". This is my modified user controller.

package com.pointerunits

class UserController {

    def index() { 
		render '

Welcome to Grails application development

' } def create() { } }

Now just use the URL "http://localhost:8080/Grails-HelloWorld/user/create" for accessing create action of the user controller.


By default, the above URL is trying to load a "create.gsp" page from "views/user" directory, but as of now we don't create any page like that. Here the name of the GSP page should be the name of the controller action. Let us create a "create.gsp" under "views/user" folder.


Now just access the same URL, you can see the result like this.



Creating a domain class

The command "grails create-domain-class class-name" is used for creating a new domain class in grails application. Let us create a "Person" domain class by executing a command "grails create-domain-class com.pointerunits.Person".
package com.pointerunits

class Person {
	String firstName
	String lastName
	String email
	String phoneNumber
	Integer age
	
    static constraints = {
    }
}

By default, Grails comes with HSQLDB and when new application created, by default three databases(devDb, testDB, prodDb) are configured in Datasource.groovy for three different environments (development, testing and production). In above domain class, we have included only some basic variables. Now the Grails automatically maps this domain class into Person table and every variables are mapped into corresponding column of the table. No more XML file needed like Hibernate. No worry, the Grails gives full freedom for customizing those behaviours.

Scaffolding

We know, scffolding is a feature that will create a basic CURD interface for your domain class automatically. Let us see how to do that.

First of all we need to create one more controller for Person domain class and need to include one line of code like "def scaffold = Person". The Grails will takes care other works for you. This is my source code of my Person controller.

package com.pointerunits

class PersonController {
	
	def scaffold = Person
	
    def index() { }
}

Now just load the URL "http://localhost:8080/Grails-HelloWorld/person/list", you will see basic CURD interface for your Person table.

1. List action - Displays the all person details as a table.


2. Create action - All us to create a new Person. Example "http://localhost:8080/Grails-HelloWorld/create".


3. Show action - Allow us view the information of particular record. Here we need to give index of the record as input through query string. Example "http://localhost:8080/Grails-HelloWorld/show/1". Here 1 indicates index of the record.


4. Delete action - Allow us to delete a particular record from database. It also require an index value of the record. Example "http://localhost:8080/Grails-HelloWorld/delete/1".

Conclusion

In this tutorial, we have created new grails application and created controllers, views, and domain classes. The scaffolding feature of grails is also demonstrated. 

Comments