Grails Hello World Example

1. Introduction

This example is about creating the first application in grails and we basically start application or any introduction with a Hello example. So this Hello World program in Grails will be the introduction for starting Grails application.
Let’s start with Grails at first. Grails is a web application framework for JAVA platform. Also Grails is highly productive framework of Java and it is easy to use too. While working in Grails we must first understand its component. Grails is based on MVC paradigm so in a basic grails program it consists of Model View and Controller.

1.1 Model

It is the part of data and information interaction. Grails basically have model called domain.

1.2. View

It is the part to be shown to the user. The file called GSP(Grails Server Page) is used as view.

1.3. Controller

It is the part where operation of data and request from the user is processed.

Now to start with our program we must have the following project environment.
1. Intellij Idea 14
2. Java 1.7
3. Grails 2.5.2
Open your intellij idea and create new project as shown in the figure.

Fig 1. View for Creating New Project

Now create project sdk as your path of jdk stored as shown in the following figure.

Fig 2. Dialog box for project sdk and grails library

Click Next and provide Project name and path where project needs to be saved as following.

Fig 3. Dialog box to provide project name and project location

After the project has been created then intellij idea creates project and a dialog box appears to select whether to make that project a plugin file or application file same as shown in figure.

Fig 4. Dialog box for create an application or to create a plugin project

Now when the create-app is clicked the project based on intellij is created which contains the following project structure as it is.

Fig 5. Project Structure

Now you should create a controller. To create a controller use Grails Run Target (ctrl+alt+g) with the following command line
create-controller example.HelloWorld
This will create a controller named HelloWorld inside example package. When we see a controller there is default method as index. We can work on that but to be more specific let’s create a method as
sayHello and write following codes.

def sayHello(){
println "Hello World "

The println prints the arguments written in quotation in interaction mode. Now run your application. To run the application open Grails Run Target (ctrl+alt+g) and add the command as run-app same as in following figure.

Fig 6. Dialog box for Grails Run Target

After the application is run open the application in browser by following the url page http://localhost:8080/HelloWorld as shown in the figure below.

Fig 7. Browser view for Project output

There will be a list of controllers and other files in grails default view in browser. To check your work click on the controller that you have created that is HelloWorldController . After you click on the created controller the program will automatically take the default action index and searches view for it . So to get Hello World in web browser let’s create a Grails Server Page for sayHello method inside views.helloworld.sayHello with the following code.


	Hello World

In this case sayHello is the generated view (sayHello.gsp) page and helloworld is the package inside views. Now save the project and stop and re-run the program. Visit the page http://localhost:8080/HelloWorld/helloWorld/sayHello and there we can see Hello World in the browser page as follows

Fig 8. Output of the project in web browser

2. Conclusion

So as we shown in the example above, we can create a small Hello World program based on Grails platform. We can create multiple methods and views for each of the method to display the work in a web browser as done in above program using “sayHello” method and “sayHello.gsp” page.

Jagriti Srivastava

I am Jagriti Srivastava , currently working as Counsellor in Sunway International Business School. I have 4 + years of experince in software development in Java and Grails. I have been also working in the post of President in Competitive Coding Community of Sunway International Bus iness School. Prior I have been involved in multiple projects regarding Java Web Application Development and worked as IT instructor for a year .
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button