Grails Tutorial for Beginners

What is Grails?

Grail is an open-source web application framework that uses the Apache Groovy programming language to develop real projects. It is a plug-in based framework that uses its build system-(Gant) but plans to migrate to Gradle. The Grails homepage provides numerous pre-defined plugins which extend the Grails framework.

The Grails framework allows for instance development without requiring any configuration. Thus, easy to learn for students or novice developers with no web experience. With Grails, you can even start hacking through it and be productive in a matter of hours. Grails uses JavaEE as the structural basis and Spring for structuring the application via dependency injection.

Features of Grails

Grails framework is opted by the startup organizations to launch the product from the development mode to production mode. It ensures a different way of creating web apps to the standard Java mechanisms of servlet programming, Java Server Faces, JSPs, etc. Apart from this, it has many other features.

Some of the salient features are listed below:

  • Smart Coding

A developer has to write less line of code to develop a software application in the Grail framework as compared to other web frameworks. Hence, it needs minimal maintenance and an increase in software developer’s productivity.

  • Vibrant and Supportive Community

The Grails technology offers excellent assistance and support for both beginner and advanced programmers. Almost, all questions and queries are answered by the active Grails community.

  • Grails Scaffolding

Grails web application provides Scaffolding, which helps developers to create an application with CRUD(Create, Read, Update and Delete) functionalities quickly. The scaffolding feature is highly useful in creating the “wow” effect. Developers can create screens that can perform various CRUD operations.

  • Compatibility

Grails provides 100% compatibility which can easily integrate with Java code. The developers can also use their existing Java code without any need to modify the system to a great extent. Java programmers can start using Grails easily without undergoing any formal training.

  • Extensive Plugins

Grails supports more than 700 plugins, which have been successfully developed by the core development team and Grails application developers. The plugins provided by Grails improves integration and reduces the effort of software developers.

  • Minimal Server Restart

It offers minimal server restart feature do the developers do not have to restart their servers again and again to sync their changes. Grails web application framework utilizes a well-defined mechanism to reload and apply all changes made during the application development process, which reduces software development time.

  • Shorter Learning Curve

Software developers find it comparatively easy to start working on Grails, especially the programmers who are accustomed to the Java language. Grails web application technology delivers a whole new level of excitement to web-oriented application development.

  • Tag Lib Mechanism

In Grails, Tags can be created very quickly. It is straightforward to modularize and reuse the view layer. It is effortless to write a tag in Grails, and these tags can also be written in Java.

Difference between Grails and Groovy

Grails was previously known as Groovy on Grails as this framework was built on Groovy. Many people use both the terminologies interchangeably. But they both are different and have different features which are listed below:

Grails Groovy
Groovy is an object-oriented programming language for the Java platform. Grails (previously known as “Groovy on Grails”) is a programming framework based on Groovy and inspired by Ruby on Rails.
All groovy codes are compiled to Java byte codes which can be executed by JVM. It’s a RAPID Web Application development framework which provides a tremendous boost in development by abstracting its common requirements in web development.
Groovy has a Java-like syntax and works seamlessly with Java bytecode. Grail uses Spring , hibernate and a bunch of other libraries.
All groovy codes are compiled to Java byte codes which can be executed by JVM. Grail is the advanced version of Groovy.

What are the advantages of Grails?

The following are the advantages of the Grails Framework:

  • Grails Framework provides a rapid development cycle.
  • If the developer is dealing with a small or medium-size project, Grails is ideal for them.
  • It offers a range of plug-ins to make the developers job very simple.
  • Grail framework provides very impressive documentation.
  • In this, the setup process is straightforward. Therefore, now the developer can build an app in an hour.
  • Simple GORM. It may take some time to learn, but once you are familiar with it, it’s lovely.
  • You can see the changes simultaneously by hitting the refresh button.
  • Less CSS framework plug-ins. Hence, it’s easier to manage CSS.
  • It supports Dynamic configuration feature. Therefore, you can change the configuration anytime without restarting the server.

What are the disadvantages?

  • You have to deal with runtime language. Its negative quality is that it is error prone and you have to bear with other cons of runtime language.
  • If you are working on a multi-threaded app, GORM can be problematic for you.
  • You have to buy IntelliJ Idea because other IDE’s either don’t have or provide limited support.
  • Developers generally declare variables with “def” which is equivalent to “object.” It’s tough to maintain.
  • Interpreted languages increase weight, and that directly affects the run time.
  • For doing efficient coding in Grails, you must first learn Groovy coding.
  • This framework only works with GORM but not with any other ORMs.
  • In Grails the integration process is complicated.

Grails MVC

Grails Framework uses Spring MVC as the underlying web application framework to implement the web applications based on the 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 MVC

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 envoys to a class called SimpleGrailsControllerHelper that 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.

Domain

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

Groovy server pages

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

How to install Grails Framework?

Step 1 – Oracle Java Development Kit (JDK) Installation

A Grails framework runs on the Java platform; therefore, before installing Grails, you will need to install a Java Development Kit (JDK)  with version 1.8 or above. Hence the first step is to establish a Java Development Kit (JDK) on your local machine. To download Oracle JDK installer, visit Oracle’s official website.

install Grails Framework

Step 2 – Grails Application Framework Installation

Similarly, download the latest version of the Grails framework by visiting the official website. After downloading the Grails software, extract the application to your system.

install Grails Framework

Step 3 – Environment Variables set-up

install Grails Framework

To set up environment variables add the JAVA_HOME (the directory where you have installed the JDK) and GRAILS_HOME (the directory where your Grails framework was extracted) to their own executable programs.

Step 4 –Grails Tool Suite Installation

install Grails Framework

Download the Grails tool, which is built on the Eclipse platform and is one of the most popular integrated development environment (IDE) for Grails application development. To download the tool, visit the website Spring.io and extract the device to your local machine.

Step 5 –Grails Tool Suite Configuration

While configuring the Grails tool ensure that Java Runtime of the IDE points to a JDK instead of a JRE. The following steps help you to identify this.

Step 1: Edit the Installed JRE  your system.

Step 2: Check if it is already pointing to a JRE installation

Step 3: If yes, change it to the JDK installation directory

install Grails Framework

Grails for Startups

 

Groovy as a language and Grails as a framework would be a great choice for startup groups or companies that are looking for a technology stack that will enable them to churn out functionality with speed and quality and that are leaning toward being a Java shop. As mentioned previously, a company’s survival depends on its ability to accelerate time to market and optimize costs. Grails can deliver against those goals.

Groovy for Enterprise

Java Shops For organizations and companies that are already “Java Shops”—and that have made heavy investments in Java—adopting Grails should be fairly smooth and easy. The framework is a natural fit in an enterprise setting for several key reasons.With Grails Java support, it becomes easier for the developer to integrate with native and third-party Java products.Second, most establishments have already made an investment in Spring and Hibernate. Since the Grails framework is built on top of Hibernate and Spring, it is easier not only to understand the framework but also to address any issues that arise in the production.

Groovy with Existing Databases

An enterprise usually has a well-defined database or domain model, with numerous applications interacting with it. In most cases, applications are usually built with these existing database tables. Thus, Java enterprises likely have existing domain models based on Spring, Hibernate or JPA. Fortunately, Grails can also use the existing modelsincluding the JPA Annotations or XML Hibernate mapping in the Grails project as Java source or as a JAR, create a Hibernate configuration file and then add GORM constraints in separate files. Doing so provides full access to all of Grails’ great features, such as ‘dynamic finders’ and ‘Where queries’.