A quick start guide to Grails
There was a period of time when Java developers can only envy the RoR developers for completing the day’s work by lunch time. Not anymore, for we have Grails to the rescue!
After exploring Grails for an hour, I’m really impressed by its fuss-free scaffolding. (In an hour, I had enough time to touch on the basics and to write this post. Ha ha
) Don’t be convinced by me, you’ve gotta try it for yourself to believe it.
Ok, first of all, we will need to install Grails, which is nothing more than just:
- Download and unzip Grails.
- Define a GRAILS_HOME environment variable to point to the unzipped Grails directory.
- Add $GRAILS_HOME/bin to your $PATH.
Installation is complete!
Now let’s begin writing our very first “real-world” application. For starters, our application will help to manage seminars and registrations. Execute these steps on your shell:
- grails create-app myapp
- cd myapp
- grails create-domain-class Seminar
- grails create-domain-class Registration
- Edit myapp/grails-app/domain/Seminar.groovy:
- class Seminar {
String title
Date startDateTime
String city
Float cost
Boolean mealsProvided
static hasMany = [registrations:Registration]
}
- class Seminar {
- Edit myapp/grails-app/domain/Registration.groovy:
- class Registration {
String name
Date dateOfBirth
String gender
Seminar seminar
}
- class Registration {
- grails generate-all Seminar
- grails generate-all Registration
- grails run-app
That’s all the code you need to write to set up the scaffolding! Let’s enjoy the fruits of our labour by browsing to http://localhost:8080/myapp. You’ll notice this scaffolding automatically includes:
- CRUD functionalities for both our Seminar and Registration domain classes.
- In the Registration data entry page, the Seminar field is rendered as a dropdown list of all Seminar objects.
- In the Seminar data entry page, the MealsProvided field is rendered as a checkbox.
Here are some screen shots of the scaffolding:
Oh, did I mention the one hour also includes a trip to the kitchen to reward myself with a cuppa coffee?
For further reading, I recommend this wonderful and FREE e-book: Getting Started with Grails by Jason Rudolph
August 3, 2007 at 6:49 pm
[...] Grails quickstart 3 08 2007 Just to jot down my delightful journey on learning the Grails framework: http://fernvale.wordpress.com/2007/08/03/grails-quickstart [...]
August 7, 2007 at 9:38 pm
Your little demo is nice. Now how I program the validations for each field?
August 14, 2007 at 12:49 am
An example to add in validation for the Seminar domain class:
class Seminar {
String title
Date startDateTime
String city
Float cost
Boolean mealsProvided
static hasMany = [registrations:Registration]
static constraints = {
title(blank:false)
startDateTime(blank:false)
cost(min:5f,max:99f,nullable:false)
}
}