Getting Started with Grails Java Framework

February 5, 2021

In recent years, many developers have been using frameworks when developing websites. Frameworks help by making application deployment quicker and by ensuring quality code. Java provides multiplatform supports, this includes a desktop application, web application, mobile app development, and many more.

The web applications can be embedded with JavaScript, HTML, CSS, and many front end applications.


Grails is Java and Groovy framework used when developing agile web applications. Grails implements the MVCS (Model, View, and Controller) design pattern.

Grails is superb because it allows developers to concentrate more on actual application requirements and spend less time configuring the framework. Grails provide tools for development, and it is built based on tools like: Quarts, Hibernate, Spring, and Gradle for library management.

There are many Grails framework versions being built that have the support of different libraries and tools.


  • Have an IntelliJ integrated development environment (IDE).
  • Some basic knowledge in Java development.
  • Have Java development kit (JDK) installed in your computer/laptop to help with the application development. In this case, we will use Java 11.

Installing Grails

Getting started

Grails provides a set of commands to support application development. IDE has also eased the use of commands and provided a way users can click menus for fast application development.

To create an application in the Grails framework, the create-app command can be executed in terminal or Windows CMD with arguments like the application’s name.

The command below shows how to create an application via terminal or CMD. Open terminal or CMD in a directory where you want to save the application.

In this case, we have a folder called Projects in the document folder.

Navigate to Projects folder via the terminal.

cd C:\Users\user\Documents\Projects         
cd C:\Users\user\Documents\Projects

Create the first application with the following command.

grails create-app Firstapp


The Grails application is formed within the Firstapp directory, and one can execute many other grails commands.

To run the application run the following command.

grails run-app

The command will run the web application via a browser. You can type the highlighted command in the browser.



Folder structure and directories

The grails application has the following folder structure.

├── .gradle
├── build
├── gradle
├── grails-app
├── src
├── .gitignore.txt
├── build.gradle
├── gradle
├── gradlew
├── gradlew.bat
├── grailsw
├── grailsw.bat
└── grails-wrapper

The grails-app has the following directories.

├── assets
├── conf
├── Controllers
├── domain
├── i18n
├── init
├── services
├── taglib
├── utils
└── views

assets folder

This folder is where all static folders are kept, like Javascript, Images, CSS, and other static multimedia content.

conf folder

This is where the configuration files reside, including resources.groovy, application.yml, logback.groovy, and any other configuration from plugins.

controller folder

The folder contains Controllers, default UrlMapping.groovy, that control requests.

domain folder

This is the folder where model classes are kept that are similar than database tables.

i18n folder

The folder has resources for translation.

init folder

The folder contains files related to when you need to launch the web application.

services folder

All business logic is usually put under the service folder.

taglib folder

This folder contains tags, and developers can add their own tags. Tags help in compressing GSP code since they help when re-using code.

utils folder

In this folder, we can put utility Groovy classes.

views folder

It contains GSP files, where HTML code is kept


MVC design patterns help in partitioning responsibilities in the application to simplify the architecture. Model classes represent domain objects in the system.

Controller classes control the flow of your application. Service classes handle the business logic in the application. Views artifacts are used to present information in a way desired.

Creating a Domain Class

The command below adds an empty Domain Class.

grails create-domain-class Student
package firstapp

class Student {

    static constraints = {

We can add attributes.

package firstapp

class Student {
    String name
    String gender
    Date dateOfBirth
    Integer position
    static constraints = {

Creating Controller Class

The below command adds an empty Controller Class.

grails create-controller Student
package firstapp

class StudentController {

    def index() { }

Configure database

Database configuration is done in the application.yml to any database with any credentials needed. Within the file, any database can be connected, provided it has a JDBC driver.

pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password: ''

dbCreate: create-drop
dbCreate: update
dbCreate: none
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

Generating views

The command below generates GSP views.

grails generate-all Student

The Student Controller Class will have many other functions in it. Run the grails application and access the following link http://localhost:8080/student/index in the browser.

The command also creates views in the views folder under the same object student.

The following views are created.

├── show.gsp
├── index.gsp
├── create.gsp
└── edit.gsp

Each view has its function. show.gsp is used to show a single object selected, index.gsp is used to display a list of items in the database, create.gsp provides a form when adding new items, and edit.gsp is used to edit any existing data.

The new view in the browser will look like the following.


The same folder can be opened in Intellij and continue with fast coding. For any Class to be added, the developer needs to right-click on a given folder and create a Grails Class based on the Folder.

Any library that is not there can be added through build.gradle. Just like CMD/Terminal grails has a terminal window where one can do the grails command without coming to the main CMD/Terminal.

This how it looks in IntelliJ.


For the source code check here.

More details


Congratulations, your fast grail app is successfully running. Grails Framework help by taking advantages of several tools and bringing them together.

In this article:

  • We have installed the Grails framework.
  • We have looked at Grails directories.
  • We have created a Student Object Management.

Peer Review Contributions by: Odhiambo Paul