04 04
Introduction to Groovy-Grails Framework

Introduction to Groovy-Grails Framework

Introduction to Grails:

Grails is an open source platform that uses Groovy for developing Java Enterprise Applications, it’s used for handling everything from the view layer down into your persistence layer. Grails has leveraged the most popular open source technologies by adopting them in its platform inherently, Hibernate, Spring, SiteMesh and Tomcat. All of these frameworks, platforms and database engine are built inside the Grails platform without any need of doing additional configuration steps. This blog walks you through the writing simple hello world program using the Grails framework.

Introduction to Groovy:

Groovy is an Object Oriented Scripting Language which provides Dynamic, Easy-to-use and Integration capabilities to the Java Virutual Machine. It absorbs most of the syntax from Java and it is much powerful in terms of funtionalities which is manifiested in the form Closures, Dynamic Typing, Builders etc.

Grails Framework Architecture Layer :

Grails introducing another layer of abstraction via Groovy language, you will not be aware of all of these mentioned technologies, when it comes to deal with Grails platform, but you will have basic touch of those technologies. Look at the below picture to know the architecture of Grails stack

As you’ve seen, Grails isn’t a web framework, is a full featured environment presented for helping all Java Developers, taking the full advantage of Java and JVM and at the same time working with dynamic, popular and flexible language like Groovy.

No other dynamic language on the JVM integrates with the Java like Groovy, it’s designed to work seamlessly with Java at every level, starting from the syntax reaching into other similarities that can be summarized as follow:

1) Groovy objects are Java objects, it is one of the great advantage.
2) Through Groovy’s joint compiler you can have circular references between Groovy and Java without getting issues while compilation.

1. Grails Installation

By following the below steps you will be able to installing the Grails platform into your machine.

1) Download JDK 1.7+ and configure the JAVA_HOME.

2) Configure PATH system variable for containing the value of JAVA_HOME\bin. That’s making the Java compiler and interpreter accessible from anywhere into your machine.

3) Make sure that you are able to execute the java command on the cmd line.

D:\> java 

4) Download Grails platform.
5) Unzip the Grails platform into your machine’s directory. We’ve unzipped into D:\Grails\grails-2.4.0.
6) Configure your PATH system variables for the Grails.

7) Make sure that grails command is recognized by the command console by opening a new command console and type grails and press enter. In case you’ve provided grails -version you would see a direct response.

D:\> grails 
D:\> grails -version

2. Creating the Grails Hello World Application

For creating your own first Grails application, which will include a simple controller that would use the render scriptlet for providing a message for the browser, you have to follow the below steps.

1) It’s easy step, and for it’s the first step you must go through at every time you want creating a Grails application.
2) Either you’re using the Grails interactive mode or the default mode you have to type grails create-app application-name or you could use grails create-app and the Grails platform would prompt you asking about the name of the application.

D:\Grails\Workspace\> grails create-app HelloWorldApplication

3) Upon completion, the command have created your Grails application and the necessary directory structure. You can see the directory structure by using tree command on cmd.

D:\Grails\Workspace\> tree

3. Creating Controller

1) Grails is an excellent MVC platform, which means it has models, views and controllers for implementing the Separate of Concern (SoC) concept.
2) Controllers, are those components in the MVC architecture that responsible for manipulating the data that received from the view and determining the next view that should be rendered.
3) For creating controllers within Grails, you have simple command create-controller, which will do creation easily. This command isn’t mandatory as in the next coming tutorials, I will explain how to define and create the controllers through IDE like Eclipse.
4) Type grails create-controller HelloWorldController into your command console, and make sure that you’re beneath the Grails application that you’ve created.

D:\Grails\Workspace\> cd HelloWorldApplication
D:\Grails\Workspace\HelloWorldApplication\> grails create-controller HelloWorldController

Once the create-controller command has finished running, Grails will have created two classes for you; a new controller called HelloWorldController within grails-app/controllers directory and an associated test case in the test/unit directory (Grails assumes you’ll need a test if you’re writing a controller). Already Grails defaulted you the created controller within HelloWorldApplication package, since you’ve not specified the package name parameter while creating.

It’s good for you to know that dynamic language like Groovy doesn’t provide you as much compile-time sensation as typed language like Java. Some errors that you might expect to be caught at compile time are actually left to runtime. For that purpose, Grails provides you a Test Unit file.

4. Grails Project Structure

Now, it’s the time for seeing the whole structure of the Grails application

Customizing Controller Response: Since the controller HelloWorldController.groovy was created by Grails platform, it was contained the default single action called index.


package helloworldapplication

class HelloWorldController {

    def index() {

By the convention, index declaration instructs Grails to try render a view called grails-app/views/helloWorld/index.gsp automatically. Even, the controller looking for index.gsp but you can customize the controller for custom display. Open the controller and type into render ‘Hello World Simple Example!’ inside the braces of index method like below.


class HelloWorldController {

    def index() {
         render ‘Hello World Simple Example!’

Now, it’s time for getting the application running.

5. Run Grails Application

To run the application using Grails, command like run-app will make things easy. Make sure you’re locating at the root folder of the application (i.e. HelloWorldApplication) and execute run-app command. Wait for some time, Grails platform for providing you the URL for accessing your deployed application.

Copy the url and paste it into your browser’s address. After pasting that URL, you should see the Grails main page which gives you an indicator of successful deployment.

As well as, if you’re looking into the available controllers you should find your created controller HelloWorldController listed above. Just, press into your controller to see the custom response that was developed using the render scriptlet.

Fore more details refer this links:

Written By Kirtee Chaudhary

For more details, Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *