Saturday, July 30, 2011

GRAILS Tutorial -- Head Start

In this post I will make a sample application. I have also provided download option at the end of the post in  zip format.

If this is the first time you are looking into grails then I would suggest you to go through my blog post here and make sure that your grails setup is up and running.

In this sample I will show you how to create a domain class, a controller and also basic CRUD operation using GRAILS scaffolding feature. I will be using grails console for giving all commands and eclipse for doing my code editing.

We will make a sample application for a school. let's say SchoolApp. First step for starting a GRAILS application is creating an application using the create-app command. so go ahead and type the below command.

grails create-app SchoolApp 

As soon as you execute this command grails will create complete application structure for you. Now that we are done with our app creation we will go to our first domain class.

We will create a domain class named Student. To do this go into the SchoolApp directory and type the below command.

grails create-domain-class Student

Once done you should find two files created.
  • Student.groovy (grails-app/domain)
  • StudentTests.groovy (test/unit)
The Student.groovy file will have below piece of code.
class Student {
   static constraints = {
Lets add some fields to our Student class.
class Student {

    Integer rollNumber;
    String name;
    Date dob;
    Integer age;    
I have added 4 fields to our domain class. Also note that I have removed the static constraints section as it was not of any use to us as of now.

We will create a controller for our Student class. Execute the below command to create a controller.

grails create-controller Student

Again you should find two files created.
  • StudentController.groovy (grails-app/controllers)
  • StudentControllerTests.groovy (grails-app/unit)
The StudentController.groovy will have below piece of code.
class StudentController {

  def index = { }    
Now, we will add scaffolding feature to our controller.
class StudentController {

  def Scaffold = Student    
So, that's it. Our CRUD for Student class is ready for use. I have also removed the def index = { } from the generated code as again it is not needed.

Next, we are going to run our app. To do this we will execute the below command.

grails run-app

In case you get something like below.

Server failed to start: Address already in use

It means that your port 8080 is already been used by some other service. So you will need to change your port to say 9090. To do this kindly go through my post here.

If things go well then you should receive a message like below.

Server running. Browse to http://localhost:8080/SchoolApp

Now, go and hit to the url above in your browser. You will get below page with you controller named StudentController.

When you click on the StudentController you will be able to do CRUD for Student class.

Grails comes bundled with HSQLDB. In case you want to change it to MYSQL or you want to get some information about various database settings and environments within grails, then you can go through my datasource post here.

The download zip of the code is here.

Once you download the zip file, extract it and go inside the SchoolApp folder. Then do a grails run-app as shown above.

In my next post I will show you how to do Relational Mappings, views of grails (GSP's)  etc. Stay tuned.

If you have any suggestions or if you are having any problem in running this sample application, Kindly leave a message and I will get back to you at the earliest.

No comments:

Post a Comment