You are on page 1of 8

Build Application With Grails

Installation For Linux

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;

3. Add to $GRAILS_HOME/bin to the path, the command is : export PATH=


$PATH:/opt/grails

4. run : “grails help”, if installation was success. It will grails help command

Installation For Windows

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

create application “MyCourses”

1. In project directory , type grails create-app MyCourses

2. It will generate folder Mycourses


3. If you want to change the database you can edit datasource.groovy in the folder ../grails-
app/conf

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"
}
}
}

In datasource.groovy there's 3 environment setting. Development ,test and production


In above source code when you set environment into production the grails application will
automatically connect to postgresql server on ip address 192.168.10.18 , the default environment in
grails is development. So if you run grails run-app, it will use the development environment. To select
the production environment, you must use this command : “grails prod run-app”

As like you see on above. The setting variables that you have to fill in is :

url="jdbc:postgresql://192.168.10.18:5432/dongrails", this will let the grails application connet


to 192.168.10.18 port 5432 and with database name : dongrails. Please note you have to create the
database name in the server. But you don't need to create the tables structure, the grails will be
automatically create it for you.

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

dialect = org.hibernate.dialect.PostgreSQLDialect, let the grails use the postgresql syntax

username = "dxndba" , username of the postgresql server.


password = "dxnoke", the password.

4. Create student domain class , grails create-domain-class student.


This will create a groovy file named student.groovy in folder domain.
Open with text editor :

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”

Create one to many relationship

We want to have like this

course 1:* Student

One courses can be participated by many of students

First we have to create domain class course

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]

in class domain student we add this line : Course course


here is the full source :

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

picture of generated student list

picture : create a student record

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

help Displays Grails command line help


usage : grails help
grails help run-app

generate-controller Generates a controller for the given domain class


usage : grails generate-controller Book

generate-views Generates a set if views for the given domain class


usage : grails generate-views Book

generate-all Generates a controller and views for the given domain class
usage : grails generate-all Book

run-app Runs Grails uses an embedded Jetty container on port 8080


usage : grails run-app
grails test run-app
grails -Dserver.port=8090 -Denable.jndi=true -
Ddisable.auto.recompile=true run-app
war The war command will create a Web Application Archive (WAR)
file which can be deployed on any Java EE compliant application
server
Usage: grails war
grails test war
grails -Dgrails.env=foo war

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

You might also like