A walk through of Grails-Part 1-Introduction


Grails logo
Grails

This is the part 1 of Grails (Note: click here to see why it is not called Groovy on Grails?) articles which I will cover the flexibility of Grails. You can see the part 2 here.

One of my friend through this to me to try it out Grails. I explored Ruby on Rails and Codeigniter and I always found more bond of convention over configuration or code over configuration right from the age of Java Beans.The simplicity will reduce the development time and increase the productivity.

With all these years with the web development and specially with java, I found these are the important feature any java web frameworks should support.

I will explore one by one with an example on my fourth coming blog posts. Another important aspect what I found interesting is that the command line tool which Grails provide make our job more easier. The only disappointment I had is it is not supporting Servlet 3.0 Spec, so you can’t really run it on Tomcat 7 and higher.

Setup Grails.

Grails running on Groovy. So we need Groovy to be setup on your machine. (Note: This setup is for Ubuntu)
Groovy can be installed from the Ubuntu repository.

>sudo apt-get install groovy

To test the installation

>groovysh

This should open a python like interactive prompt to test your scripts quickly

>groovyconsole

This will open a GUI based swing tool to test your scripts quickly.

Installing Grails:

The simplest way to install Grails is to install from Ubuntu repositories.

>sudo apt-cache search grails

and get the latest version of Grails. In my demo I have used

> sudo apt-get install grails-1.3.7

But you can’t get the latest version from the repository. You need to do either manual installation or integrate with maven which I will cover on my next blog.
To start a new project simply issue this below command.

 >grails create-app grails-app

The folder structure of the hello world application will be like,

Grails folder Structure

conf:
The conf folder has the configuration files for hibernate and spring if you don’t want to use GORM and override it.
controllers
It holds all the controllers groovy files
domains
All your domain objects which interact with data source goes here
view
The view part of Grail Server Pages (gsp) goes here
services
If you want to expose the app as services all your web services code goes here
i18n
The internationalization support goes here
taglib
Grails support you to develop its own Taglibs as well.
plugins
Grails allow you to develop plugins. There are some you can found it here
src
If you want to develop any components on java or Grails you can create and reuse with Grails.
test
All your test cases for Grails goes here.
webapp
Typical java web application container holds all the js, image, css and container configuration files.

The Hello world:
Lets create a simple controller and run our hello world. Grails have interesting set of command tools which help to speed up the process. you can check their help tool by executing,

 grails help 

Now to create a controller issue,

grails create-controller com.test.hello

This will create HelloController.groovy file inside com.test package with index() file.

package com.test
class HelloController {
    def index() {
         def myname = "hello groovy"         
         render "This is cool ${myname}"
    }
   
}

now run the build-in server to test it out.

> grails run-app

you can see

server running on http://localhost:8080/grails-app

Now on your browser just try

http://localhost:8080/grails-app/hello/index

There you are, you cooked your first fish on Grails.

Advertisements

3 thoughts on “A walk through of Grails-Part 1-Introduction

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s