Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

Grails

Creating CRUD applications

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

What will be presented


Domain object Controller View (gsp groovy server pages)

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

Book as Domain Object

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 are configurable according to convention Domain class is by convention created in directory domain

How to Create Domain Class?


Generate Grails application with: grails create-app Create domain class with: grails create-domain-class Code the domain class

What is Controller?
Controller handle requests and creates responses Controller is request based Each request is mapped with closure If not specified differently, closure in controller is mapped to view (gsp)

Book Controller

Following closures are automatically generated for domain class controller:


y index displays list of all domain objects y list displays list of all domain objects y 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 object y create opens page for domain object creation y save saves the domain object

What to Notice?
Generated closures are for CRUD operations Use of dynamic methods on domain objects (save, hasErrors, get, list) Rendering and redirecting Name of the controller by convention is: <domain_class_name>Controller Controller is by convention created in directory controllers

How to Generate Controller for Domain Class?


Generate controller using script: grails generate-controller <domain_class> Make changes to controller if necessary (for simple CRUD applications you can leave it as is)

What is View (gsp)?


In Grails views are gsp groovy server pages gsp is similar to jsp or asp but much more flexible Data models provide data to view gsp page consists of markup (html) and gsp tags (e.g. g:remoteLink, g:paginate,)

Book Views

Following views are automatically generated for domain class:


y create y edit y list y show

What to Notice?
Generated views are for CRUD operations Accessing to model data with ${..} construct Views are by convention generated in the directory views/<domain_class_name> Actually functionality and layout of page is good out of the box and for internal application can be reused without modifications

How to Generate Views for Domain Class


Generate views using script:


grails generate-views <domain_class_name>

Make changes to views if necessary (for simple CRUD applications you can leave it as is)

And for the end


Questions? Oh, yes you can run the application with grails run-app And access application on http://localhost:8080/<project_name>

You might also like