In this article I would like to introduce Grails framework using a practical example and a way of generating the application based on a model called scaffolding. We will configure our work environment step by step and run our first application. It will serve as an entry point to further work with Grails.
Grails is a framework based on Groovy language. It introduces the “convention over configuration” principle. The dynamic behaviour of Groovy together with integrated Java solutions (we use the same virtual machine) allow us to develop quickly not only a prototype but a fully functional application. Adding DSL (Domain Specific Language) to that and we will get an excellent package to work with. What is worth mentioning is the fact that Grails is not reinventing the wheel. Some similarities to other frameworks (not only Java based) can be observed, but “under the hood” we can find widely used solutions: Spring, Hibernate, Jetty, HSQLDB, SiteMesh. Learning curve is very beneficial for almost every programmer, regardless of experience and abilities. Let us check it out in practice.
In order to start working with Grails we have to spend a little time preparing the environment. Nice surprise, it only takes a moment. What do we need? I suggest using NetBeans 6.5 and Grails 1.1. This set gives us an environment to work without any additional installation and configuration. We download NetBeans 6.5 (http://netbeans.org) and Grails 1.1 (http://grails.org). NetBeans has by default the ability to create Grails projects. The package with Grails contains, besides the framework and Groovy, Jetty web server, HSQLDB database and other necessary components (Hibernate, Spring, SiteMesh). The only thing we have to do after installing NetBeans and extracting Grails to any location, is point NetBeans to this location. We select Tools -> Options from the menu, Miscellaneous -> Groovy tab and fill out the Grails Home field.
After preparing the environment it is time to create our first project. We select File -> New Project… from the menu and then Groovy -> Grails Application. We type AddressBook as the project name (we will prepare a simple address book). We click Finish and we are ready. Our first Grails project can be run. We can do it by clicking the Play button or pressing the F6 key. A welcome screen will appear that assures us we did everything correctly.
Let us start implementing our address book. We will create the model in the beginning. The classes of the model are called domain classes in Grails. A domain class is an object representation of an entity from a database. From the context menu of the project we choose New -> Grails Domain Class…, type the name Contaxt and click Finish. A new class called Contact.groovy is created in grails-app/domain directory. There we can define attributes which describe a contact from our address book. To make things simple we will define 4 attributes: name, phone, email and address. Our class should look like below:
class Contact { String name String phone String email String address static constraints = { } }
To create a working application we will need a controller, because Grails uses the MVC pattern. In order to do that, select New -> Grails Controller… from the project context menu, use Contact as the name and click Finish. There will be a new class ContactController.groovy created in the grails-app/controllers directory (surely you have noticed that NetBeans shows these directories as “Controllers” and “Domain Classes”). The newly created controller has a default action – index, but we will use a Grails feature which is called scaffolding.
The scaffolding feature allows us to generate user interface and business logic to perform basic CRUD operations on our data (Create, Read, Update, Delete). In order to do that we have to modify our controller to look like this:
class ContactController { def scaffold = true }
Now we are ready to run the application again (Play or F6). This time we should see a welcome page with all available controllers. Only ContactController will be available in our case. After selecting it we should see an empty list of contacts as well as an icon allowing us to add a new one. We can give it a try right now. Our application is not very complex but it is fully functional. The one thing that the application lacks is data validation. We can use the built-in validation which works well together with scaffolding. To define the constraints let us add constraints section in our domain class. It is used to define not only what values could the attributes have but also order them in the generated application. The constraints section should look like:
static constraints = { name(blank:false) phone(matches:"^\\+*\\d+") email(email:true) }
The order of the attributes in this section will be used on the screen displaying the details of the record, the list view and on the add/edit forms. We define that the name attribute cannot be empty. You should remember that blank can be used only for text values and states whether the value may be an empty string. There is a nullable constraint as well, but it states if the value may be null. In case of the phone attribute we use a regular expression to validate it (an optional plus sign at the beginning and one or more digits). For the email attribute we use a built-in e-mail address validation. Plain addresses are not validated, that is why we do not put this attribute in the constraints section (we should if we wanted to change its order). After running the application again we can test the validation. The validation messages are not very clear (especially for the phone number), but it works.
Grails and Groovy have been very popular recently, so everyone interested in them should not have any problems finding information about them, other programmers’ experiments, tips and add-ons. Below is a short list of links which could be used as a starting point:
Official Grails website – http://grails.org
Official Groovy website – http://groovy.codehaus.org
Jacek Laskowski website – http://jaceklaskowski.pl (in polish)
Chlebik’s website – http://chlebik.wordpress.com (in polish)
You can visit my blog http://tech.mrozewski.pl in order to discuss Grails, find some useful information e.g. contact.
Translation: Paweł Cegła
Source: http://www.javaexpress.pl/article/show/Wprowadzenie_do_Grails