Introduction
This is part 1 in what will hopefully be a series of examples using Grails. Like most of the other lessons on this site, the purpose of these examples are to demonstrate the steps in building working applications. Although I'll often make some brief comments, the lessons aren't meant to provide detailed explanations of the parts that make up Grails. For thorough coverage pick up a book and check out the online documentation. The only book I've read so far is The Definitive Guide to Grails by Graeme Rocher (I found it quite excellent.)

For those familiar with the other tutorials on this site, I'm keeping with the "Employee Maintenance" theme that the others follow.
Before you get started
If this is your very first grails application, you'll need to download grails and set up your GRAILS_HOME environment variable. These initial steps are described well here. (After following the steps described in the previous link, open up a terminal and type grails to make sure you have everything set up correctly. You should see "Welcome to Grails" as part of the first line.)

You could also follow along looking at the source code provided but I suggest not downloading the source and actually follow the steps below instead so that you get a better feel for the ease of development with Grails.
Creating our project skeleton
Using a terminal (command line), navigate to the directory where you work on your projects. Once in this directory type: grails create-app grails-employee. You'll notice a lot of directories and files set up for you in a new directory called "grails-employee." Already this is sort of nice, since there is no need to worry about how to set up your project or worrying about creating your own Ant build file. Grails also comes with a built-in HSQL database and comes using Jetty as an application server. Of course, we could configure our application to use another database and it's one command to create a war file that we can deploy to a different application server. For this demo, however, we'll just use the default setup.
Create our domain objects
We'll assume we don't have an existing database already set up and populated. Grails uses Hibernate by default and as we build domain objects and save and edit them we'll actually be persisting them to the underlying database without touching any SQL. For this simple application we're going to work with two domain objects, an "Employee" and a "Department." We'll start by building a Department domain object.

If you open up your 'grails-employee' project you just created in the last step, you'll find a directory: 'grails-app/domain.' We can simply start by either creating a domain class here by using our IDE or editor and naming it "Department.groovy" or we could go to the terminal and within our grails-employee directory type: grails create-domain-class Department. After entering this command, a "Department.groovy" class will be created automatically in the grails-employee/grails-app/domain directory. Navigate to this class, or edit the one that you created with your editor, and have the class look like:
class Department { 
    String name;
    
    String toString() { 
        return name;
    }
}
Next, let's create our Employee domain object (Employee.groovy) in the same grails-app/domain directory (using the same procedure as you did for the Department domain object.) This domain object will contain a reference to a Department object. Create your Employee.groovy class as such:
class Employee { 
    Department department;
    String firstName;
    String lastName;
    int age;
    
    static constraints = {
        firstName(blank:false, nullable:false, size:1..150) 
        lastName(blank:false, nullable:false, size:1..150)  
        age(min:18,max:120)
    }
   
    String toString() {
        return "$firstName $lastName (id: $id)"
    }
}
Populate our database
Since we're running in dev mode, we'll just use the grails-app/conf/BootStrap.groovy class to populate our database. (If you take a look at the grails-app/conf/DataSource.groovy file, you'll notice that in development mode the dbcreate mode is set as dbCreate = "create-drop" which means that the tables will be dropped and recreated on startup.) In the BootStrap init method we'll just create some instances of the domain objects we just created and call "save()" on them which will persist them to the database. The following initialization code will create three Departments then create fifty Employees populated with some random information.
Open up the BootStrap.groovy class and modify to look like:
class BootStrap {

     def init = { servletContext ->
        
         Department a = new Department( name: "HR").save();
         Department b = new Department( name: "Accounting").save();
         Department c = new Department( name: "Sales").save();
         def departments = [a, b, c]
         def firstNames = ["Greg","Fred","Doug","Craig","Dustin","Steve","Kristy","Rick","Wilma"]
         def lastNames = ["Smith","Flinstone","Abbot","Williams","Adams","Goober","Brady","Jones","Heffernen"]
         Random random = new Random()
         1.upto(50) { i ->  
            def first = firstNames[ random.nextInt(9)]
            def last = lastNames[ random.nextInt(9)] + i
            def dep = departments[ random.nextInt(3)]
            def age = random.nextInt(40)+18
            new Employee( firstName: first, lastName: last, age: age, department: dep).save();
         }
         assert( Employee.list().size() == 50 ) 
     }
     def destroy = {
     }
} 
Create Employee controller
From the terminal (still in our grails-employee directory) type: grails create-controller Employee. Grails will create the controller called "EmployeeController.groovy" and it will be placed in the grails-app/controllers directory. Open up that file and you'll see it looks like:
class EmployeeController {
    def index = { }
}
'index' will be the default action executed in this controller which, of course in this case, doesn't do anything. We'll be modifying this controller shortly.
Dynamic Scaffolding
If you want a quick and usable application to now handle all your CRUD operations for an Employee we can do it simply now by using just one line of code! They key is in dynamic scaffolding. I'll quote Graeme Rocher's explanation on scaffolding:
Scaffolding is the name coined by the Rails framework for the generation of the artifacts that make up a usable interface to perform CRUD operations. This term has been fruther extended and generalized to basically mean the generation of an interface to fufill a use case based on the convention within a project (Graeme Rocher 'The Definitive Guide to Grails' p.89)
Let's demonstrate an example of dynamic scaffolding by modifying the "EmployeeController.groovy" file you just created in the previous step. Open up that file and change it to look like:
class EmployeeController {
    def scaffold = Employee
}
Now lets run the application. From the terminal (still in the grails-test directory we created) type:
grails run-app
Open up your browser, and you should be able to navigate to http://localhost:8080/grails-employee.

The screen should look like:


You could then click on the "EmployeeController" link and view the employees...



You can sort the columns by clicking on the column headers and you can show the employee detail and edit the employee after clicking on the employeeId in the left-hand column.

Quite impressive - From one line of code in our EmployeeController we end up with a fully functional CRUD application! (You might notice that if you go to edit an employee and try to edit the department, you'll get a 'page not found' error. This is because we didn't create a controller with a scaffold definition for the Department domain object. You certainly can create that controller if you wish (just like we did for an Employee), which would then allow you to modify the Deaprtments as well.)

This scaffolding is pretty cool, but often times you'll want to modify the views (the GSP pages) and you'll often need to provide some added functionality to the controllers. We'll get to this in the next two sections.