Skip to main content

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

Popular posts from this blog

Getting key/value pair from JSON object and getting variable name and value from JavaScript object.

 Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.    function getKeyValueFromJSON() {     var jsonObj =  {a:10,b:20,c:30,d:50} ;     for ( var key in jsonObj) {       alert( "Key: " + key + " value: " + jsonObj[key]);     }  }  In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.  Finally I got the alert like this,    Key: a value:10    Key: b value:20    Key...

Simple Login Application Using Spring MVC and Hibernate – Part 1

 I hope developers working in web application development might hear about MVC architecture. Almost all technologies provide support for MVC based web application development, but the success is based on many factors like reusable of the code, maintenance of the code, future adaption of the code, etc..,  The success of the Spring MVC is “ Open for extension and closed for modification ” principle. Using Spring MVC the developers can easily develop MVC based web application. We don’t need any steep learning curve at the same time we need to know the basics of spring framework and MVC architecture. The Spring MVC consists of following important components. 1. Dispatcher servlet 2. Controller 3. View Resolver 4. Model Spring MVC - Overview  The overall architecture of Spring MVC is shown here.  1. When “Dispatcher Servlet” gets any request from client, it finds the corresponding mapped controller for the request and just dispatches the request to the c...

Simple Login Application Using Spring MVC and Hibernate – Part 2

 I hope you have visited my part1 of my tutorial . Let’s see the steps for integrating hibernate framework into Spring MVC. Here I have used MySQL database. I have created one database called “ springmvc ” and created one table called “ user ” with userid, username, password fields.  I have inserted some records into table like this. Step 1: Creating User POJO class.  We need to create a User POJO class for mapping user table. Ok let’s create it. Step 2: Creating hibernate mapping xml file for user class.  In hibernate we need to create hibernate mapping xml file for all domain class for mapping into corresponding database table. Instead of creating xml file you can use annotation for mapping domain class into database table. This is my mapping xml document created for mapping our user domain class into user database table. Step 3: Creating authenticate service class.  The method “verifyUserNameAndPassword” pres...