Why I Prefer Grails For Rapid Scaffolding of New Apps

There are many good frameworks out there, but the one I prefer for quick demos, simple applications and rapid scaffolding is Grails. Why? Simply put – with just a few lines of code you have a fully functional application: user interface, controllers, data validation and persistence.

But Grails is much more than a tool for quick prototypes. It is a fully featured platform based on the rock solid foundation of Spring, Hibernate and other enterprise-grade frameworks. Plug-ins can be added any time and they will seamlessly add new features to the application.

Grails uses Groovy as its primary programming language, but as it runs on the Java Virtual Machine, you have full access and interoperatbility with any existing Java library. Tooling support is also excellent, both from command line and from IDEs like Eclipse or IntelliJ IDEA.

If you think that the above paragraphs are just hyperboles from an enthusiast fanboy, continue reading and experiment for yourself how easy is to build a new app from scratch.

Downloading and Installing Grails

To start working with Grails, you need to go to its web site at https://grails.org/ and download the platform and documentation. For this post I have been using Grails 2.4.3 (which includes Groovy 2.3.6).

Once downloaded, decompress it in any folder of your choice (we will refer to it as GRAILS_HOME going forward) and I also recommend putting GRAILS_HOME/bin folder to your system or user PATH.

Of course, you will also need a Java Development Kit installed and preferably the JAVA_HOME/bin will be on the PATH. I have been using the JDK 7 update 75 as it seems to be some issues with JDK 8 (for example, SEVERE: Problems copying method. Incompatible JVM? errors at runtime).

Optionally you may use an IDE to help you with coding, testing and debugging activities. Eclipse (through the Groovy/Grails Tool Suite from Pivotal), NetBeans and IntelliJ IDEA Ultimate Edition support Grails very well.

Creating and Running a Project

Although a Grails project can be created manually that would not be very productive, so the best way to start working is to create a new project from a template using Grails tooling.

If using the command-line, just issue this command on any folder of your choice:

>grails create-app

Some libraries will be downloaded on first use and in a few seconds Grails will prompt you for the application name. Once entered, a new folder with the name of the application will be created with a sample application ready to be executed:

| Environment set to development.....
> Application name not specified. Please enter: demograils
| Created Grails Application at C:\projects\temp\demograils

C:\projects\temp>dir /b demograils

Now, let’s change to that directory and issue the following command:

>grails run-app

A few more components needed at runtime will be downloded and shortly the application will be up and running:

| Server running. Browse to http://localhost:8080/demograils

NOTE: In the case that you are on Windows platform and see this error:

WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...)

You will have to manually create the registry key (although I’ve not identified any issue so far if not done). See this page in StackOverflow for more details about the way to solve the issue: http://stackoverflow.com/questions/16428098/groovy-shell-warning-could-not-open-create-prefs-root-node

The Application Domain

The application I want to build is a simple School management app: Teachers, Alumns and Classrooms. A Teacher has 3 attributes: name, date of employment and salary. An Alumn has 3 attributes: name, date of birth and gender. A Classroom, finally, has 5 attributes: name of the class, type (i.e. grade), capacity and two associations with Teacher (1:1) and with Alumn (1:n). Simply put, a Classroom has one Teacher and many Alumns attending it. This is an UML model of our application domain.


Building the Domain as Groovy Classes

In Grails, the domain is preferredly defined as Groovy Classes. In Groovy/Grails, you don’t need getters and setters, and it’s very easy to define constraints to properties, like a maximum length or a range of valid values. Domain classes in Groovy looks very compact and neat – just the key information and the platform takes care of all the rest.

Let’s start with Alumn entity definition. Besides the 3 attributes defined in the UML model, we will define some constraints to attribute values: maximum lengths, required properties and allowed values for the gender attribute. We will also overwrite the toString() method to simply return the Alumn name. This is how the domain class looks in Groovy:

class Alumn {
  String name
  Date born
  String gender
  static constraints = {
    name(maxLength: 100, blank: false)
    born(nullable: false)
    gender(inList: ["Male", "Female"])
  String toString() {

For the Teacher entity, we have a similar setup, and for salary attribute we will define a range of valid values:

class Teacher {
  String name
  Date since
  Integer salary
  static constraints = {
    name(maxLength: 100, blank: false)
    since(nullable: false)
    salary(min: 18000, max: 60000)
  String toString() {

Finally, the definition of the Classroom entity. It is very similar to the other two. A 1:1 association is just defined as with any other attribute. For 1:n associations, like the one binding Alumns to the Classroom, the static hasMany property should be used:

class Classroom {
  String name
  String type
  Integer capacity
  Teacher teacher
  static hasMany = [alumni: Alumn]
  static constraints = {
    name(maxLength: 50, blank: false)
    type(inList: ["1st grade", "2nd grade", "3rd grade",
                  "4th grade", "5th grade", "6th grade"])
    capacity(min: 15, max: 35)
  String toString() {
    "${this.name} [${this.type}]"

The model is simple, concise and understandable even if you don’t know Groovy, which is an excellent test in my opinion on how good the language and platform are defined.

Scaffolding the Application

Once the domain is built, we are ready to scaffold the whole application. One important aspect to note of Grails is that there is no code generation as such unless you want to do it. As a result, data access, controller actions and user interfaces views are generated at runtime directly from the domain definition.

If needed, the whole applicaiton code can be generated which allows to start working on the specifics, but if a CRUD app is just what is needed, then you will get it “for free”.

To activate the scaffolding, we will have to define 3 controller classes, one per each domain class, with a very minimalistic content:

class AlumnController {
  static scaffold = true

class TeacherController {
  static scaffold = true

class ClassroomController {
  static scaffold = true

Running and Validating the Application

And that’s all. Really. When the application was created, Grails automatically set an H2 in-memory database that will be populated on start-up, so the persistence and data store is good for development and testing. When the application is executed, the web container (Apache Tomcat) is automatically configured and the application fully built and deployed. Only thing that is left is to validate that we have defined the right domain, so let’s run the application:

>grails run-app

This is the default, automatically created, home page:


Let’s create a Teacher – the list is iniatially empty so let’s click the New Teacher button. Note that values are validated while they are being typed, so feedback is immediate:

grails-teacher-1 grails-teacher-2 grails-teacher-3 grails-teacher-4 grails-teacher-5

Now, let’s proceed in a similar way and create a few Alumns. Note that since gender attribute has constrained the valid values to those in a list, Grails decided to use a drop-down list in the generated view:

grails-alumn-1 grails-alumn-2

And finally, let’s create a Classroom:




With Grails is easy to start with a new application from scratch. Its scaffolding capabilities are impressive, generated UI and code is nice and exemplar. You can use it for simple applications with minimal effort or to setup a solid foundation from where to build more advanced enterprise grade applications.

P.S.: Code for the sample application shown in the post can be found in GitHub here: https://github.com/deors/deors.demos.web.grails2


2 thoughts on “Why I Prefer Grails For Rapid Scaffolding of New Apps

  1. Great summary! As you mentioned in the opening, the plugin architecture makes adding support for many opensource tools and libraries remarkably quick and easy, usually with completely reasonable default configurations. This is one of my favorite features of the framework and sometimes gets overlooked. Can’t wait for 3.0 to go GA.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s