alphageeks il #2 groovy & grails

28
Groovy & Grails (also) the future of Java Web Development Yuval Goldstein, [email protected] , @yuvalgo

Upload: alphageeks

Post on 10-May-2015

1.126 views

Category:

Technology


2 download

DESCRIPTION

Second Alphageeks IL meetup:Yuval Goldstein talks on Groovy & Grails and the shift from static typing to dynamic typing.Check us out for more events:http://alphageeks.blogli.co.il/

TRANSCRIPT

Page 1: Alphageeks IL #2 Groovy & Grails

Groovy & Grails(also) the future of Java Web Development

Yuval Goldstein, [email protected],

@yuvalgo

Page 2: Alphageeks IL #2 Groovy & Grails

Groovy, Grails, Agenda

Beyond Java What’s groovy? What’s grails? How people use them?

Page 3: Alphageeks IL #2 Groovy & Grails

Beyond Java

Java= Language + Classes +JVM JEE = Robust spec to support server-

driven apps The good: OO, popular, tons of

frameworks, enterprise ready (queues, transactions, web-services)

The Bad: Verbose, long turn-around, mix and match your own framework

Page 4: Alphageeks IL #2 Groovy & Grails

Beyond Java

Java code > compiled into bytecode > JVM

The JVM supports other languages:› Groovy› Scala› JRuby› Jython

But WHY? : Because not every problem should be solved by a general purpose language

Page 5: Alphageeks IL #2 Groovy & Grails

Groovy

Cool language, compiles into bytecode Similar to Java, a lot of additions and

improvements:› Language level support for collections› Closures and Builders› Dynamic typing› Concise syntax› Groovy2Java, Java2Groovy integration

Page 6: Alphageeks IL #2 Groovy & Grails

Demo

Let’s take a Java class and show how it’s done in Groovy

Page 7: Alphageeks IL #2 Groovy & Grails

Closures A piece of code that can be passes

around and executed (sort of an Anonymous class with a method)

Def foo = { name -> println “Hello ${name}” }foo(“yuval”) // prints “Hello yuval”

Page 8: Alphageeks IL #2 Groovy & Grails

More Closures

public class TaxCalculator {

def calculateTax (salary, age, strategy) { return (salary * 0.5) + strategy(age) } static void main(String[] args) { def goldenAgeDiscount = { age -> age * 2 } TaxCalculator c = new TaxCalculator() println c.calculateTax(10000, 30, goldenAgeDiscount) }

Page 9: Alphageeks IL #2 Groovy & Grails

Demo

Let’s see some Collections and Closures working together

Page 10: Alphageeks IL #2 Groovy & Grails

Groovy typing

Types can be specified and enforced Types can unspecified and deducted

(Duck Typing)

Page 11: Alphageeks IL #2 Groovy & Grails

Duck Typing

Behavior of objects is determines by their own methods and members rather than by declarations

def x = "im a string" println x.toUpperCase() def y = 3 println y*y // 9 def z = 2.0 println z+1 // 3.0 println z.toUpperCase() // Cast Exception

Page 12: Alphageeks IL #2 Groovy & Grails

Duck Season? - Demo

Let’s see how duck typing works

Page 13: Alphageeks IL #2 Groovy & Grails

Duck Typing

Sacrifice certainty for laziness, WTF?› Some application really need strict typing,

other doesn’t› Frameworks generate a lot of code for you

on runtime› Faster code changes› Good testing really helps› You already doing it:

HTML, CSS, SQL, JSP

Page 14: Alphageeks IL #2 Groovy & Grails

Expando Classes

def cat = new Expando() cat.name = "pocket" println cat.name cat.sayHello = { println "miewww!" } cat.sayHello() }

Output:

PocketMiewwww!

Page 15: Alphageeks IL #2 Groovy & Grails

String Matching

=~ searches for matches (returns a java.util.regex.Matcher)==~ is there a match? (returns a Boolean)~ returns a Pattern

def match = "yuval" ==~ /.*uv.*/ println match

Page 16: Alphageeks IL #2 Groovy & Grails

Introducing Grails

Grails=Groovy + Spring + Hibernate + Sitemesh

A framework for developing web-apps with pleasure

Open-Source, supported by Spring-Source Convention over configuration Model-driven Short turnaround Good developer-testing support A lot more…

Page 17: Alphageeks IL #2 Groovy & Grails

Demo

Create an empty application Develop a domain classes Generate a trivial CRUD : dynamic,

then static

Page 18: Alphageeks IL #2 Groovy & Grails

Domain Objects and GORM GORM: Hibernate DSL 1:1, 1:N, M:N, Object hierarchies,

Versioning, Cascade, Fetch Objects are DAOs Automatic Query Syntax:

› Object.save()› Object.get(id)› Object.findAll()

def result = Clip.findAllBySourceAndBeginAtBetween( vs, fromDate, toDate )

Page 19: Alphageeks IL #2 Groovy & Grails

Controllers Each closure maps to a request name

Render templates, GSP, XML, JSON Automatic variables: param, session,

flash Support Spring Webflow Dependency Injection support (for

services)

http://myserver/[APP_NAME]/[Controller_NAME]/[Request]

Page 20: Alphageeks IL #2 Groovy & Grails

Views

GSPs, taglibs, Templates HTML centric Built in Tags for: Forms and error

handling , AJAX, Pagination, uploads Pluggable Ajax support: Prototype, YUI,

others Plugins: Grails-UI, Flex, GWT, Open

Lazzlo, Portlet integration

Page 21: Alphageeks IL #2 Groovy & Grails

Services Help re-use code, scope transaction,

expose web-services Different Scopes: singleton, session,

prototype, request, conversation, flow Dependency Injection support Plugins: JMS integration, Quartz Jobs,

Xfire/Spring web-services, etc…class MyService{

boolean transactional = true def otherservice // Injected}

Page 22: Alphageeks IL #2 Groovy & Grails

Testing support

Built-in Unit and Integration testing Good mockup support Can’t do without it

Page 23: Alphageeks IL #2 Groovy & Grails

(Only some) Plugins

GrailsUI Acegi Security Twitter Flex Quartz Searchable

Page 24: Alphageeks IL #2 Groovy & Grails

Groovy, Grails in the wild Groovy:

› As a scripting tool for developers› For testing plain java code› For developing swing code (special DSL)› For coding grails application

Grails:› Prototypes and back-office applications› Grails Frontend with Java backend› Grails frontend and backend

Page 25: Alphageeks IL #2 Groovy & Grails

Community and support

Open-Source, use for free Commercial training and support by spring Grails user newsgroup on Nabble:

40-80 messages a day

IDEs: Eclipse, Intellij, Netbeans, TextMate

Books:Groovy in action, Grails in Action, Definitive Guide to Grails, Getting started with Grails, Groovy & Grails Recepies, more…

Sites:grails.org, aboutgroovy.com, grailspodcast.com, grailstutorials.com

Page 26: Alphageeks IL #2 Groovy & Grails

Who use Grails?

Linkedin.com Sky.com twitcaps.com blerp.com twitsms.co.uk Ganttzilla.com Lot’s of community, e-commerce and

twitter oriented sites

Page 27: Alphageeks IL #2 Groovy & Grails

Summary

Grails is ideal for small and medium size application

Grails ~= Rails capabilities + JVM + All existing Java frameworks

0 Setup development environment Fun to use

Page 28: Alphageeks IL #2 Groovy & Grails

Thanks !