Grails is a framework for simplifying the complicated java web development. You can find more details about Grails in here.
Don't waste our time, let's jump into a real example. Now, we are creating a web application with crud feature. Before we started, just make sure you have installed grails framework. If not installed yet, this must be a good resource for installation.
Next, open your console/terminal. Go to your projects folder then type this command to create new grails application.
grails create-app crud-webapp
It's mean to create a new application with app name "crud-webapp". Next, go to that newly created application folder.
cd crud-webapp
Type this command to enter grails interactive console.
grails
In this console type help to view all available grails command.
| Available Commands (type grails help 'command-name' for more info):
| Command Name Command Description
----------------------------------------------------------------------------------------------------
assemble Creates a JAR or WAR archive for production deployment
bug-report Creates a zip file that can be attached to issue reports for the current project
clean Cleans a Grails application's compiled sources
compile Compiles a Grails application
console Runs the Grails interactive console
create-controller Creates a controller
create-domain-class Creates a Domain Class
create-functional-test Creates a Geb Functional Test
create-integration-test Creates an integration test
create-interceptor Creates an interceptor
create-scaffold-controller Creates a scaffolded controller
create-script Creates a Grails script
create-service Creates a Service
create-taglib Creates a Tag Library
create-unit-test Creates a unit test
dependency-report Prints out the Grails application's dependencies
generate-all Generates a controller that performs CRUD operations and the associated views
generate-async-controller Generates an asynchronous controller that performs CRUD operations
generate-controller Generates a controller that performs CRUD operations
generate-views Generates GSP views for the specified domain class
gradle Allows running of Gradle tasks
help Prints help information for a specific command
install Installs a Grails application or plugin into the local Maven cache
install-templates Installs scaffolding templates that use f:all to render properties
list-plugins Lists available plugins from the Plugin Repository
open Opens a file in the project
plugin-info Prints information about the given plugin
run-app Runs a Grails application
schema-export Creates a DDL file of the database schema
shell Runs the Grails interactive shell
stats Prints statistics about the project
stop-app Stops the running Grails application
test-app Runs the applications tests
url-mappings-report Prints out a report of the project's URL mappings
The first step for creating CRUD web application is creating a domain (in MVC it's called Model). Just type this command.
create-domain-class com.djamware.Employee
That command will create new domain class named "Employee" inside package name "com.djamware". Edit that Employee class in folder crud-webapp/grails-app/domain/com/djamware/Employee.groovy and fill with required data fields like this.
package com.djamware
class Employee {
String name
String email
String position
Double salary
Date created = new Date()
static constraints = {
email email: true
}
}
Let me explain a little bit. This domain has 5 fields name, email, position, salary and created with different data types. Inside constraints are validation, this time the only email that has validation must be a valid email. By default, if no constraints defined, fields will be a required field.
Next, generate a controller and views by this command.
generate-all com.djamware.Employee
This command will create CRUD controller and views for Employee domain. Now, run your app by this command.
run-app
Open this URL http://localhost:8080 in your browser, and this is default grails view.
You see employee controller in the left bottom of the page. Just click on it and you will see this page.
In this CRUD page, just click "New Employee" button to create new employee data.
In that employee, form fills data but don't fill as right data to test built in fields validation. Fill email with the invalid email, left the name blank and fill another data. Then click "Create" button, you will see validation works like this.
If you filled this form correctly data will be saved and you will see the detail of employee.
Click on "Employee List" button to go back to employee page. You will see new data listed.
Now, you can try another CRUD function by yourself.
You might be asking how and where data saved. Yes, by default Grails 3 use the in-memory database using HSQLDB. You can find this configuration in file crud-webapp/grails-app/conf/application.yml. This line is database configuration by default using h2 or HSQL with help of hibernate as ORM or translator Object to the Database table.
hibernate:
cache:
queries: false
use_second_level_cache: true
use_query_cache: false
region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
dataSource:
pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password:
For different database configuration, you can change those lines.
That it's for now.
Thanks