0
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

Quick Intro to Grails
Maggi

    grails create-app Maggi

    Domain class: Noodle, Packaging

    grails generate-all Noodle, Packaging

  ...
Web Frameworks with Scripting

    Ruby on Rails (2004)


    CakePHP (2005)


    Django / Python (2005)


    Groovy...
Power of these frameworks

    Baked Experience


    The Language


    Agility / Productivity
Baked Experience

    Model View Controller

    Object-Relational Mapping

    Templates

    Layout

    URL rewrit...
The Language

    Dynamic

    More expressive code

    Smaller code

    Native support for Lists, Hashmaps

    La...
Agile

    Scaffolding

    Unit tests

    No compile 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...
HelloWorld.groovy
class HelloWorld {
    String name
    def hello() { "Hello $name" }
}
def helloWorld = new HelloWorld(n...
Key Groovy Features

    Java-like syntax

    Complements Java

    Object-oriented

    Targets Java VM (JSR-241)
  ...
Brevity

    Optional semicolons, package prefixes

    Automatic imports (java.util.*, java.net.*,
    java.io.*, groov...
Groovy Gravy

    GStrings: ”$book.title: $book.author (${book.reviewers.length})”

    Regular expressions: assert ‘123...
Closures

    Block of statements

    First class objects

    Parameters including default values

    Carries local...
Dynamic Programming

    Add methods, properties to classes/objects
    at run-time

    Mixins to inject behaviour

  ...
Poolster

    Online “football pools” application

    Entities: Game, User

    Game  Stake, Option, Ends

    To jo...
Demo

The Poolster Webservice
Grails – Philosophy

    Convention over Configuration
     −   Magic directories
     −   Implicit table names, column n...
Grails – Key Features

    Database constraints, composition

    MySQL integration

    URL Mapping

    Authenticati...
Demo

Poolster Web Client
Grails Web Application

    Custom Tag libraries

    Session variables

    Sitemesh layout

    Templates

    Inte...
Unseen Gravy

    JUnit test cases, Mocking/Stubbing

    Web testing with Canoo Webtest

    Bootstrapping

    Pagin...
Cons

    Learning curve

    Performance ?!

    Early adopter issues
Scripted In Groovy

    Canoo WebTest

    Tellurium

    Ant / Maven config files

    SoapUI script step

    Sprin...
Invoking Groovy Scripts
import java.io.File;
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;

public cl...
Getting Started

    groovy.codehaus.org

    grails.org

    Free e-books
     −   Beginning Groovy and Grails (Apress...
Thanks
Upcoming SlideShare
Loading in...5
×

Groovy - Grails as a modern scripting language for Web applications

2,095

Published on

Dynamic scripting languages are a powerful addition to a software designer’s toolbox. Rails/Ruby and Python have not gained much acceptance in the enterprise. Grails and Groovy are an attempt to bridge the gap between the modern scripting world and the Enterprise Java world.

This talk is an introduction towards building web applications in Grails. First we will go about creating a REST based webservice. We will also show how to replace the default database backend of Grails with MySQL.

We will then build a web application that consumes this webservice. The emphasis will be on the design patterns and idioms in Grails that address the web application development lifecycle.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,095
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Groovy - Grails as a modern scripting language for Web applications"

  1. 1. Groovy & Grails Scripting for Modern Web Applications Rohit Nayak Talentica Software
  2. 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. 3. Demo Quick Intro to Grails
  4. 4. Maggi  grails create-app Maggi  Domain class: Noodle, Packaging  grails generate-all Noodle, Packaging  grails run-app
  5. 5. Web Frameworks with Scripting  Ruby on Rails (2004)  CakePHP (2005)  Django / Python (2005)  Groovy on Grails (2006)
  6. 6. Power of these frameworks  Baked Experience  The Language  Agility / Productivity
  7. 7. Baked Experience  Model View Controller  Object-Relational Mapping  Templates  Layout  URL rewriting  Ajax support  XML / JSON support
  8. 8. The Language  Dynamic  More expressive code  Smaller code  Native support for Lists, Hashmaps  Lang. support for IO, Net, XML  Idioms for common Design Patterns
  9. 9. Agile  Scaffolding  Unit tests  No compile cycles  Built-in webservers  Fail faster!
  10. 10. http://www.zacker.org/ruby-on-rails (Nov 2nd)
  11. 11. http://www.zacker.org/ruby-on-rails (Nov 2nd)
  12. 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. 13. HelloWorld.groovy class HelloWorld { String name def hello() { "Hello $name" } } def helloWorld = new HelloWorld(name:"Groovy") println helloWorld.hello()
  14. 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. 15. Brevity  Optional semicolons, package prefixes  Automatic imports (java.util.*, java.net.*, java.io.*, groovy…)  GroovyBeans (generated accessors)  Optional typing  Optional return
  16. 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. 17. Closures  Block of statements  First class objects  Parameters including default values  Carries local context def foo(n) { // method return { n += it } // returns closure } def accumulator = foo(1) //curried closure assert accumulator(2) == 3 //assert used for self-documenting, unit-testing assert accumulator(1) == 4
  18. 18. Dynamic Programming  Add methods, properties to classes/objects at run-time  Mixins to inject behaviour  Can extend class field-access mechanism  Dynamic method invocation
  19. 19. 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
  20. 20. Demo The Poolster Webservice
  21. 21. Grails – Philosophy  Convention over Configuration − Magic directories − Implicit table names, column names  Don’t Repeat Yourself − Database maps to domain − hasMany defines relationship & declares variable − Layout, form validations  Lightweight − Modify and F5  Strong shoulders − Spring (Grails MVC, DI, Transactions) − Hibernate (GORM) − Ant, JUnit, SiteMesh
  22. 22. Grails – Key Features  Database constraints, composition  MySQL integration  URL Mapping  Authentication / Filters  Bootstrapping  Unit test  Logging
  23. 23. Demo Poolster Web Client
  24. 24. Grails Web Application  Custom Tag libraries  Session variables  Sitemesh layout  Templates  Internationalization
  25. 25. Unseen Gravy  JUnit test cases, Mocking/Stubbing  Web testing with Canoo Webtest  Bootstrapping  Pagination
  26. 26. Cons  Learning curve  Performance ?!  Early adopter issues
  27. 27. Scripted In Groovy  Canoo WebTest  Tellurium  Ant / Maven config files  SoapUI script step  Spring beans  <YOUR APP HERE> 
  28. 28. 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 = {"C:groovy"}; GroovyScriptEngine gse = new GroovyScriptEngine(paths); Binding binding = new Binding(); Object[] path = {"C:musicmp3"}; binding.setVariable("args",path); gse.run("Songs.groovy", binding); } }
  29. 29. 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
  30. 30. Thanks
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×