Home » JVM Languages » Groovy » Grails » Grails tutorial for beginners

About Ima Miri

Ima is a Senior Software Developer in enterprise application design and development. She is experienced in high traffic websites for e-commerce, media and financial services. She is interested in new technologies and innovation area along with technical writing. Her main focus is on web architecture, web technologies, java/j2ee, Open source and mobile development for android.

Grails tutorial for beginners

Grails is an open source framework based on Groovy and Java. It also supports MVC architecture to for developing web application.

This tutorial will describe more details about Grails and represent a simple web application with Grails.

1.Groovy

Groovy is a dynamic object-oriented programming language for the Java platform. It is compiled to Java Virtual Machine (JVM) byte code and integrates with all existing Java classes and libraries.

Want to master Groovy?

Subscribe to our newsletter and download Groovy Programming Cookbook right now!

In order to get you prepared for your Groovy development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

 

2. Grails MVC

Grails uses Spring MVC as the underlying web application framework to implement the web applications based on Model-View-Controller (MVC) design pattern. It includes three core framework components: Controller, Domain, and Grails Servlet Page. The diagram below shows how different part of Grails works together.

grails-architecture

2.1. Controller

The GrailsDispatcherServlet uses SpringDispatcherServlet to bootstrap the Grails environment. There is a single Spring MVC controller called SimpleGrailsController which handles all Grails controller requests.The SimpleGrailsController delegates to a class called SimpleGrailsControllerHelper that actually handles the client request and look up a controller for the request. The Controller which is responsible for transferring the domain to view, also determines which gsp should render the view.

2.2. Domain

Domain stores the data that can be used by controllers and views. Controllers might create a model or may be just process them.

2.3. Groovy server pages

Groovy server pages creates a view for the client response. GSP is responsible for how to display the model to user. It could use grails tags besides any other UI grails plugin.

3. Grails Simple Application

This example is implemented in Groovy/Grails Tool Suite and using Grails 2.4.4. Now, lets create a simple application with Grails.

First, create a Grails Project in GGTS. The GGTS includes Grails, so, if you wish to use a different version of Grails you need to install it and update the new Grails path in GGTS.

create-grails-project

3.1. Controller

We only have one controller here. The UserController is responsible to handle client requests and return proper result when they submit the form. There is a save method which defined with def. When we declare the return type as def, it means the method can return any type of object. Inside the method, an instance of User class is created and parameters are set in the domain object and transfered to the view.

UserController.groovy

package com.java.code.geeks

class UserController {

    def index() {
    }
	
    def save() {
        def user = new User(params)
        user.save()
        render (view: "user", model: [user: user])
    }
	
}

3.2. Model

Creating a domain class is very simple in Grails as there is no need to define setter and getter methods. Grails will handles it for the application.

User.groovy

package com.java.code.geeks

class User {
	
	String firstName
	String lastName   
	 
}

3.3. View

The following gsp file represents a form to user which includes two items with the same name as model attributes.

index.gsp

<!DOCTYPE html>
<html>
	<head>
		<title>Welcome to Grails Tutorial</title>
		<style>
            .form, .text-field, .submit{
                margin: 20px;
            }
		</style>
	</head>
	<body>
		<g:form name="form" controller="user" id="form">
			<div class="text-field"><label>First Name: </label><g:textField name="firstName" value="${firstName}" /></div>
			<div class="text-field"><label>Last Name: </label><g:textField name="lastName" value="${lastName}" /></div>	
			<div class="submit"><g:actionSubmit value="Submit" action="save"/></div>
		</g:form>
	</body>
</html>

Grails uses ${ } to to get model attributes which is firstName and lastName here. When we use

user.gsp

<!DOCTYPE html>
<html>
	<head>
		<title>User page</title>
		<style>
            .user-panel{
                margin: 20px;
            }
		</style>
	</head>
	<body>
		<div class="user-panel">
			Welcome ${user.firstName} ${user.lastName}! 
		</div>		
	</body>
</html>

3.4. Run the web application

Now, it is time to run the web application. To run the web application, click on the Grails Command in the Toolbar and type run-app in the popup.

runing-grails-application

And here are the pages.

3.4.1. Index page

grails tutorial - user page

3.4.2. User page

grails tutorial user page

4. Download the source code

This was a Grails Tutorial for beginners.

Download
You can download the full source code of this example here: Grails tutorial

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments