1 R. Volz - Groovy & Grails Groovy & Grails Lightning Talk Raphael Volz Sep. 10, 2007 FZI, Karlsruhe Builds on content from the following presentations Rod Cope and James Strachan The Groovy Programming Language Presentation held at JavaOne 2004 Graeme Rocher Rapid Web Application Development with Grails Presentation held at JavaOne 2006
2-3 hour introduction to GROOVY and GRAILS given on Sep 10, 2007 for an internal tech talk at the FZI, Germany
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1 R. Volz - Groovy & Grails
Groovy & Grails Lightning Talk
Raphael Volz
Sep. 10, 2007
FZI, Karlsruhe
Builds on content from the following presentations
Rod Cope and James StrachanThe Groovy Programming LanguagePresentation held at JavaOne 2004
Graeme RocherRapid Web Application Development with GrailsPresentation held at JavaOne 2006
2 R. Volz - Groovy & Grails
Agenda
Why should I care ?
Get Groovy
Find Grails
3 R. Volz - Groovy & Grails
Why should I care ?
40-60%User Interface
40-60%Functionality
Software Development Effort
Variance Max. 20 %User Interface
Min. 80%Functionality
Focus :Users Great Interface
Developer Gigantic Functionality
Typically Research
… try to be efficientin programming efforts …
... try to be efficientin UI efforts...
4 R. Volz - Groovy & Grails
Efficiency Problems in Web Development
Many repetitive tasks Consider example data flow:
Database – Object Representation – Presentation HTML
Different structure of individual applications Hard to understand Hard to maintain
Difficulty to get started Many concepts at once:
HTML, XML, SQL, JavaScript, Programming Languages …
Simplified addressing of list elements avoids calls to len()
Simplied modification of lists
Simplified iteration
17 R. Volz - Groovy & Grails
Groovy // List Handling Example
public class Filter { public static void main( String[] args ) { List list = new java.util.ArrayList(); list.add( "Rod" ); list.add( "James" ); list.add( "Chris" ); Filter filter = new Filter(); List shorts = filter.filterLongerThan( list, 4 ) for ( String item : shorts ) { System.out.println( item ); } } public List filterLongerThan( List list, int length ) { List result = new ArrayList(); for ( String item : list ) { if ( item.length() <= length ) { result.add( item ); } } return result; }}
Select all names with at most 3 characters from a list
Groovy
Java
list = ["Rod", "James", "Chris"]shorts = list.findAll { it.size() <= 4 }shorts.each { println it }
18 R. Volz - Groovy & Grails
Groovy // Ranges and Maps
def sum = 0for (i in 1..10) sum += iassert sum ==55
Maps Operators to Methods Can be implemented by any class and subsequently used Operator Name Method
a+b plus a.plus(b)a-b minus a.minus(b)a*b star a.multiply(b)a/b divide a.div(b)a % b modulo a.mod(b)a++ increment a.next()a-- decrement a.previous()a**b power a.power(b)
22 R. Volz - Groovy & Grails
Groovy // Advanced switch
switch (v) { case 1: println 'Number one'; break;
case 'test': println 'String test'; break;
case 2..15 : println 'in range 2 to 15'; break;
case [1, '1', 'One'] : println 'Number one'; break;
case ~/A.*/: println 'Starts with A'; break;
default: println 'Default Output'; break}
23 R. Volz - Groovy & Grails
Groovy // Additional Library
Adds methods missing from the JDK String
contains(), count(), execute(), padLeft(), center(), padRight(), reverse(), tokenize(), each(), etc.
• Spring MVC under the hood• Support for flash scope between requests• GSP: Groovy alternative to JSP• Dynamic taglib development: no TLD, no configuration, just conventions• Adaptive AJAX tags (Yahoo, Dojo, Prototype)• Customizable layout with SiteMesh• Page fragments through reusable templates• View under grails-app/views
VM C S J
TIP: Use grails install-templates to install and customize your the templates to be used
• URL mapping convention: controller/action/id http://localhost:8080/library/book/show/1 • Scaffolding can be ▪ dynamic (def scaffold = true) ▪ static (code generation)• Controllers pass data to the view through maps• Direct access to parameters• Easy redirect and forward• Can define allowed methods for each action
VM C S J
53 R. Volz - Groovy & Grails
GRAILS // Controllers provide actions to execute (call)
class BookController { def index = { redirect(action:list,params:params) } def list = { [ bookList: Book.list( params )] }
def show = { [ book : Book.get( params.id ) ] }
def edit = { def book = Book.get( params.id ) if(!book) { flash.message = “Book ${params.id} not found” redirect(action:list) } else return [ book : book ] }}
VM C S J
54 R. Volz - Groovy & Grails
GRAILS // Chaining and Flashing
Source: G. K. Rocher, The Definite Guide to Grails, Apress, 2006
55 R. Volz - Groovy & Grails
GRAILS // Services
• Services are Groovy classes that should contain your business logic
• Automatic injection of services in controllers & services simply by declaring a field:
class BookController { MySuperService mySuperService }
VM C S J
56 R. Volz - Groovy & Grails
Grails // Jobs
• You can create recuring events with Quartz under the hood, configured by Spring• Again a convention on the name and directory• Regular intervals, or cron definitions