Home > Uncategorized > Grails tutorial – How to create and run First Application in Grails

Grails tutorial – How to create and run First Application in Grails

I am hoping you would have gone through Introducing Grails and then the Part 1 of this post Setting Up Grails .

Now I am covering the details of developing and running the application

1. Go to command prompt and your grails home directory.
2. Create the application.

  • The create-app command creates the full project, with a template with placeholders for the different components of your application such as configuration, MVC, library, and much more.
  • > grails create-app   (it will ask for a application name say – first_app)
  • Will create basic  view layout as main.gsp page

3. Add the Business Logic and Model( Domain Classes)

  • The domain class is the core of the business application; it contains the state and behavior of your application.
  • > cd first_app
  • > grails create-domain-class  (enter name say – first;you need to use the same name for the class and the table you want to map your object on.)
  • The domain class is located in the following location:./first_app/grails-app/domain/First.groovy

class First{ // A blank class will be created on create-domain
/*can add vars you don’t need to worry about creating the getters and setters*/
String name
Date birthdate

4. Create the different screens from the domain class.

  • > grails generate-all (For creating all screens-enter domain class name – first)
  • This command creates the different Views and Controllers; you can take a look to the directories: ./grails-app/controllers &. /grails-app/views

class FirstController {//will create <domainname>Controller
// with some default closures
def list = {…}

FirstController also gets lots of new and useful dynamic methods whose names are handily self-explanatory:

  • save() saves the data to the table in the HSQLDB database.
  • delete() deletes the data from the table.
  • list() returns a list.
  • get() returns a single record
  • create()
  • update().

Also generates the 4 view gsp pages to render the output on screen.

5.      Run the Application

Grails provides a way to run the application in standalone mode (run-app). This command starts a Web container (based on Jetty) with the application deployed.

> grails run-app  and go to http://localhost:8080/first_app/ to see it running.

Categories: Uncategorized Tags: