Tuesday, 27 April 2010

Grails From Basics - First Application


Yesterday I covered the basics of getting your environment set up and ready to run your first Grails application.

We'll now step through creating an application using the command-line, which is quite straightforward.

First of all we need to create a 'workspace' directory (I've created one called 'GrailsDemo' under 'MyDocuments') then change into that directory and type the following:

grails create-app library





What Grails actually does is create a directory heirarchy that is largely empty but with directories that will hold our classes, controllers, services, tag-libraries, etc. Some of these directories (most notably the 'grails-app/conf' directory) will be populated with default configuration files:



As the name implies, our sample Grails application is going to be an online library system that allows us to build up a database of books together with associated authors, publishers, and so on - this also ties in quite closely with the examples provided in the main Grails documentation.

We could run this right now, but the application wouldn't actually do anything as we've not yet defined any classes...

What we need to do is define our first Domain Class; a Domain Class is simply a regular Groovy class (Plain Old Groovy Object or POGO) that resides in the 'grails-app/domain' directory. Any class that is found in this directory (or subdirectories) will be persisted by Grails to some sort of underlying database.

For our example application we will start by simply creating a Book domain class:

grails create-domain-class com.example.Book

Note the use of 'com.example' before the class name - this is simply a fully-qualified class name that prevents the name of our Book class from clashing with Book classes defined anywhere else in the system - you would normally use your own domain-name (in revese) here:



This command will create the 'com/example' directory structure under 'grails-app/domain' for us and create a file called 'Book.groovy' with skeleton code inside:



We need to edit the skeleton code to something meaningful - for now we'll simply add a single String property called 'title' and place a 'constraint' on it that says that it cannot be blank. We'll also create a toString() method (that simply returns the 'title' string) which Grails' scaffolding can make use of - more on all this later:



So far we have defined our Domain Class (Book) but we also need to create a Controller that will interact with our users. The Controller will handle user requests and manage instances of our Domain Class for us - in other words the Controller will allow us to create, read, update and delete (CRUD) instances of our Domain Class called Book:

grails create-controller com.example.Book

Without a Controller our users can do nothing:



As with the Domain Class, this command will create the 'com/example' directory structure under 'grails-app/controller' for us and create a file called 'BookController.groovy':



Again, the file will be created with skelton code:



Grails offers something called 'scaffolding' to get you up and running as quickly as possible - this means that although we do need to create a Controller, we can tell it to 'scaffold' the usual create, read, update and delete operations for us via a single line of code:



Grails uses 'convention-over-configuration' to assocate the BookController controller class with the 'Book' domain class, as they both have the same prefix - you can change this if desired, but it's likely to make things confusing...

We now have everything in place to try our first run of the library application - give it a go:

grails run-app





Note the URL in the last line; open your favorite browser and enter that URL in the address-bar - you should see something like this:



Click on the link below 'Available Controllers' and have a play - you should find that Grails has created a set of Views that will interact with our BookController, both based on 'scaffolding':



The only two hints we had to provide were:

  • The toString() method in the Book class
  • The def scaffold = Book line in the BookController class

Grails does the rest for us!

To terminate the application simply press <CTRL>+<C> in the command window:



The covers the absolute basics of creating a Grails application. Next time we'll create the same application using an IDE, rather than doing everything from the command-line - this should help to illustrate just how much easier using an IDE makes things.

3 comments:

  1. The very second you post, you get spam. How about that for blog fame?

    It's nice to see you back alive and online Duncan. I'm afraid I don't work with Grails, but I thought I'd drop in and say hi. I wonder if you can work out who I am... especially as we haven't communicated in over 10 years...

    ReplyDelete
  2. Things are good, thanks. Returned from Japan in 2004; now have a son, another troublemaker is en route as well. Still in finance, despite saying to myself "I'm leaving" several times since 2002.

    There are fewer pictures of me online these days, children hog all the limelight despite being so small. I suppose you might say it's their turn, but I am selfish and want the limelight back. You can probably dig out an e-mail address on www.joelgoodwin.com, I won't post it onto a unsecure blog =)

    ReplyDelete