Groovy and Grails

      Prabhu
prabhu@prabhu-s.com
Groovy
                         • is an agile and dynamic language for JVM
                         • Features inspired fr...
Features
•   Closures
•   Dynamic Methods
•   List & Maps
•   Better XML/Swing Capabilities
Closure
• Statements enclosed within curly braces
  “{}“
• Can be passed around like variables
• Return value either using...
Closure (Contd)
square = { it * it }
square (4)
Output = 16

“it” implicit parameter representing the
   passed value.
Closure (Contd)
square {
   it * it
   return 3
}

square (4)
Output = 3 (Since there is an explicit
  return statement!)
Closure (Contd)
square = { param1 ->
   param1 * param1
}

square (3)
Output = 9
Param1 – Named parameter
Factorial using closures
fact = 1               fact = 1
for (i in 2..5) {      (2..5).each {
                         num...
Factorial again!
fact = 1
2.upto(5) { num ->
  fact *= num
}
print fact

// Numbers are treated as object.
• upto method g...
Generic Factorial
fact = 1
fun = { n->
       2.upto(n) { num ->
       fact *= num
  }
}

fun(5)
print fact
Dynamic methods
class webcamp {
  def haveTea() {
     println quot;Time for teaquot;
  }
}

camp = new webcamp()
camp.hav...
Dynamic methods (Contd)
• Add a new method “haveSamosa”
class webcamp {
   def haveTea() {
         println quot;Time for ...
Lists and Maps
• Like python
List = [2,3,4]
Map = [„a‟ : 2, „b‟ : new Date()]
Grails
• Web Framework inspired by RoR
• Latest version is 1.0.1.
   – The one in the DVD is 1.0 and doesn‟t run cleanly i...
Create your first app
• Set environmental variables
  GRAILS_HOME and PATH
• grails create-app firstapp
Directory Structure

                      Controllers
        Domain



            Views


            Sources

        ...
First app Demo
• grails create-domain-class <name>
• grails create-controller <name>
• grails run-app (Starts inbuilt Jett...
Features
• Both gsp and jsp are supported.
• Specify constraints
static constraints = {
      username (minLength : 3, max...
Features (Contd)
• Dynamic finder methods
def search = {
   render(view : „list‟,
        model : [userList :
            ...
Features (Contd)
• Create Advanced Search criteria using
  Hibernate Criteria Builder.
def criteria = User.createCriteria(...
Features (Contd)
• Flash scope - Data will be stored as
  name-value pairs for current request
  and the next request.
• C...
Features (Contd)
• AJAX Support
  – Supports prototype, YUI, DOJO
  – Use render method in your groovy code to
    render ...
Features (Contd)
• Plays nicely with EJB
  – http://www.infoq.com/articles/grails-ejb-
    tutorial
• Has lots of plugins ...
Scalability
• Thanks to Spring and Hibernate, Grails
  is highly scalable and already enterprise
  ready!
RoR Competition?
• No.
  – Instead going to compete with Java based
    frameworks like struts 2, JSF, Seam.
Should I learn?
• If you are a Java guy, then yes.
• Companies like Oracle have already
  started showing full support!
Thanks



Q&A
Upcoming SlideShare
Loading in...5
×

Groovy and Grails talk

3,512

Published on

Prabhu talking about Groovy and Grails at WebCamp India

Published in: Business, Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,512
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
138
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Groovy and Grails talk

  1. 1. Groovy and Grails Prabhu prabhu@prabhu-s.com
  2. 2. Groovy • is an agile and dynamic language for JVM • Features inspired from Python, Ruby … • Seamless integration with all existing Java classes and libraries. • Groovysh and GroovyConsole • Can run on .Net 2.0 using IKVM • Latest version 1.5 • http://groovy.codehaus.org
  3. 3. Features • Closures • Dynamic Methods • List & Maps • Better XML/Swing Capabilities
  4. 4. Closure • Statements enclosed within curly braces “{}“ • Can be passed around like variables • Return value either using return keyword or the value of last executed statement.
  5. 5. Closure (Contd) square = { it * it } square (4) Output = 16 “it” implicit parameter representing the passed value.
  6. 6. Closure (Contd) square { it * it return 3 } square (4) Output = 3 (Since there is an explicit return statement!)
  7. 7. Closure (Contd) square = { param1 -> param1 * param1 } square (3) Output = 9 Param1 – Named parameter
  8. 8. Factorial using closures fact = 1 fact = 1 for (i in 2..5) { (2..5).each { number -> fact *= i fact *= number } } print fact print fact // (2..5) = Range
  9. 9. Factorial again! fact = 1 2.upto(5) { num -> fact *= num } print fact // Numbers are treated as object. • upto method generates all number objects • To each object, the closure gets passed around!
  10. 10. Generic Factorial fact = 1 fun = { n-> 2.upto(n) { num -> fact *= num } } fun(5) print fact
  11. 11. Dynamic methods class webcamp { def haveTea() { println quot;Time for teaquot; } } camp = new webcamp() camp.haveTea()
  12. 12. Dynamic methods (Contd) • Add a new method “haveSamosa” class webcamp { def haveTea() { println quot;Time for tea“ } } camp = new webcamp() camp.haveTea() webcamp.metaClass.haveSamosa = { -> println quot;Have Samosa alsoquot; } camp = new webcamp() // Create a fresh object since the class has changed! camp.haveSamosa()
  13. 13. Lists and Maps • Like python List = [2,3,4] Map = [„a‟ : 2, „b‟ : new Date()]
  14. 14. Grails • Web Framework inspired by RoR • Latest version is 1.0.1. – The one in the DVD is 1.0 and doesn‟t run cleanly in windows! • Grails = Spring MVC 2.5.1 + Hibernate 3 + SiteMesh 2.3 • Newbie‟s are unaware that Grails is full and full Spring! • Provides several commands to auto generate code. • Comes bundled with HSQLDB.
  15. 15. Create your first app • Set environmental variables GRAILS_HOME and PATH • grails create-app firstapp
  16. 16. Directory Structure Controllers Domain Views Sources Test cases J2EE Web Appln.
  17. 17. First app Demo • grails create-domain-class <name> • grails create-controller <name> • grails run-app (Starts inbuilt Jetty 6.1.4 server) • Visit http://localhost:8080/firstapp • Demo
  18. 18. Features • Both gsp and jsp are supported. • Specify constraints static constraints = { username (minLength : 3, maxLength: 8, blank:false) password(minLength : 5, maxLength:9, blank:false) } • Specify Optional fields static optionals = [“middle_name”, “addr2”]
  19. 19. Features (Contd) • Dynamic finder methods def search = { render(view : „list‟, model : [userList : User.findAllByUsernameLike ( „%‟ + params.username + „%‟) ] ) } • User.findAllByUsernameLikeAndPassword Like
  20. 20. Features (Contd) • Create Advanced Search criteria using Hibernate Criteria Builder. def criteria = User.createCriteria() // Dynamic method def results = criteria { and ( like („username‟ , „%‟ + params.username + „%‟) le(„age‟, params.age) ) } Render(view:‟list‟, model:[userList : results])
  21. 21. Features (Contd) • Flash scope - Data will be stored as name-value pairs for current request and the next request. • Create custom tags just by creating a new file in grails- app/taglib/<name>TagLib.groovy
  22. 22. Features (Contd) • AJAX Support – Supports prototype, YUI, DOJO – Use render method in your groovy code to render text, HTML, JSON or OpenRico Response XMLs • Plays nicely with flex – Simply add this static expose = [“flex-remoting”] – Automatic flex based CRUD generation and Domain class to AS3 compilation coming up!
  23. 23. Features (Contd) • Plays nicely with EJB – http://www.infoq.com/articles/grails-ejb- tutorial • Has lots of plugins already! – grails install-plugin <name> – grails create-plugin <name>
  24. 24. Scalability • Thanks to Spring and Hibernate, Grails is highly scalable and already enterprise ready!
  25. 25. RoR Competition? • No. – Instead going to compete with Java based frameworks like struts 2, JSF, Seam.
  26. 26. Should I learn? • If you are a Java guy, then yes. • Companies like Oracle have already started showing full support!
  27. 27. Thanks Q&A
  1. A particular slide catching your eye?

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

×