Groovy On Grails

  • View
    1.785

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Transcript

  • 1. Groovyon Grails Groovy - , Java, Java Python ,Ruby Smalltalk . , HelloWorld: println"Hello world!"// ";"

2. defage = 20 defsimpleStr ='My age is $age'//- "My age is $age" definterpStr ="My age is $age"//- "My age is 20" defmultiline =''' First line Second line Third line ''' 3. Ranges defrng = 1..10 rng. size ()// rng. contains (5)// rng. from // rng. top // 4. defsomeList = [1,2,3,4]// defemptyList = []// someList[0]// someList[5] = 6// someList a + b } defdo = { println"done"} defforEvery3rd( list, block ) { defi = 0 for( einlist ) { if( i % 3 == 0 ) block( i ) i ++ } } 8.

  • , :
  • it . . :
  • def pr = { println it }// ,
  • this ,
  • owner , . - this, , , .
  • delegate , owner, , Builders

9. Currying C urring , . : defadd = { a, b -> a + b }// - defadd1 = add.curry( 1 )// - Expando. , , def obj =newExpando(); obj.a = 1// obj.b = 2// obj. do= { println"done" }// 10. If classGreet { defname Greet(who) { name = who[0].toUpperCase() + who[1..-1] } defsalute() { println"Hello $name! } } g =newGreet( 'world' )// create object g.salute()// Output "Hello World! def x=1 if (x=2) printlnx=2 // Exception 11.

  • .
  • S yntax-sugar .
  • .
  • Template Engine.
  • Alias.
  • , final.
  • safe navigation .
  • Duck - typing .
  • Currying .
  • G p ath .
  • Ant .
  • XML RPC & GroovyWS & Gdata & Jabber RPC .
  • GroovyMock & JUnit & TestNG .
  • Groovy Scriptom Groovy.
  • Grails!

12. 13. Grails

  • Grails -, Groovy
  • -, Java .
  • , , .
  • , .
  • , :
    • ( ).
    • (view), GSP.
    • -.
    • Ajax , .
  • , .
  • , - .

14. Grails Controller. classTripController { ... deflist = { if(!params.max) params.max = 10 [ tripList: Trip.list( params ) ] } } defsave = { deftrip = new Trip(params) if(!trip.hasErrors() && trip.save()) { flash.message ="Trip ${trip.id} created" redirect( action:show, id:trip.id ) } else { render( view: 'create' , model:[trip:trip] ) } } 15. Grails Views. < g:eachin ="${tripList}"status= "i"var= "trip" > ${ trip.id?.encodeAsHTML() } g:each > 16. Grails Model. classAirline {staticconstraints = { name (blank:false, maxSize:100) url (url:true) frequentFlyer (blank:true) notes (maxSize:1500)} statichasMany = [trip:Trip] Stringname Stringurl StringfrequentFlyer Stringnotes StringtoString(){ return name } } 17. Grails TagLib. classDateTagLib { defthisYear = { out