Last updated by
5 years ago
Page: Quick Start, Version:0
Quick Start
The following makes it simple to start a grails project. There's also a screencast that follows these steps for creating a small app. Create a Grails project
Once you have installed Grails you can use the built-in target for creating new projects:grails create-app
%PROJECT_HOME%
+ grails-app
+ conf ---> location of configuration artifacts like data sources
+ 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 (e.g., codecs, etc.)
+ 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-INFConfigure a Data Source (Optional)
The "create-app" target created a Grails data source artifact for you in the "<..>/grails-app/conf" directory called DataSource.groovy with closures for each of the standard environments: Development, TestData, and Production. All the examples that follow operate on the development environment. For more information on environments see Configuration#environments.By default, each data source is configured with an in-memory HSQLDB database (great for testing, but probably not that useful for live deployment) so this step is optional: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"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:file:prodDb;shutdown=true"
}
}
}Create a Domain Class
Make sure you are in the root directory of your project (for argument sake "my-project") by typingcd my-projectgrails create-domain-class
class Book {
String title
String author
}class BootStrap { def init = { servletContext ->
// Create some test data
new Book(author:"Stephen King",title:"The Shining").save()
new Book(author:"James Patterson",title:"Along Came a Spider").save()
}
def destroy = {
}
}Create a controller
Controllers are central to Grails applications they handle web requests and URLs of the request map to a controller class and a closure within the class.Run the "grails create-controller" target and type in the name of the controller. In our example we type "Book" which generates a controller called {{grails-app/controllers/BookController.groovy}}. Open up this controller and change it as follows to use dynamic Scaffolding which dynamically generates your application at runtime:class BookController {
def scaffold = Book
}Start Grails
To start your Grails app run the following targetgrails run-app
http://localhost:8080/my-project/book/list
http://localhost:8080/my-project/book