Grails workshops

26
Introduction to Łukasz Tenerowicz [email protected] [email protected]

Transcript of Grails workshops

Page 1: Grails workshops

Introduction to

Łukasz [email protected]

[email protected]

Page 2: 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)

Page 3: Grails workshops

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

Page 4: Grails workshops

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

Page 5: Grails workshops

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

Page 6: Grails workshops

itsilesia.com e: [email protected]

Folder structuresource : https://grails.org/Quick+Start

Page 7: Grails workshops

Our first task will be ...

Page 8: Grails workshops

Hello World!What else ? :D

Page 9: Grails workshops

itsilesia.com e: [email protected]

Controller

● Class name and method name maps to an URL○ /hello/index○ /hello/show

Page 10: Grails workshops

itsilesia.com e: [email protected]

Controller - parameters

● URL parameters are mapped to method arguments

● Also accessible from params map

Page 11: Grails workshops

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

Page 12: Grails workshops

But that’s all static HTML...Give me the DB!

Page 13: Grails workshops

Domain classesWhat our application is all about

Page 14: Grails workshops

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

Page 15: Grails workshops

itsilesia.com e: [email protected]

Domain classes - constraints

Page 16: Grails workshops

itsilesia.com e: [email protected]

Domain classes - relationships

Page 17: Grails workshops

itsilesia.com e: [email protected]

Domain classes - properties

Page 18: Grails workshops

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()

Page 19: Grails workshops

Data bindingPowerful!!!

Page 20: Grails workshops

itsilesia.com e: [email protected]

Databinding of a domain class

Page 21: Grails workshops

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

Page 22: Grails workshops

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}” />

Page 23: Grails workshops

Dependency InjectionEasier than ever!

Page 24: Grails workshops

Other featuresMaybe next workshops :P

Page 25: Grails workshops

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

Page 26: Grails workshops

Thank you!Questions ?