Gettings Started With Grails

So if you want to get started with Grails after reading Holy Guacamole Web Development with Grails then you are in the right place. In the next few paragraphs I will go over how to go from installing Grails to generating the scaffolding for a domain model. Just to provide a context for the code, lets just imagine we are building yet another blog engine.

Installing Grails is pretty straight forward. Just download and following the installation guide available from the official Grails website. I will make one observation that on OS X I add to change mode of the grails and ant scripts under the Grails installation directory. You might have to do the following if you are using a non Windows platform:

chmod u+x grails
chmod u+x ant

Also make sure you have the JAVA_HOME, GRAILS_HOME environment variables set and add GRAILS_HOME/bin to the system path. Once you have grails installed you can create a Grails application directory structure by running the following command:

grails create-app

The ‘create-app’ command line option is a ant build target. As noted, this command will create a directory structure for the Grails application and this directory structure is similar to that used by Ruby on Rails. But before Grails creates anything, you will be prompted to enter the name of the application. Let’s name the application blogjet for a lack of a better name.

That is it, you are all set to start generating your domain model classes. You might be wondering, ‘But what about the database configuration?’ Well, unlike Ruby on Rails, Grails does read you mind and guess that you want to test drive Grails not waste an extra five minutes configuring out the database connection. By default Grails will use HSQLDB as an in-memory database for you get get started. Just in case you ever want to use a different database you can modify the data source configuration file:


Before we can proceed we need to create a domain model class. Change directories to the blogjet directory. To create a domain class enter the following command from the Grails application directory.

blogjet$>grails create-domain-class

Just as before when creating the Grails application, you will be prompted for name, this time for the domain class. Enter ‘Post’ as the domain class name. The create-domain-class target will create a Groovy class in grails-app/domain named Post.groovy:

class Post {
  Long id
  Long version

  String toString() { "${} :  $id" }

  boolean equals(other) {
    if(other?.is(this))return true
    if(!(other instanceof Post)) return false
    if(!id || !other?.id || id!=other?.id) return false
      return true
    int hashCode() {
    int hashCode = 0
    hashCode = 29 * (hashCode + ( !id ? 0 : id ^ (id >>> 32) ) )

For brevity’s sake I will not display the toString, equals, and hasCode methods in future references of domain classes.

At this point we are ready to create the controller for our blog. Execute the following from the Grails application directory:

blogjet$>grails create-controller

Again, you will prompted for a name. Enter Post for the controller’s name. This command will create a PostController.groovy file in the grails-app/controllers directory.

class PostController {
  def index = { }

You can create a CRUD scaffold for posts by modifying the Post controller in the following fashion:

class PostController {
  def scaffold = Post
  def index = { }

Grails comes with the Jetty servlet container as part of its installation. To start the server run the following command from the command prompt:

blogjet$>grails run-app

The Jetty server will start on port 8080 so you can direct your browser to http://localhost:8080/blogjet/post/list to create, read, update, and delete blog posts.

In the course of this post, we created a new Grails application by executing the ‘grails create-app’ target. In a similar fashion we created a domain and controller class. We did not modify our datasource configuration so we are using the default in-memory database in development mode as we are familiarizing ourselves with Grails. Grails comes with the Jetty servlet container and we can start the server by executing the ‘grails run-app’ target. We were able to create a CRUD scaffold with just one line of Groovy code in the the controller.

Yes, I know there are still a lot more question than answers especially since our models don’t have any data fields. In the next post we will go ahead and flush this out, add data fields to our models, and add has many relationships.

Technorati Tags: , , , , , , , ,

Leave a Reply