Your SlideShare is downloading. ×
Groovy & Grails: Scripting for Modern Web Applications
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Groovy & Grails: Scripting for Modern Web Applications

3,761
views

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 …

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

1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,761
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
63
Comments
1
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Grails uses domain classes unlike Rails which uses databases
  • Transcript

    • 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!
    • 10. http://www.zacker.org/ruby-on-rails (Nov 2nd)
    • 11. http://www.zacker.org/ruby-on-rails (Nov 2nd)
    • 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() { "Hello $name" }
      • }
      • def helloWorld = new HelloWorld(name : " Groovy" )
      • 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 "Total: ${total}"
    • 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
    • 25. Demo Poolster Web Client
    • 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:roovy&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
    • 35. Thanks