Getting Started With Grails

Yoram Gondwe
Developer Circle Lusaka
6 min readApr 19, 2019
Introduction to Grails

In my last article, I gave a concise prologue to the Grails system featuring the reasons why it is generally utilized. In this article will go somewhat further into investigating the Grails structure. We will investigate the establishment of the Grails system and making our first Grails application.

To start I exceedingly prescribe IntelliJ Idea as the IDE (Integrated Development Environment ) of decision when developing in Grails. To begin with, download the most recent version of Grails from their site. You will likewise need to download the Java SDK which is also accessible on their site for download.

Placing Grails in the Global windows classPath

I will take you through placing the framework in your classpath. This is to empower us to utilize the cmd to run Grails commands in the console.

If you are not a windows user you can skip this part of the article.

  1. Place the framework file that you just downloaded and place into your C:/ drive
  2. Go to your system properties and click advanced system settings

3. Another small window will pop up and then click Environment Variables

4. Now that you are here click the variable that says path and ensure that it is highlighted and then click edit.

5. Now you can browse to the grails folder that you placed in your C:/ drive and select the path that is named bin.

Now you are done with installing Grails in your classpath.

Creating Your First Grails Application

We will utilize IntelliJ Idea as our IDE to make our first application. This application will be a straightforward application that will enable us to enter our name and it will welcome us by the name gave.

Creating a new project

Make a new Project on Idea and select the path where your Grails framework files are located on your system and you are ready to proceed

Now select the where you would like your application to be placed on your system and give it a name. In the tutorial, we will call our app DemoApp.

Grails will now need to build and install some dependencies for this make sure you are connected to some internet. Please do not worry it does not take a lot of data complete the build. You will require some internet access on your first installation and when you want to install some new dependencies.

Explaining the contents of the grails-app [main]

When we investigate our application we can see an envelope called grails-app [main] , this is the grails root folder. I will explain some of the main folders that we will use for creating our application.

  1. Assets- contains Images, JavaScript and CSS files for your application
  2. Config- contains configuration files for your application i.e DB connections and Security permissions
  3. Controllers- contains your application's controllers ( The Controller is responsible for controlling the application logic and acts as the coordinator between the View and the Model. The Controller receives an input from the users via the View, then processes the user’s data with the help of Model and passes the results back to the View).
  4. Domain- contains your Model classes which create your database tables. Model classes can either be created manually or generated from database entities.
  5. Views- Is the component involved with the application’s User Interface. Here you create your user interface files using HTML however, they end in the extension .gsp

Let get down to it

We are going to build a simple hello world program that will help us get a basic understanding of how MVC works in Grails.

Fork or clone this DemoApp in Github and follow along. As we proceed I will give you the paths in which the code I will present in this article is in the actual project.

1.1 Domain Classes

Following the MVC framework that I just mentioned earlier, domains and Models are the same things. They are used to define the schema of tables in the database. This includes table name and constraints.

/grails-app/domain/tutorialforgrails/Names.groovy

class Names {
/* Here you define the column names for your table in the Database*/
String firstName
String lastName

static constraints = {
/* This where you add constrains to your table columns
such as UNIQUE PASSWORD BLANK NULLABLE etc
* */
firstName nullable: true, blank: true
lastName nullable: true, blank: true
}
}

1.2 Controller

grails-app/controllers/tutorialforgrails/home.groovy

class HomeController {
/*This function renders the page where we are going to enter our Names */
def
index() {

render(view: '/name')
}

/* This function receives data from the form in the view and saves the data in the database*/
def
save() {
String firstName = params?.firstName
String lastName = params?.lastName

new Names(
firstName: firstName,
lastName: lastName
).save(flush: true, failOnError: true)
/* redirects control to the next function*/
redirect(action: 'greet')
}

/*This function queries the last entry in the database and sends that data to the view */
def greet() {
def name = Names.last()
/*The keyword model is what we use to send data to the view */
render(view: '/hello', model: [name: name])
}
}

1.3 URL Mappings

Url Mappings are simply a mechanism that the framework uses to interpret URLs.It gives a specification of what controller that URL is mapped to and what action or method does it have to call. Hence we give every URL a name because each mapping has a different function.

grails-app/controllers/tutorialforgrails/UrlMappings.groovy

class UrlMappings {

static mappings = {
"/$controller/$action?/$id?(.$format)?" {
constraints {
// apply constraints here
}
}

// "/"(view:"/index")
"500"(view: '/error')

"404"(view: '/notFound')

//we changed the landing page from the original index page to our names page

'/'(controller: 'home', action: 'index')

//This maps the controller and action(method or function) where name saving will happen

'/save'(controller: 'home', action: 'save')

//this maps where our application will query the name we entered and greet us
'/hello'(controller: 'home', action: 'greet')
}
}

1.4 Views

/grails-app/views/name.gsp

In this view, we will enter a first and last name that we will send to the controller to save in the database.

<!doctype html>
<html>
<head>
<title>Demo App</title>
<asset:stylesheet src="bootstrap.css" rel="stylesheet"/>
</head>

<body>
<g:form controller="home" action="save" method="post">
<div class="input-group mb-3" style="width: 50%;">
<div class="input-group-prepend">
<span class="input-group-text">Person</span>
</div>
<input type="text" name="firstName" class="form-control" placeholder="First Name">
<input type="text" name="lastName" class="form-control" placeholder="Last Name">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</g:form>
</body>
</html>

This next view will greet us with the names that we entered.

<!DOCTYPE HTML>
<html>
<head>
<title>Demo App</title>
<asset:stylesheet src="bootstrap.css" rel="stylesheet"/>
</head>
<body>
<div class="content">
<div class="jumbotron">
<h1>Grails Tutorial</h1>
<p><h1>Hello ${name.firstName} _ ${name.lastName}</h1></p>
<g:link controller="home" action="index" class="btn btn-primary">Go back</g:link>
</div>

</div>
</body>
</html>

Congratulations you have created your first application !!!

Conclusion

In my next article, I will take us through the security of our applications utilizing the spring security service. In the event that you might want to connect with me for any questions concerning this article visit my Linkedin and follow me on Github.

--

--