SlideShare a Scribd company logo
Groovy & Grails Scripting for Modern Web Applications Rohit Nayak Talentica Software
Agenda Demo: Quick intro to Grails Scripting, Web Applications and Grails/Groovy REST service in Grails Demo Internals Web Client in Grails Demo Internals Perspective
Demo Quick Intro to Grails
Maggi grails create-app Maggi Domain class: Noodle, Packaging grails generate-all Noodle, Packaging grails run-app
Web Frameworks with Scripting Ruby on Rails (2004) CakePHP (2005) Django / Python (2005) Groovy on Grails (2006)
Power of these frameworks Baked Experience The Language Agility / Productivity
Baked Experience Model View Controller  Object-Relational Mapping Templates Layout URL rewriting Ajax support XML / JSON support
The Language Dynamic  More expressive code Smaller code Native support for Lists, Hashmaps Lang. support for IO, Net, XML Idioms for common usage
Agile Scaffolding Unit tests No build cycles Built-in webservers Fail faster!
http://www.zacker.org/ruby-on-rails   (Nov 2nd)
http://www.zacker.org/ruby-on-rails   (Nov 2nd)
HelloWorld.java public class  HelloWorld { String name; public void  setName(String name) {  this .name = name; } public  String getName() {  return  name; } public  String hello() {  return “Hello “ + name; } public static void  main(String args[]) { HelloWorld helloWorld =  new  HelloWorld(); helloWorld.setName( “Java” ); System.out.println( helloWorld. hello() ); } }
HelloWorld.groovy class  HelloWorld { String name def  hello() {  "Hello $name"  } } def  helloWorld =  new  HelloWorld(name : " Groovy" ) println helloWorld.hello()
Key Groovy Features Java-like syntax Complements Java Object-oriented Targets Java VM (JSR-241) Invoke Java class within Groovy Invoke Groovy class within Java Dynamic Scripting (JSR-223) Brevity
Brevity Optional semicolons, package prefixes Automatic imports (java.util.*, java.net.*, java.io.*, groovy…) GroovyBeans (generated accessors) Optional typing Optional return
Groovy Gravy GStrings:  ”$book.title: $book.author (${book.reviewers.length})” Regular expressions:  assert ‘12345’ =~ /\d+/ Only objects: primitives converted to Reference Types Lists:  def list = [1, 2, 'hello', new java.util.Date()]  Maps:  def map = ['name':‘Indic Threads', 'location':‘Pune']  Closures  [1,2,3].collect {it*2} ===> [2, 4, 6] String literals – single, double, triple quotes
Closures Anonymous block of statements First class objects Parameters including default values Return value Binds variables in definition scope
Pluggable Behavior - Groovy def sum(num, closure) { def sum=0 1.upto(num) { if (closure(it)) sum+=it } } total=sum(10){it%2==0} println "Total: ${total}"
Pluggable Behavior - Java public static int sum(int num, Algo algo) { int val=0; for (int i=1; i<=n, i++)  { if (algo.eval(i)) val+=i; } return val; } public static void main (String[] args) { int total; total = sum(10, new Algo() { public boolean eval(int i) { return i%2==0; } } }); System.out.println(&quot;Total: &quot; + total); }
Dynamic Programming Add methods, properties to classes/objects at run-time Mixins to inject behaviour class ListUtils { static foo(List list) { &quot;foo called&quot; } } List.metaClass.mixin ListUtils assert &quot;foo called&quot; == [1, 2, 3].foo() Can extend class field-access mechanism Dynamic method invocation   myObj.”$action”()
Poolster Online “football pools” application Entities: Game, User Game    Stake, Option, Ends To join User chooses an Option REST Webservice backend Clients: iPhone, Grails, Android, Silverlight
Demo The Poolster Webservice
Grails – Philosophy Convention over Configuration Magic directories Implicit table names, column names Don’t Repeat Yourself Database maps to model class hasMany defines relationship  &  declares variable Layout, form validations Lightweight Modify and F5 Strong shoulders Spring (Grails MVC, DI, Transactions) Hibernate (GORM) Ant,  JUnit, SiteMesh, log4j, OSCache
Grails – Key Features Database mapping, relations MySQL integration URL Mapping Authentication / Filters Logging
Demo Poolster Web Client
Grails Web Application Rest Service Proxy Custom Tag libraries Sitemesh layout Templates
Unseen Gravy JUnit test cases, Mocking/Stubbing Web testing with Canoo Webtest Bootstrapping Pagination
Cons Learning curve  Performance ?! Early adopter issues Dynamic/Functional programming
Scripted In Groovy Canoo WebTest Tellurium Ant / Maven config files SoapUI script step Spring beans
Invoking Groovy Scripts import java.io.File; import groovy.lang.Binding; import groovy.util.GroovyScriptEngine; public class ScriptEngineEmbedGroovy{ public static void main(String args[]) throws Throwable{ String[] paths = {&quot;C:\\groovy&quot;}; GroovyScriptEngine gse = new GroovyScriptEngine(paths); Binding binding = new Binding(); Object[] var1 = 123; binding.setVariable(&quot;args&quot;,var1); String ret = gse.run(“DoSomething.groovy&quot;, binding); } }
Groovy Script Eval import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class CalcMain { public static void main(String[] args) throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName(&quot;groovy&quot;); // basic example System.out.println(engine.eval(&quot;(1..10).sum()&quot;)); //    55 // example showing scripting variables engine.put(&quot;first&quot;, &quot;HELLO&quot;); engine.put(&quot;second&quot;, &quot;world&quot;); System.out.println(engine.eval(&quot;first.toLowerCase() + second.toUpperCase()&quot;)); //    helloWORLD } }
Using Grails Web UI Flow Prototyping  REST Service Prototyping Supporting Web Apps Internal Web Apps
Using Groovy  Glue together existing components Reengineering legacy components Externalizing business logic, rules Domain Specific Languages Smart Configuration Scripting language of choice
Getting Started groovy.codehaus.org grails.org Free e-books Beginning Groovy and Grails (Apress) Getting started with Grails (InfoQ) refcardz.com  cheat sheets ibm.com  Mastering Grails, Practically Groovy
Thanks

More Related Content

KEY
PyCon AU 2012 - Debugging Live Python Web Applications
PDF
Golang Performance : microbenchmarks, profilers, and a war story
PDF
Apache Groovy: the language and the ecosystem
PPT
Javaone2008 Bof 5101 Groovytesting
PPT
Javaone2008 Bof 5102 Groovybuilders
PDF
Infinum android talks_10_getting groovy on android
PPTX
Introduction to Grails Framework
ZIP
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
PyCon AU 2012 - Debugging Live Python Web Applications
Golang Performance : microbenchmarks, profilers, and a war story
Apache Groovy: the language and the ecosystem
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5102 Groovybuilders
Infinum android talks_10_getting groovy on android
Introduction to Grails Framework
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge

What's hot (20)

PDF
Gradle in a Polyglot World
PDF
Using the Groovy Ecosystem for Rapid JVM Development
PPT
What's New in Groovy 1.6?
PDF
Node.js cluster
PDF
Build microservice with gRPC in golang
PDF
Javascript TDD with Jasmine, Karma, and Gulp
PDF
Javascript Promises/Q Library
PDF
Cool Jvm Tools to Help you Test - Aylesbury Testers Version
PPT
JS everywhere 2011
PPTX
Jersey framework
PDF
Groovy Powered Clean Code
PDF
Clojure: Simple By Design
PDF
Practical REPL-driven Development with Clojure
PDF
Exploring Clojurescript
PDF
ClojureScript: The Good Parts
PDF
JRuby @ Boulder Ruby
PDF
Kotlin - Better Java
PDF
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
PDF
JVM Dive for mere mortals
PDF
Python Load Testing - Pygotham 2012
Gradle in a Polyglot World
Using the Groovy Ecosystem for Rapid JVM Development
What's New in Groovy 1.6?
Node.js cluster
Build microservice with gRPC in golang
Javascript TDD with Jasmine, Karma, and Gulp
Javascript Promises/Q Library
Cool Jvm Tools to Help you Test - Aylesbury Testers Version
JS everywhere 2011
Jersey framework
Groovy Powered Clean Code
Clojure: Simple By Design
Practical REPL-driven Development with Clojure
Exploring Clojurescript
ClojureScript: The Good Parts
JRuby @ Boulder Ruby
Kotlin - Better Java
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
JVM Dive for mere mortals
Python Load Testing - Pygotham 2012
Ad

Viewers also liked (13)

PPT
Introduction to Progamming Applications for the iPhone
PPTX
An Efficient encryption using Data compression towards Steganography,introduc...
PPT
Hardware and Software parallelism
PPT
RC4&RC5
PPT
Introduction to Selenium
PPT
Selenium Architecture
PPT
Natural language processing
PPT
Introduction to Natural Language Processing
PPTX
Groovy for System Administrators
PPTX
PPT
Introduction to Natural Language Processing
PDF
Natural Language Processing
PPTX
Vehicle Number Plate Recognition System
Introduction to Progamming Applications for the iPhone
An Efficient encryption using Data compression towards Steganography,introduc...
Hardware and Software parallelism
RC4&RC5
Introduction to Selenium
Selenium Architecture
Natural language processing
Introduction to Natural Language Processing
Groovy for System Administrators
Introduction to Natural Language Processing
Natural Language Processing
Vehicle Number Plate Recognition System
Ad

Similar to Groovy & Grails: Scripting for Modern Web Applications (20)

PDF
Groovy - Grails as a modern scripting language for Web applications
PPT
Boosting Your Testing Productivity with Groovy
PPT
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
PPT
GTAC Boosting your Testing Productivity with Groovy
PPT
Svcc Groovy Testing
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
KEY
Android Bootstrap
PDF
Oscon Java Testing on the Fast Lane
PPT
Introduction To Groovy 2005
PDF
Griffon @ Svwjug
PDF
Enterprise Guice 20090217 Bejug
PDF
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
ODP
Agile web development Groovy Grails with Netbeans
PPT
Groovy Introduction - JAX Germany - 2008
KEY
JavaScript Growing Up
PDF
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
PPT
Introduction To Groovy
PPTX
Test Driven In Groovy
PDF
Introduction to Oracle Groovy
PPT
Jet presentation
Groovy - Grails as a modern scripting language for Web applications
Boosting Your Testing Productivity with Groovy
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
GTAC Boosting your Testing Productivity with Groovy
Svcc Groovy Testing
2007 09 10 Fzi Training Groovy Grails V Ws
Android Bootstrap
Oscon Java Testing on the Fast Lane
Introduction To Groovy 2005
Griffon @ Svwjug
Enterprise Guice 20090217 Bejug
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
Agile web development Groovy Grails with Netbeans
Groovy Introduction - JAX Germany - 2008
JavaScript Growing Up
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
Introduction To Groovy
Test Driven In Groovy
Introduction to Oracle Groovy
Jet presentation

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Cloud computing and distributed systems.
PPTX
A Presentation on Artificial Intelligence
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Big Data Technologies - Introduction.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Groovy & Grails: Scripting for Modern Web Applications

  • 1. Groovy & Grails Scripting for Modern Web Applications Rohit Nayak Talentica Software
  • 2. Agenda Demo: Quick intro to Grails Scripting, Web Applications and Grails/Groovy REST service in Grails Demo Internals Web Client in Grails Demo Internals Perspective
  • 3. Demo Quick Intro to Grails
  • 4. Maggi grails create-app Maggi Domain class: Noodle, Packaging grails generate-all Noodle, Packaging grails run-app
  • 5. Web Frameworks with Scripting Ruby on Rails (2004) CakePHP (2005) Django / Python (2005) Groovy on Grails (2006)
  • 6. Power of these frameworks Baked Experience The Language Agility / Productivity
  • 7. Baked Experience Model View Controller Object-Relational Mapping Templates Layout URL rewriting Ajax support XML / JSON support
  • 8. The Language Dynamic More expressive code Smaller code Native support for Lists, Hashmaps Lang. support for IO, Net, XML Idioms for common usage
  • 9. Agile Scaffolding Unit tests No build cycles Built-in webservers Fail faster!
  • 12. HelloWorld.java public class HelloWorld { String name; public void setName(String name) { this .name = name; } public String getName() { return name; } public String hello() { return “Hello “ + name; } public static void main(String args[]) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName( “Java” ); System.out.println( helloWorld. hello() ); } }
  • 13. HelloWorld.groovy class HelloWorld { String name def hello() { &quot;Hello $name&quot; } } def helloWorld = new HelloWorld(name : &quot; Groovy&quot; ) println helloWorld.hello()
  • 14. Key Groovy Features Java-like syntax Complements Java Object-oriented Targets Java VM (JSR-241) Invoke Java class within Groovy Invoke Groovy class within Java Dynamic Scripting (JSR-223) Brevity
  • 15. Brevity Optional semicolons, package prefixes Automatic imports (java.util.*, java.net.*, java.io.*, groovy…) GroovyBeans (generated accessors) Optional typing Optional return
  • 16. Groovy Gravy GStrings: ”$book.title: $book.author (${book.reviewers.length})” Regular expressions: assert ‘12345’ =~ /\d+/ Only objects: primitives converted to Reference Types Lists: def list = [1, 2, 'hello', new java.util.Date()] Maps: def map = ['name':‘Indic Threads', 'location':‘Pune'] Closures [1,2,3].collect {it*2} ===> [2, 4, 6] String literals – single, double, triple quotes
  • 17. Closures Anonymous block of statements First class objects Parameters including default values Return value Binds variables in definition scope
  • 18. Pluggable Behavior - Groovy def sum(num, closure) { def sum=0 1.upto(num) { if (closure(it)) sum+=it } } total=sum(10){it%2==0} println &quot;Total: ${total}&quot;
  • 19. Pluggable Behavior - Java public static int sum(int num, Algo algo) { int val=0; for (int i=1; i<=n, i++) { if (algo.eval(i)) val+=i; } return val; } public static void main (String[] args) { int total; total = sum(10, new Algo() { public boolean eval(int i) { return i%2==0; } } }); System.out.println(&quot;Total: &quot; + total); }
  • 20. Dynamic Programming Add methods, properties to classes/objects at run-time Mixins to inject behaviour class ListUtils { static foo(List list) { &quot;foo called&quot; } } List.metaClass.mixin ListUtils assert &quot;foo called&quot; == [1, 2, 3].foo() Can extend class field-access mechanism Dynamic method invocation myObj.”$action”()
  • 21. Poolster Online “football pools” application Entities: Game, User Game  Stake, Option, Ends To join User chooses an Option REST Webservice backend Clients: iPhone, Grails, Android, Silverlight
  • 22. Demo The Poolster Webservice
  • 23. Grails – Philosophy Convention over Configuration Magic directories Implicit table names, column names Don’t Repeat Yourself Database maps to model class hasMany defines relationship & declares variable Layout, form validations Lightweight Modify and F5 Strong shoulders Spring (Grails MVC, DI, Transactions) Hibernate (GORM) Ant, JUnit, SiteMesh, log4j, OSCache
  • 24. Grails – Key Features Database mapping, relations MySQL integration URL Mapping Authentication / Filters Logging
  • 26. Grails Web Application Rest Service Proxy Custom Tag libraries Sitemesh layout Templates
  • 27. Unseen Gravy JUnit test cases, Mocking/Stubbing Web testing with Canoo Webtest Bootstrapping Pagination
  • 28. Cons Learning curve Performance ?! Early adopter issues Dynamic/Functional programming
  • 29. Scripted In Groovy Canoo WebTest Tellurium Ant / Maven config files SoapUI script step Spring beans
  • 30. Invoking Groovy Scripts import java.io.File; import groovy.lang.Binding; import groovy.util.GroovyScriptEngine; public class ScriptEngineEmbedGroovy{ public static void main(String args[]) throws Throwable{ String[] paths = {&quot;C:\\groovy&quot;}; GroovyScriptEngine gse = new GroovyScriptEngine(paths); Binding binding = new Binding(); Object[] var1 = 123; binding.setVariable(&quot;args&quot;,var1); String ret = gse.run(“DoSomething.groovy&quot;, binding); } }
  • 31. Groovy Script Eval import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class CalcMain { public static void main(String[] args) throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName(&quot;groovy&quot;); // basic example System.out.println(engine.eval(&quot;(1..10).sum()&quot;)); //  55 // example showing scripting variables engine.put(&quot;first&quot;, &quot;HELLO&quot;); engine.put(&quot;second&quot;, &quot;world&quot;); System.out.println(engine.eval(&quot;first.toLowerCase() + second.toUpperCase()&quot;)); //  helloWORLD } }
  • 32. Using Grails Web UI Flow Prototyping REST Service Prototyping Supporting Web Apps Internal Web Apps
  • 33. Using Groovy Glue together existing components Reengineering legacy components Externalizing business logic, rules Domain Specific Languages Smart Configuration Scripting language of choice
  • 34. Getting Started groovy.codehaus.org grails.org Free e-books Beginning Groovy and Grails (Apress) Getting started with Grails (InfoQ) refcardz.com cheat sheets ibm.com Mastering Grails, Practically Groovy

Editor's Notes

  • #6: Grails uses domain classes unlike Rails which uses databases