Groovy Grails Quick Start Groovy Getting Started Grails Define Groovy

Quick Tutorial to run Groovy On Grails:

The following makes it simple to start a grails project with step wise demonstration.

Quick Start Steps :

  1. Create a Grails project
  2. Configure a Data Source (Optional)
  3. Create a Domain Class
  4. Create a controller
  5. Create Views(Optional)
  6. Start Grails

Step : 1 Create a Grails project

Once you have installed Grails you can use the built-in target for creating new projects:

grails create-app project-name

The project structure below:

%PROJECT_HOME%
    + grails-app
       + conf                 ---> location of configuration artifacts
           + hibernate              ---> optional hibernate config
           + spring                 ---> optional spring config
       + controllers          ---> location of controller artifacts
       + domain               ---> location of domain classes
       + i18n                 ---> location of message bundles for i18n
       + services             ---> location of services
       + taglib               ---> location of tag libraries
       + util                 ---> location of special utility classes
       + views                ---> location of views
           + layouts              ---> location of layouts
   + lib
   + scripts                  ---> scripts
   + src
       + groovy               ---> optional; location for Groovy source files
                                   (of types other than those in grails-app/*)
       + java                 ---> optional; location for Java source files
   + test                     ---> generated test classes
   + web-app
       + WEB-INF

Step : 2 Configure a Data Source (Optional)

The “create-app” command creates a DataSource.groovy file with default contents .

Each data source is configured with an in-memory HSQLDB database ,which is the default configuration.The code looks like :

dataSource {
  pooled = false
  driverClassName = "org.hsqldb.jdbcDriver"
  username = "sa"
  password = ""
}
// environment specific settings
environments {
  development {
    dataSource {
      dbCreate = "create-drop" // one of 'create', 'create-drop','update'
      url = "jdbc:hsqldb:mem:devDB"
//    loggingSql = true
    }
  }
  test {
    dataSource {
      dbCreate = "update"
      url = "jdbc:hsqldb:mem:testDb"
    }
  }
  production {
    dataSource {
      dbCreate = "update"
      url = "jdbc:hsqldb:file:prodDb;shutdown=true"
    }
  }
}

Configuring the data source is a simple matter of changing the values for the desired database and driver and placing the driver jar file in the <..>/lib directory. Properties set in the dataSource node are inherited by the children.

Step : 3 Create a Domain Class

AT first please go to the project directory.

cd my-project
grails create-domain-class ClassName

A domain class is a persistent object and all properties are by default persisted to the database

class ClassName {
    String Course

}

Step : 4 Create a controller

Controllers are request dispatchers in Grails applications they handle web requests and URLs of the request map to a controller class and a closure within the class.

grails create-controller ClassName

Class will be created by name ClassNameController.groovy in path GRAILS_HOME/grails-app/controllers/my/project. You can edit it with your favorite text editor or IDE

Open up this controller and change it as follows to use dynamic Scaffolding which dynamically generates your application at runtime:

class ClassNameController {
     def scaffold = ClassName
}

Alternatively, you could also have run “grails generate-all“, which creates all the scaffolding for you, and left the generated controller alone, instead of replacing it with the default scaffolding. It might be worth learning from.

Step : 5 Start Grails

To start your Grails app run the following target

grails run-app

This will startup an instance of the Jetty servlet engine running on port 8080. To access the list of domain class records open up a browser and type:

http://localhost:8080/project-name/ClassName/list

Or, as the “list” closure is the default action for the ClassNameController you can type:

http://localhost:8080/project-name/ClassName
          Note:

To increase response times during developing try increasing your maximum heap size by setting the JAVA_OPTS environment variable to something like: ‘-Xmx512m’ – this will set the maximum heap size to 512Mb when running Grails hence resulting in  improvement in response.

Number of View :1347

Tags:

  1. No comments yet.

SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache