Getting Started with Grails
Grails is a full-stack web application framework built on top of such tried and true open source frameworks as Spring, Hibernate, Ant, JUnit and more. By applying principles such as Convention over Configuration and Don't Repeat Yourself, and taking advantage of the dynamic Groovy programming language, Grails makes it incredibly easy to use these powerful tools. Grails doesn't reinvent the wheel; Grails makes a wheel that inflates itself and rolls where you want it to!
In case you are new to Grails, we'll start with a brief introduction, which should be enough to get you hooked and turn you into a Grails developer. That's when this Refcard will come in handy; it is a cheat sheet for Grails developers, a quick source for those things you keep having to go back to the docs to look up. Controllers, Services and Views with a detailed GSP taglib reference.
Installing Grails
Download the Grails archive from http://grails.org/download and extract it to a local directory. Set a GRAILS_HOME environment variable to that directory and add GRAILS_ HOME/bin to your path. (You also need a valid JAVA_HOME environment variable.) Now you're ready to go!
A Web App in the Blink of an Eye
To create a new Grails application, type:
$ grails create-app AutoMart
Now change to the AutoMart directory and create a domain class:
$ grails create-domain-class Car
Open AutoMart/grails-app/domain/Car.groovy and edit it, like so:
class Car {
String make
String model
Integer year
}
Save this file, and run:
$ grails generate-all Car
Create-app and create-domain-class are Grails scripts. To see what other scripts are provided by Grails, run grails help from the command line.
You now have a complete working web application, with pages for creating, displaying, editing and listing Car instances. You can launch it with:
$ grails run-app
Grails runs on port 8080 by default. You can easily run on a different port like this:
$ grails -Dserver.port=9090 run-app
Figure 1: Create Car
Navigate to http:// localhost:8080/AutoMart and look around. Figure 1 and Figure 2 show a couple of the views Grails gives us 'out of the box.' You can also leave it running while you continue to develop. Just save your changes and refresh your browser. This rapid feedback is one of the strengths of Grails.
Figure 2: Car List
Grails Conventions
In our example the Car.groovy file that Grails created for us was placed in a directory called grails-app/domain. This is one of the many conventions in Grails. Placing source files in certain directories and naming them in certain ways can make magical things happen in a Grails application.
Domain Classes
Placing a Groovy class file in the grails-app/domain directory will turn it into a persistent domain class. Several properties and methods will be added to the class dynamically, and Grails will create a table based on the name of the class, with fields for each property.
Controllers
Grails controllers are simple Groovy classes with names ending in 'Controller' and residing in the grails-app/controllers directory. Controllers also receive several methods and properties dynamically. Any closure defined as a property in a Controller will become an action reachable by a URL in the following form: application/controller/action.
Views
For each controller in your application there will be a directory under grails-app/views/ named after the controller class (ie. grails-app/views/car). This directory is where your views (.gsp files) go. When a controller action is completed it will automatically attempt to render a view with the name of the action. So, when you call http://localhost:8080/AutoMart/car/list the list action will execute and render the list.gsp page. You can, of course render specific pages but convention can be a huge time-saver.
Services
A Groovy class with a name ending in 'Service' and residing in the grails-app/services directory becomes a Grails service and has built-in transaction handling and more.
TagLibs
Creating custom tags in Grails is so easy it should be illegal. Just create a Groovy class ending with 'TagLib' and place it in the grails-app/TagLib directory. Then define a closure property and write to the OutputStream called 'out' that is already given to you. You can also use the tag's attributes and body by simply declaring them.
class YourTagLib{
def saySomething = {attrs, body ->
if (attrs.tone == 'loud')
out << body().toUpperCase()
elseif (attrs.tone == 'quiet')
out << body().toLowerCase()
else
out << body()
}
You can use this tag in a .gsp like this:
<g:saySomething tone='loud'>
I'm shouting now!
</g:saySomething>
You can use this tag in a .gsp like this:
<g:saySomething tone='loud'>
I'm shouting now!
</g:saySomething>
I'M SHOUTING NOW!
There are no extra classes to create, no interfaces to implement, no TLDs to create. You could probably take up a new hobby with the time you'll save!
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}