Download pdf - Grails Overview

Transcript
Page 1: Grails Overview
Page 2: Grails Overview

 WhatisGrailsandwhyshouldIcare?

  BuildingonJava’sstrengths

  TheGroovylanguage

  AtourofGrails

  AncillarytoolsusefulinGrails©2009DevJam‐Allrightsreserved.

Page 3: Grails Overview

 Webapplicationdevelopmentenvironment  BuiltontheJavaPlatform  Groovyastheprimarylanguage

  JavacanalsobeusedandintermixedwithGroovyseamlessly

  FavorsConventionoverConfiguration  SimilarinspirittoRubyonRails

©2009DevJam‐Allrightsreserved.

Page 4: Grails Overview

  Javawebdevelopmentcanbetedious  TheJVM,classlibraries,andframeworksaretherealstrengthoftheJavaPlatform

  GroovyisanexcellentdynamiclanguagefortheJVM  CompilestoJavabytecode(.classfiles)

  Favorconventionoverconfiguration  Grailsisanopinionatedframework

  Enhancesproductivity©2009DevJam‐Allrightsreserved.

Page 5: Grails Overview

  BuiltontopoftheJavaVirtualMachine Maturity  Optimizationandperformance  Runtimefamiliarity

  IntegrationwithJavaapplications  GroovyclassesareJavaclasses  ReusewhatworksfromtraditionalJavastack▪  JavaEE:ServletAPI,JDBC,JMS▪  Hibernate,SpringFramework,SpringMVC,SiteMesh

©2009DevJam‐Allrightsreserved.

Page 6: Grails Overview

©2009DevJam‐Allrightsreserved.

Page 7: Grails Overview

  Hibernate  Object/relationalmapping(ORM)

  SpringFramework  Dependencyinjectionservices  Applicationlifecyclefacilities

  SpringMVC Webframework

  SiteMesh  Userinterfacelayoutandthemingfacilities

©2009DevJam‐Allrightsreserved.

Page 8: Grails Overview

  Grailsconventiondictates… Wherecomponentsresideintheapplication  Howthecomponentisnamed  Howthecomponentiswiredandcollaborateswithotherapplicationcomponents

  Howapplicationscanbeextendedthroughtheuseofthird‐partyplugins

©2009DevJam‐Allrightsreserved.

Page 9: Grails Overview

  Standardizedconfiguration  Log4j,databasedatasources  Autowiringanddependencyinjectionofcomponents

  Standardizedbuild  Gantscripts

  Standardizeddomainmodel  GORMfacilitiesinjectedintodomainobjects

  Standardizedservices  Automatictransactionsupport

©2009DevJam‐Allrightsreserved.

Page 10: Grails Overview

  FeaturesinspiredfromPython,Ruby,andSmalltalk

  CanbecompiledtoJavabytecode  GroovyclassesareJavaclasses

  GroovycanseamlesslyuseJavaclasses  JavacanseamlesslyuseGroovyclasses*

  Lotsofnewlanguagesfeatures  GPath,GString,internaliterators,closures,Meta‐ObjectProtocol(MOP),andmanyothers

©2009DevJam‐Allrightsreserved.

Page 11: Grails Overview

  JDBCisusedforcommunicatingwithdatabases

  HSQLDB:defaultdatasource  DataSource.groovy

  ConfigurationofDataSourcesforallyourenvironments

  Easytoswitchtoadifferentdatabase,evenacrossdifferentdeployments

©2009DevJam‐Allrightsreserved.

Page 12: Grails Overview

  DomainclassesarecentraltoaGrailsapplication

  Locatedingrails‐app/domain  Allattributespersistentunlessmarkedinthetransientsproperty

  Nogettersandsettersnecessary  Validators:specifiedinconstraintsproperty  GORMmapping:customizedinthemappingproperty

©2009DevJam‐Allrightsreserved.

Page 13: Grails Overview

  GrailsObjectRelationalMapping  LeveragesHibernate

  Automaticallymapsdomainobjectstodatabase

  Providesqueryandupdatefacilities  Findingobjectsinthepersistencestore(findBy…methods).

  Persistencelifecyclemethods(save,update,delete).

  CriteriaandHQLqueryfacilities.©2009DevJam‐Allrightsreserved.

Page 14: Grails Overview

  Noneedtoextendacommonpersistentbaseclass  GrailsinjectsGORMfunctionalityintoobjectsatrun‐time.

  Declarepropertiesforyourdomainobjects  Theidorversionpropertiesareinjectedautomatically.

  Allpropertiesarenotnull/requiredbydefault

©2009DevJam‐Allrightsreserved.

Page 15: Grails Overview

  Associations  One‐to‐one,one‐to‐many,many‐to‐many  Uni‐directionalandbi‐directional  OwnersdefinedbyusingbelongsToproperty

  Inheritancehierarchies  Table‐per‐hierarchy:Allclassesmaptoasingletable

  Table‐per‐subclass:EachclassmapstoitsowntableandJOINsareused

©2009DevJam‐Allrightsreserved.

Page 16: Grails Overview

  Retrievingobjects  get(), getAll(), read()

  Listingobjects  list(), listOrderBy*() methods  order, sort, offset, andmaxnamedarguments

  Dynamicfinders(findBy*())  Usepropertynamesoftheclass  Supportformanyexpressionsinfindermethod

©2009DevJam‐Allrightsreserved.

Page 17: Grails Overview

  BuiltonHibernate’sCriteriaAPI  Groovybuilderisusedtobuildupthecriteria

  Hierarchyofmethodcallsandclosuresforbuildingtree‐likestructures

  EachdomainclasshasacreateCriteria()method  Callget,list,scroll,orcountoncriteria  Thecriteriabuilderisusedtorestrictthequery

  Supportsassociationsandprojections©2009DevJam‐Allrightsreserved.

Page 18: Grails Overview

  SupportforHibernateQueryLanguage  find(hql), findAll(hql), andexecuteQuery(hql) forqueries

  executeUpdate(hql)forDML‐styleopera3ons(setsofupdatesordeletes)

  Supportforpositionalandnamedparameters

©2009DevJam‐Allrightsreserved.

Page 19: Grails Overview

  HandlesHTTPrequestsanddeterminesrenderingresponsibility

 Manyobjectsareavailabletoyourcontrollerclosures  log,params,request,response,session,servletContext,flash

  Interceptorsonacontroller  beforeInterceptorandafterInterceptorclosures

©2009DevJam‐Allrightsreserved.

Page 20: Grails Overview

  Fileupload/multipartrequests  Automaticbindingtodomainobjects  <g:uploadForm />

  Binaryresponsewriting  response.outputStream  response.contentType   Overloaded<<operatortoappendcontent

  RequestInputStreamreading  def incomingText = request.inputStream.text

©2009DevJam‐Allrightsreserved.

Page 21: Grails Overview

  BuiltuponSpring’svalidationframework  org.springframework.validationpackage

  Domainobjectshavevalidationbakedin  save()andvalidate()methodstriggervalidation  Validationerrorspopulateerrorspropertyondomainobject

  Errors  Interrogatethedomainobjectinstance  hasErrors()methodanderrorsproperty

©2009DevJam‐Allrightsreserved.

Page 22: Grails Overview

  SimilartoJSPs  UtilizesGPathandGStringsandothercoolGroovylanguagefeatures

  AutomaticdiscoveryofGrailstaglibs  Supportfortemplates

  Smallsnippetsofviewcodethatotherviewscanuse

  UsefulforAJAXresponses

©2009DevJam‐Allrightsreserved.

Page 23: Grails Overview

  Grailsshipswithastandardsetoftaglibs  Iteration,filtering,switching,logicaltaglibs  HTMLmarkupcreation:links,forms,formfields,datepicker

  Validationerrorcheckingandrendering  Paginatingviews  Viewtemplaterendering(reusablesub‐views)

  Customtaglibsareeasy  GroovyclasswithaTagLibsuffixandresidesingrails‐app/taglibdirectory

©2009DevJam‐Allrightsreserved.

Page 24: Grails Overview

  GrailsusesPrototypelibrarybydefault   EasilychangedtoanotherJavaScriptproviderthroughGrailspluginsystem

  GrailstaglibsinsulateyoufromtherawJavaScript  Remotelinks,remoteforms,remoteformfields,beforeandaftercallbacks,eventhandling,effects,animations

©2009DevJam‐Allrightsreserved.

Page 25: Grails Overview

  Grailshasbuilt‐insupportformanagingmessagebundles,locales

 Messagefilesaremanagedinthegrails‐app/i18ndirectory

  Grailstaglibcanbeusedtoretrievemessagesfrommessagebundles  Supportsparameterizedmessaging

  SupportforspecialURLmappingstosupportencodinglocaleinformationintheURI

©2009DevJam‐Allrightsreserved.

Page 26: Grails Overview

  Allowsforcentralizationofapplicationbehavior  Necessarycomponentinanynon‐trivialGrailsapplication

  Transactionalbydefault,setthroughthetransactionalproperty

  Usetheexposepropertytoexposeaserviceviaremotingtootherprocesses  Usewithvariousremotingplugins

©2009DevJam‐Allrightsreserved.

Page 27: Grails Overview

  ReflectionandtheGroovyMetaClassareusedtoscaffoldCRUDfunctionality

  Enableinthecontrollerclass…

class UserController {

def scaffold = User … }

©2009DevJam‐Allrightsreserved.

Page 28: Grails Overview

  Extensionmechanismforintegratingthird‐partyfunctionality

  About80orsopluginstodayandmanymorecomingonboard  AJAX,JavaScript,Flex/Flash,UIwidgets  Security,Crypto,CAPTCHA,CAS  Databasemigration,codecoverage,functionaltesting

 Manyothers…findatgrails.org/plugin/list©2009DevJam‐Allrightsreserved.

Page 29: Grails Overview

  SwingGUIconsole  AllowsexecutionofarbitraryGroovystatements

  YourGrailsdomainmodelisaccessibleinthisenvironment  GORMfacilitiesareavailable  Greatwaytotinkerandplaywithyourdomainobjectmodel

©2009DevJam‐Allrightsreserved.

Page 30: Grails Overview

  Eclipse  Pluginavailable,butthestoryisnotgoodatthemoment

  NetBeans  GroovyandGrailssupportinversion6.5

  IntelliJIDEA  JetGroovypluginbyJetBrainsoffersgoodsupportforGrails

  Seemstobetheleaderatthemoment

©2009DevJam‐Allrightsreserved.

Page 31: Grails Overview

  TakeGrailsonatestdrive!  grails.org

  Books  GroovyinAction  TheDefinitiveGuidetoGrails,SecondEdition  BeginningGroovyandGrails  GrailsinAction  GroovyandGrailsRecipes  GrailsPersistencewithGORMandGSQL

©2009DevJam‐Allrightsreserved.

Page 32: Grails Overview

CoachingandDevelopingAgility

www.devjam.com

©2009DevJam‐Allrightsreserved.