Simplistic CRUD application with Grails on GlassFish V3 PreludeMy 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.
On my OS X system this meant putting the following lines in my .profile:
Step 2: Create a new Grails application
grails create-app CRUD-GRAILSin 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
grails create-domain-class bookfrom inside your project directory.
This creates the file
grails-app/domain/Book.goovythat 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:
Step 4: Create a controller
grails create-controller bookfrom inside your project directory.
This creates the file
grails-app/controllers/BookController.goovythat 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):
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:
+----------------------+-------+-------+ | 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:
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
<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 :-)