Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Download and unzip grails.zip from the Grails site: www.grails.org and extract into
/opt/grails
2. Set GRAILS_HOME environment variables into above installation directory, by using this
console command : $ export GRAILS_HOME=/opt/grails;
4. run : “grails help”, if installation was success. It will grails help command
1. Download and unzip grails.zip from the Grails site: www.grails.org and extract it to
C:\grails
2. Set GRAILS_HOME environment variables into above installation directory, open the
Right Click My Computer->System properties ->Environment variables
3. Add to GRAILS_HOME/bin to the path, the way to add in is the same as above.
4. run grails help, if installation was success. It will grails help command
Datasource.groovy
dataSource {
pooled = true
driverClassName = "org.hsqldb.jdbcDriver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// 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:postgresql://192.168.10.18:5432/dongrails"
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
username = "dxndba"
password = "dxnoke"
}
}
}
As like you see on above. The setting variables that you have to fill in is :
driverClassName="org.postgresql.Driver", the postgresql jdbc library driver, you must have this
postgresql-8.3-604.jdbc4.jar file. You can get this file from www.postgresql.org
class Student {
String Nama
String Sex
String address_1
String nationality
static constraints={
nama(blank:false)
sex(inList:["Male","Female"])
address_1(length:10..50)
nationality(inList:['Australia','Indonesia','Malaysia','Japan'])
}
static searchFields = ['nama', 'sex', 'address_1']
}
then save. After that run “grails generate-all student”. This executed command will create Controller
and view for you automatically. To see the result, run the grails application, using “grails run-app”
class Course {
static hasMany=[students:Student]
String courseName
Date startDate = new Date()
}
As you see above, to make Course relate to student in one to many relation. We just add this line :
static hasMany=[students:Student]
class Student {
String Nama
String Sex
String address_1
Course course
String nationality
static constraints={
nama(blank:false)
sex(inList:["Male","Female"])
address_1(length:10..50)
nationality(inList:['Australia','Indonesia','Malaysia','Japan'])
}
After that, you have to re-generate all the course and the student domain
That's all how to create application with basic CRUD (Create, read, update and delete ).
Appendix:
A. Command reference
create-app This command creates a Grails application and requires the user to
specify the application name. A subdirectory within the directory
the command was executed from is then created based on the
entered application name.
Usage : grails create-app [name]
create-controller The create-controller command will create a controller and
associated integration test for the given base name.
Usage : grails create-controller book
create-service
The create-service command will create a Grails service class for
the given base name.
usage: grails create-service book
create-domain-class The create-domain-class command will create a domain
and associated integration test for the given base name.
Usage: grails create-domain-class book
generate-all Generates a controller and views for the given domain class
usage : grails generate-all Book
B. More resource :
• http://www.grails.org/Tutorials
• Screencast : http://www.grails.org/Grails+Screencasts
• http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=mastering+grails