30
 Groovy And Grails Harshad Oak Rightrix Solutions

Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

  • Upload
    danganh

  • View
    224

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovy And Grails

Harshad OakRightrix Solutions

Page 2: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovy Basics• Scripting / Agile / Dynamic ...Language

• Syntax very close to Java

• Meant for Java developers.

• A powerful high level language for the Java “platform”

• Groovy code Compiles to Java bytecode.

• You can get productive quickly

­ Harshad Oak

Page 3: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovy Basics• Open source using a BSD / Apache style licence

• Backed by JSR 241

• groovy.codehaus.org

­ Harshad Oak

Page 4: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovy Installation

• Download binary

• Set GROOVY_HOME

• Add GROOVY_HOME/bin to your PATH

• Set JAVA_HOME

• Try Groovy and GroovyConsole

­ Harshad Oak

Page 5: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovy With Java

• Use groovyc instead of javac to create bytecode files.

• Run with java

• To embed Groovy you can use the GroovyClassLoader or GroovyScriptEngine

• Groovy integrates with the Bean Scripting Framework, which allows you to embed any scripting engine into your Java code

­ Harshad Oak

Page 6: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovy Development

• IDE Support

•Eclipse plugin

•NetBeans using Coyote (Groovy + Jython)

•Textpad

•UltraEdit

•Crimson Editor

• IntelliJ IDEA ...

­ Harshad Oak

Page 7: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovy Development

• Language Constructs

• List & Maps support

• XML Capabilities

• Swing Capabilities

• Testing

• Groovy SQL

• The power of Java libraries stays with you. It's just easier and faster with Groovy

­ Harshad Oak

Page 8: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Closures

• Closures are reusable blocks of code

• One or more program statements enclosed in curly brackets

• Closures do not require a class or a method name

­ Harshad Oak

Page 9: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Closures

• Can be passed around like a variable

• The statements within a closure are not executed until the call() is made

• Return is using an explicit return statement or the value of the last executed statement

­ Harshad Oak

Page 10: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Strings

• Strings work almost like those in Java

• Expect for Gstring.

•GStrings can contain expressions in the form

${expression}

­ Harshad Oak

Page 11: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Dynamic Methods

• Adding new methods to an object at runtime

• Intercept non-existent method calls

• Used extensively in Grails

• Meta-Object Protocol

­ Harshad Oak

Page 12: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Differences from Java

• == means equals

• Semicolon & return are optional

• Methods and classes are public by default

­ Harshad Oak

Page 13: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovlets and GSP

• Groovy alternatives for Servlets and JSP

• Will work on any J2EE container.

• Add JAR files

• Tweak web.xml

­ Harshad Oak

Page 14: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Groovlets and GSP

• Implicit Variables• request ServletRequest • response ServletResponse -• context ServletContext • application ServletContext • session getSession(false) • out response.getWriter() • sout response.getOutputStream()• html new MarkupBuilder(out)

­ Harshad Oak

Page 15: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Some More Groovy

• Readymade modules for:

•XML-RPC

•SOAP

•Xdoclet like tag processing

•Working with Google APIs

• ...

­ Harshad Oak

Page 16: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails

• Web Framework inspired by Ruby On Rails

• Latest version is 0.3

• Coding by convention paradigm

• Reduce the need for configuration files and other boilerplate code

• File names are important

­ Harshad Oak

Page 17: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails

• Provides several commands to auto generate common requirements in a web app.

• Based on open source technologies like Spring, Hibernate and SiteMesh

• Grails hides underlying complexity

• Focus is on rapid development and simplicity

­ Harshad Oak

Page 18: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Installation

• Extract contents

• Set GRAILS_HOME environment variable

• Add GRAILS_HOME\bin to the PATH variable

• Run the grails command from the command prompt to check usage

­ Harshad Oak

Page 19: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Installation

• Extract contents

• Set GRAILS_HOME environment variable

• Add GRAILS_HOME\bin to the PATH variable

• Run the grails command from the command prompt to check usage

­ Harshad Oak

Page 20: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Installation

­ Harshad Oak

Controllers

Domain Classes

Views

Normal J2EE web application

Page 21: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

GORM

• Domain Classes are objects that are mapped to the database

• Uses Hibernate 3 underneath

• Link domain classes using relationships

• Provide powerful dynamic methods for CRUD (Create/Read/Update/Delete) operations

• An HSQL database comes built-in with Grails

­ Harshad Oak

Page 22: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Development

• Example -Develop a Clothes Manager web application•Change database

•Setup DB•Setup ApplicationDataSource

•Create Domain Classes

­ Harshad Oak

Page 23: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Development ­ Domain Classes

• Create Domain Classes•Cabinet.groovy•Shirt.groovy•Trouser.groovy

• Define relationships

­ Harshad Oak

Page 24: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Development ­Controllers

• Controllers are responsible for handling the requests in the web application

• Use grails -generate-all to generate controllers for our 3 domain classes

• Once controller is done processing a request, it has to delegate to an appropriate view

­ Harshad Oak

Page 25: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Development ­Views

• Controllers delegate to view based on a convention mechanism

• So list closure in ShirtController will delegate to the view /grails-app/views/shirt/list.gsp or /grails-app/views/shirt/list.jsp.

• Note that you can use JSPs

­ Harshad Oak

Page 26: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Development ­Deploy

• Built in Resin server • Directly run your application by using the grails

run-app command• Lets use Tomcat instead

•WAR file creation using grails war•Deploy the WAR file just like any other J2EE

WAR file

­ Harshad Oak

Page 27: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Services

• Services are named in the format XXService.groovy

• Directory /grails-app/services• Can have Business Logic • You can call these services from within

controllers

­ Harshad Oak

Page 28: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Tags

• Range of tags for both GSPs and JSPs• No configuration• No updating of TLD descriptors• Edit grails-app\taglib\ApplicationTagLib.groovy

or create a new file XXTagLib.groovy

­ Harshad Oak

Page 29: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Grails Ajax

• Can work with Prototype, Yahoo or Dojo• Configure the library and then just use the grails

tags • Use render method to send text, markup, JSON

or OpenRico responses

­ Harshad Oak

Page 30: Groovy And Grails - IndicThreads – Software … And Grails Harshad Oak Rightrix Solutions Groovy Basics • Scripting / Agile / Dynamic ...Language • Syntax very close to Java

   

Thank You

• Further Reading• groovy.codehaus.org

• grails.org

• Several books in the works• Groovy in Action (Manning)

• Groovy Programming: An Introduction for Java

Developers (Morgan Kaufmann)

­ Harshad Oak