Reality-Loop

Simplistic CRUD application with Grails on GlassFish V3 Prelude

December 26, 2008 | 4 Minute Read
This post was originally posted on my old blog.

My colleague Matt wrote a brilliant tutorial: EJB 3.1 and JSF 2.0 with GlassFish V3 Prelude.

A commenter on Matt's post claimed that using Grails to create the same application would be a tremendous simplification.
I wanted to find out if this was really true, and how much effort it would actually take to achieve the same functionality with Grails.

This post is not a Grails tutorial, and it is also not the goal to create an intelligent application. The goal is to reimplement Matt's application as efficiently as possible.

I assume, that you have GlassFish V3 Prelude installed.

Step 1: Install Grails
Start the updatetool or start glassfish an go to the updatetool inside the admin console. Select Grails for installation and perform the installation.
updatetool.png
Set the environment variable GRAILS_HOME to point to the Grails installation inside GlassFish, and include the Grails binary in your PATH.

On my OS X system this meant putting the following lines in my .profile:
export GRAILS_HOME=/Applications/NetBeans/glassfish-v3-prelude/glassfish/grails
export PATH=$PATH:$GRAILS_HOME/bin


Step 2: Create a new Grails application
Execute grails create-app CRUD-GRAILS in on the console.
This creates a directory named CRUD-GRAILS with a fully functional Grails project skeleton.

You can start your new Grails project with grails run-app. This starts up GlassFish and loads the application. The application is available at http://localhost:8080/CRUD-GRAILS.
As you can see there is not much there apart from a welcome screen.


Step 3: Create the Book entity
Execute grails create-domain-class book from inside your project directory.
This creates the file grails-app/domain/Book.goovy that contains a skeleton class for the book entity.
You could also create the file manually, the grails command just also creates a file for tests...

Edit Book.groovy to add properties:
class Book {
	String name
	String isbn
}


Step 4: Create a controller
Execute grails create-controller book from inside your project directory.
This creates the file grails-app/controllers/BookController.goovy that contains a skeleton class for the book controller.

As with the Book entity, you could also create the file manually, the grails command just also creates a file for tests...

Edit BookController.groovy to provide the CRUD operations on the book entity (this is called scaffolding):
class BookController {

    def scaffold = true;
}


That's basically it! The application can create, update, show and delete books.

It took typing three commands and writing three lines of code ... all completed in less than 10 minutes ... not bad I would say!

Let's see what Grails has to say: grails stats
	+----------------------+-------+-------+
	| Name                 | Files |  LOC  |
	+----------------------+-------+-------+
	| Controllers          |     1 |     3 | 
	| Domain Classes       |     1 |     4 | 
	| Integration Tests    |     2 |     8 | 
	+----------------------+-------+-------+
	| Totals               |     4 |    15 | 
	+----------------------+-------+-------+

Ok, using dynamic scaffolding is a bit like comparing apples with oranges ... lets change that: grails generate-all
This generates the concrete views and the controller for the book entity, that have been generated dynamically by scaffolding up to now...
... now you can look at the code and adjust it to your needs.

Another little thing is getting rid of the Grails logo. Edit grails-app/view/layouts/main.gsp: Delete the lines between the <body></body> tags except <g:layoutBody /> ... I think we stay with the nice CSS and icons :-)

Ok, I don't claim that Grails is a silver bullet, but it is quite impressive how fast you can achieve some core functionality! The question is now how well it scales for real-world-requirements ...

Matt are you ready to implement some entity-relations, validations, conversations, AJAX-UI ... ? I would be ready for the challenge :-)

http://www.google.com/s2/favicons?domain=twitter.com follow me on twitter, I need some friends :-)