building web applications with grailschariotsolutions.com › wp-content › uploads › ... ·...
TRANSCRIPT
![Page 1: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/1.jpg)
Building Web Applications
Fall Forecast 2009
27
![Page 2: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/2.jpg)
Agenda
• The Grails Framework • Components
– Domains – Controllers – Views - Groovy Server Pages
28
![Page 3: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/3.jpg)
The Grails Framework
Some embedded
frameworks:
Spring Spring MVC
Spring Webflow Hibernate
Criteria API Sitemesh Quartz
29
![Page 4: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/4.jpg)
Key Grails Classes
• Domain Class – A class representing an object in your
domain (database)
• Controller – A class that operates on URLs submitted
to the web site
• View – A Groovy Server Page (GSP) designed
to render the content based on a specific request
30
![Page 5: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/5.jpg)
Grails Controllers
• Analogous to a Struts Action • Backed by Spring Controllers • Each method handled by the Controller
is a closure
grails generate-controller com.chariot.Customer
31
![Page 6: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/6.jpg)
Scaffolding
• Allows quick structure of your application
• Typically used for quick demo or prototype
• Controller code:
32
class CustomerController { def scaffold = Customer }
![Page 7: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/7.jpg)
Scaffold @ Runtime
• Views Generated • Actions Generated
– list – show – edit – delete – create – save – update
33
![Page 8: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/8.jpg)
Controller Methods
def save = { def customerInstance = new Customer(params) if (customerInstance.save(flush: true)) { flash.message = "${message(code:
'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])}"
redirect(action: "show", id: customerInstance.id) } else { render(view: "create", model: [customerInstance:
customerInstance]) } }
34
![Page 9: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/9.jpg)
3 R’s of Controller Methods
• Return (not required) – returns to view with the same name as
closure • Render
– Renders specified content – Simple text, views or templates
• Redirect – Redirects one action to another – via HTTP redirect
35
![Page 10: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/10.jpg)
Returning from a Controller
• “return” keyword is optional
• Returns a map of data to the page return [ customer : cust ]
• No return map? – Grails returns controller
properties 36
class MyController { List myVar1 List myVar2
def list = { myVar1 = MyObj1.list() myVar2 = MyObj2.list() } }
![Page 11: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/11.jpg)
View
• A Groovy Server Page rendered in response to a Controller action
• Use Groovy tag libraries to handle form input, iteration, conditions, etc...
• Expected View Locations – /grails-app/views/domainname/
viewname 37
![Page 12: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/12.jpg)
Generating Views
• Provides a starting point for you
• For both controller & all views
38
grails generate-views com.chariot.Customer
grails generate-all com.chariot.Customer
![Page 13: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/13.jpg)
Grails Workflow - Domain Driven
• Build domain class – apply constraints & relationships
• Build a controller • Build views • Code tests • Customize controllers & views • Focus on the data first! (not the UI)
39
![Page 14: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/14.jpg)
Groovy Server Pages
• Grails GSP Features – Expression Syntax (EL) – Grails Tag Libraries for forms, links,
iteration, etc. – Support for Templates
40
![Page 15: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/15.jpg)
Grails GSP and the GSP EL
• You may refer to variables using EL Syntax • Example using g:each GSP tag:
41
<html> <body> <ul> <g:each in="${myValues}" var="value"> <li>${value}</li> </g:each> </ul> </body> </html>
![Page 16: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/16.jpg)
GSP Tags
Tag Use Examples <g:if> <g:elseif> <g:else>
Conditional Logic
<g:if test="{editAllowed == true}"> <!-- render form --> </g:if> <g:elseif test="{viewAllowed == true}"> <!-- render view mode --> </g:elseif> <g:else> <!-- render access denied --> </g:else>
<g:each> Iterating a collection.
Default var name is 'it'
status : the iteration #
<g:each in="${orders}" var="order" status="i"> Order #${i}: Quantity: ${order.quantity} Cost: ${order.cost}<br/> </g:each>
42
![Page 17: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/17.jpg)
Tags: Formatting Text/Numbers
GSP Tag Use Examples
<g:formatNumber> Replaces DecimalFormat
<g:formatNumber number="${salary}" format="\$###,##0.00"/>
<g:formatDate> Replaces SimpleDate Format
<g:formatDate date="${new Date()}"> format="MM/dd/yyyy hh:mm:ss a"/>
43
![Page 18: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/18.jpg)
GSP Form Tags
• <g:form> tag defines a form – action – the controller action (optional) – controller – the controller base name
(optional) – name – the id and name to set in the
form tag – id – the ID to use in the link (if saving
an existing domain element, for example)
44
![Page 19: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/19.jpg)
The Form Itself
• Expurgated from the generate-all code
• Notice the multiple buttons via g:actionSubmit and the embedded javascript validate
• One calls update(), the other delete()
45
<g:form method="post">
... form fields here ...
<g:actionSubmit value="Update" /><br/>
<g:actionSubmit onclick= "return confirm('Are you sure?');" value="Delete" />
</g:form>
![Page 20: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/20.jpg)
Customizing Templates
• You can install the templates used for scaffolding – Customize them – Your scaffold classes use the modified
templates
• Install templates with: grails install-templates
• Installs templates in src/templates
46
![Page 21: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/21.jpg)
Scaffolded GSPs
• grails-app/views/domain/ – create.gsp – edit.gsp – list.gsp – show.gsp
• References the base page via meta tag: <head> <meta name="layout“ content="main" /> …
47
Name of the layout gsp
![Page 22: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/22.jpg)
Customizing Templates
• Modify the main gsp that is used: grails-app/views/layouts/main.gsp
• Weaves in your page content at runtime
<g:layoutHead/>
<g:layoutBody/>
48
![Page 23: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/23.jpg)
Navigation Plugin
• grails install-plugin navigation
• In main.gsp
• In Controller configure menu options:
static navigation = [ [action:'list', order: 0, isVisible: true], [action:'create', order: 1, isVisible: true] ]
49
<body> … <div id="menu"> <nav:render/> </div> <g:layoutBody/>
<head> … <nav:resources/>
![Page 24: Building Web Applications with Grailschariotsolutions.com › wp-content › uploads › ... · Grails Workflow - Domain Driven • Build domain class – apply constraints & relationships](https://reader035.vdocument.in/reader035/viewer/2022081402/5f125a91bff0802f02376048/html5/thumbnails/24.jpg)
Summary
• Grails is a web MVC platform (Domains, Controllers, Views)
• Grails is backed by Hibernate, Spring MVC, and other technologies
• Controllers handle the flow of the application • GSPs provide the view technology with
customizable templates
50