Grails workshops
-
Upload
lukasz-tenerowicz -
Category
Software
-
view
75 -
download
7
Transcript of Grails workshops
itsilesia.com e: [email protected]
Disadvantages of web apps in Java ?
● Hard configuration (e.g. XML soup )● Large amounts of boilerplate code ( vs.
scripting languages like PHP, Python)
itsilesia.com e: [email protected]
Grails = No disadvantages of Java
● Convention over configuration - like in RoR
● Groovy as a primary language○ Short syntax○ Easy! ○ Functional elements
● Configuration also in Groovy
itsilesia.com e: [email protected]
… + its advantages
● Lots of libraries○ Spring ( DI, Transactions, Spring Security )○ Hibernate○ Quartz○ Lots of APIs ( e.g. Amazon Web Services Java
SDK)● Deployment via .war file● ~20 years of JVM development
itsilesia.com e: [email protected]
About Grails
● version 1.0 in 2008● Looks similar to Ruby on Rails
○ MVC○ convention over configuration○ console
● Based on Spring MVC, Hibernate● Used in LinkedIn, Netflix, Sky TV,
Vodafone
itsilesia.com e: [email protected]
Folder structuresource : https://grails.org/Quick+Start
Our first task will be ...
Hello World!What else ? :D
itsilesia.com e: [email protected]
Controller
● Class name and method name maps to an URL○ /hello/index○ /hello/show
itsilesia.com e: [email protected]
Controller - parameters
● URL parameters are mapped to method arguments
● Also accessible from params map
itsilesia.com e: [email protected]
Hello world!
● render(“Hello world”)
● render(“Hello “ + user)
● Views! ${user}
● render(view: ‘index’, model: [user: ‘user’])
● return [user: ‘user’]
● redirect(action: ‘index’)
● GSP -> g:if, g:each
But that’s all static HTML...Give me the DB!
Domain classesWhat our application is all about
itsilesia.com e: [email protected]
Domain classes
● Mapped to database via GORM ○ Class is table, property is column
● Constraints ○ nullable, unique, blank, size, ...
● Relations○ hasMany, belongsTo
● Properties
itsilesia.com e: [email protected]
Domain classes - constraints
itsilesia.com e: [email protected]
Domain classes - relationships
itsilesia.com e: [email protected]
Domain classes - properties
itsilesia.com e: [email protected]
Domain class: finders and other functions
● Person.findByName(‘asdf’)○ Person.findByNameAndAge(‘asdf’, 16)○ Person.findAllBy...And…
● Person.save()○ Person.discard()○ Person.refresh()
Data bindingPowerful!!!
itsilesia.com e: [email protected]
Databinding of a domain class
itsilesia.com e: [email protected]
Validation
● Domains and all classes with @Validateable
● Occurs on obj.validate() or during databinding
● If any constraints are violated, error codes are added to obj.errors map
● Those errors can be rendered via i18n codes
itsilesia.com e: [email protected]
Internationalisation - i18n
● Based on codes found in /grails-app/i18n/messages.properties
● Tags : <g:message code=”com.some.code” />● So yes, we can render errors too!
○ <g:message error=”${object.errors}” />
Dependency InjectionEasier than ever!
Other featuresMaybe next workshops :P
itsilesia.com e: [email protected]
Other features
● REST controllers - http://grails.org/doc/latest/guide/webServices.html
● Spring Security - http://grails.org/plugin/spring-security-core
● Database migrations - http://grails.org/plugin/database-migration
Thank you!Questions ?