Creating Grails App
Before we can create an Grails application, we need to install Java Development kit in our computer by typing
sudo apt-get install oracle-java6-installer
(this will also fixed the JAVA_HOME was NOT set error in your terminal, if you have installed the GVM first.)
after installation we can now create our grails application by typing
grails create-app name
ex. grails create-app sampleProject
then cd sampleProject
then to run the grails project simply type grails run-app
then browse to http://localhost:8080/sampleProject/
you will now able to see your first grails app.
Configuring Database to use mySQL as default Database
By default Grails uses H2 as its database but we can change this by simply going to your Grails Project then open grails-app/conf/
directory then open DataSource.groovy
change
dataSource { pooled = true driverClassName = "org.h2.Driver" username = "sa" password = "" }
to
dataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" username = "root" password = "" }
and change all
dbCreate = "update" url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
to
dbCreate = "update" url = "jdbc:mysql://localhost/testing"
Then open BuildConfig.groovy
find and uncomment the line runtime ‘mysql:mysql-connector-java:5.1.16’
NOTE: dbCreate dictates what grails will do to the database, these are the following definition you can use:
create-drop – Drop and re-create the database when Grails is run
create – Create the database if it doesn’t exist, but don’t modify it if it does. Deletes existing data.
update – Create the database if it doesn’t exist, and modify it if it does exist
-Both create-drop and create will destroy all existing data-
MVC Model View Controll
Grails uses MVC architecture pattern to implement user interface
Syntax in creating Domain (Model)
grails create-domain-class className
ex. grails create-domain-class book
Syntax in creating Controller
grails create-controller controllerName
ex. grails create-controller bookController
For creating the View
we could simply create a file with .gsp extension name inside the “projectName”/Grails-app/views directory
ex. books.gsp
Basic Grails/Groovy Syntax
just like html tags grails uses opening and closing tags
example of grails form
<g:form controller="Persons" action="save" class="grails-form"> <label>First Name: <g:textField name="firstName"/>
<label>Last Name: <g:textField name="lastName"/>
<label>Age: <g:textField name="age"/>
<g:actionSubmit value="Save"/> </g:form>
example of grails input field
<g:textField name=“lastName”/>
example of grails submit button
<g:actionSubmit value=“Save”/>
example of looping data to display database content
<table border="1"> <g:each in="${viewRecords}" status="i" var="thisRecord"> <tr> <td>${thisRecord.id} <td>${thisRecord.firstName} <td>${thisRecord.lastName} <td>${thisRecord.age} </tr> </g:each> </table>
Basic Controller Functions
Inserting Data to MySQL Database
def save() { def users = new Persons(params) users.save() render "Success!" redirect(action:"view") }
Pulling data from MySql database
def view = { def viewUsers = Persons.list(); [viewRecords: viewUsers]; }
Deleteing data from MySql database with if else statement
def delete = { def deleteUser = Persons.get(params.idNum) if(!deleteUser){ render "User Id don't exist!" }else{ deleteUser.delete() render "Deleted!" } }