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)
}
}
September 15, 2008 at 12:37 pm |
very informative
February 27, 2009 at 5:26 pm |
Really cool demo!
March 25, 2009 at 4:25 am |
I just downloaded and installed Grails version 1.1 along with Java 1.6.0_10 and set up my environment variables GRAILS_HOME and JAVA_HOME accordingly and added %GRAILS_HOME%\bin; to my Path environment variable. I hen attempted this tutorial and when I entered >grails generate-all Seminar I received the error:
Error executing script GenerateAll: No such property: readable for class: org.sp
ringframework.core.io.ClassPathResource
gant.TargetMissingPropertyException: No such property: readable for class: org.s
pringframework.core.io.ClassPathResource
at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:329)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:344)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:334)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:495)
at gant.Gant.processTargets(Gant.groovy:480)
Caused by: groovy.lang.MissingPropertyException: No such property: readable for
class: org.springframework.core.io.ClassPathResource
at _GrailsSettings_groovy$_run_closure7_closure12.doCall(_GrailsSettings
_groovy:202)
at _GrailsSettings_groovy$_run_closure7.doCall(_GrailsSettings_groovy:20
1)
at _GrailsPackage_groovy$_run_closure7.doCall(_GrailsPackage_groovy:222)
at _GrailsPackage_groovy$_run_closure2.doCall(_GrailsPackage_groovy:75)
at GenerateAll$_run_closure1.doCall(GenerateAll.groovy:32)
at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324)
… 10 more
Do you have any idea what is causing this error and what I need to do to correct it?
May 7, 2009 at 3:47 pm |
I don’t know exactly what causes the error.
But to get around it, you can change the value of the
grails.enable.native2ascii
setting from true to false in the
grails-app/conf/Config.groovy
file.