Grails - CRUD
-
Upload
ritesh-gupta -
Category
Documents
-
view
244 -
download
0
Transcript of Grails - CRUD
-
8/3/2019 Grails - CRUD
1/16
Grails
Creating CRUD applications
-
8/3/2019 Grails - CRUD
2/16
Example Application:
Books Wish List Book is added to the wish list
Book from the wish list can be bought or
refused Simple CRUD application with only one
domain object
-
8/3/2019 Grails - CRUD
3/16
What will be presented
Domain object
Controller
View (gsp groovy server pages)
-
8/3/2019 Grails - CRUD
4/16
What is Domain Object?
Represents part of the domain problem
Mapped to database
Database is automatically generated Groovy class extended with useful methods
GORM Grails Object Relations Mapping
-
8/3/2019 Grails - CRUD
5/16
Book as Domain Object
-
8/3/2019 Grails - CRUD
6/16
What to Notice?
Domain object is mapped to database table
Properties are mapped to table columns
Constraints are used to validate input(minSize, maxSize, inList, blank,)
Error messages due the constraints areconfigurable according to convention
Domain class is by convention created indirectory domain
-
8/3/2019 Grails - CRUD
7/16
How to Create Domain Class?
Generate Grails application with:grails create-app
Create domain class with:grails create-domain-class
Code the domain class
-
8/3/2019 Grails - CRUD
8/16
What is Controller?
Controller handle requests and createsresponses
Controller is request based
Each request is mapped with closure
If not specified differently, closure incontroller is mapped to view (gsp)
-
8/3/2019 Grails - CRUD
9/16
Book Controller
Following closures are automatically generated fordomain class controller:
y index displays list of all domain objects
y list displays list of all domain objectsy show shows details of the domain object
y delete deletes the domain object
y edit opens page for editing of domain object
y update updates changes made to domain objecty create opens page for domain object creation
y save saves the domain object
-
8/3/2019 Grails - CRUD
10/16
What to Notice?
Generated closures are for CRUD operations
Use of dynamic methods on domain objects(save, hasErrors, get, list)
Renderingand redirecting
Name of the controller by convention is:Controller
Controller is by convention created indirectory controllers
-
8/3/2019 Grails - CRUD
11/16
How to Generate Controller for
Domain Class? Generate controller using script:grails generate-controller
Make changes to controller if necessary (forsimple CRUD applications you can leave itas is)
-
8/3/2019 Grails - CRUD
12/16
What is View (gsp)?
In Grails views are gsp groovy serverpages
gsp is similar to jsp orasp but much more
flexible
Data models provide data to view
gsp page consists of markup (html) andgsp
tags (e.g.g:remoteLink,g:paginate,)
-
8/3/2019 Grails - CRUD
13/16
Book Views
Following views are automatically generatedfor domain class:
y create
y edit
y list
y show
-
8/3/2019 Grails - CRUD
14/16
What to Notice?
Generated views are for CRUD operations
Accessing to model data with ${..} construct
Views are by convention generated in thedirectory views/
Actually functionality and layout of page isgood out of the box and for internalapplication can be reused withoutmodifications
-
8/3/2019 Grails - CRUD
15/16
How to Generate Views for Domain
Class Generate views using script:
grails generate-views
Make changes to views if necessary (forsimple CRUD applications you can leave itas is)
-
8/3/2019 Grails - CRUD
16/16
And for the end
Questions?
Oh, yes you can run the application withgrails run-app
And access application onhttp://localhost:8080/