Marketplace for Content, Tests and Assessment
 
 

Archive

Posts Tagged ‘grails’

Groovy Closures

August 31st, 2009 sadhna No comments

Groovy supports closures and they are very useful when we create Groovy applications.

The most important difference between a normal function/method and a Closure is that Closures can be passed onto other functions as arguments and they serve as Callbacks to the calling function.

For example we can pass closures as arguments to methods to execute them. We can create closures ourselves, but we can also convert a method to a closure with the .& operator. And we can use the converted method just like a normal closure. Because Groovy can use Java objects we can also convert a Java method into a closure.

syntax to write Closure-

def closure1 = {params ->
//statements & logic
}
e.g-
//type 1  - closure without any parameter
def closure2 = {
    println "Test Only"
}
//type 2 – closure with two param
def closure3 = { String name, int age ->
    println "My name is : $name and age is: $age"
}

A Closure is usually defined within the braces {… }. Parameters defines the list of parameters that are to be passed for the closure. The symbol '->' is used to separate the Arguments with the set of statements in the Closure Definition.
call a Closure Definition-

closure1.call( args )
closure1(args )
e.g 

closure2()
closure2.call()
output- 
Test Only
Test Only
-------------------------------------------
closure3("sadhna", 30)
closure3.call("saisha", 3)

output- 
My name is : sadhna and age is: 30
My name is : saisha and age is: 3

Like ‘this’ keyword which refers to the current object, there is ‘it’ keyword which, when used within a Closure Definition refers to the default first parameter being passed to the method. The following code will prove that,

def closure4 = {
    println it
}
Closure4.call("Test Again")
Closure4.call()

Output--
Test Again
null

Java  method  to  groovy closure

public class Test {
 public static void javaTest(final String s) {
  System.out.println("Java --" + s + "!");
 }
}

With the following script we use this Java class and convert the javaTest method to a closure:
// Simple list with names.
def names = ['groovy', 'grails', 'closure']
// Simple closure.
names.each { println 'Closure --' + it + '!' }
// Groovy method to convert to closure.
def groovySays(s) {
 "Groovy --${s}!"
}
// Use .& syntax to convert method to closure.
names.each(this.&groovySays)
// Convert Java method to closure and use it.
def javaTest = Test.&javaTest
names.each javaTest

output-
Closure --groovy!
Closure --grails!
Closure --closure!
Groovy --groovy!
Groovy --grails!
Groovy --closure!
Java --groovy!
Java --grails!
Java --closure!

Grails tutorial – How to create and run First Application in Grails

July 23rd, 2009 sadhna No comments

I am hoping you would have gone through Introducing Grails and then the Part 1 of this post Setting Up Grails .

Now I am covering the details of developing and running the application

1. Go to command prompt and your grails home directory.
2. Create the application.

  • The create-app command creates the full project, with a template with placeholders for the different components of your application such as configuration, MVC, library, and much more.
  • > grails create-app   (it will ask for a application name say – first_app)
  • Will create basic  view layout as main.gsp page Read more…

Setting up Grails

July 8th, 2009 sadhna No comments

In case you have  missed it , here is the first post Introduction to Grails

Before creating the first grails application we need to first  install & set up grails environment –

  1. Go to http://www.grails.org/Download , Download and unzip grails.
  2. Create a GRAILS_HOME environment variable.
  3. Add $GRAILS_HOME/bin to the PATH.

Pre-requisite -  Jdk 1.4 or above.

Cool thing about Grails is you don’t need to install server or database for running test apps. web server is ‘jetty’ server comes with grails so you do not need to install separately. But if you want to use any other server like tomcat , jboss   ..,you can.

Grails also comes with HSQLDB, a pure-Java database. Using another database, such as mysql or Oracle Database is just changing the dbdriver in DataSource.groovy ,thanks to Hibernate and GORM.

Enjoy setting your machine for start Grailing.

Update:  In this post, how to create and run First Applications . I am covering the next steps for starting development

Grails – A breath of fresh air for Java people

June 26th, 2009 sadhna 1 comment

I’ve been working in J2ee/java technologies from 8 years and really loved it. Never wanted to switch or look anything beyond java but started working on Groovy & Grails recently, few months ago, and found that in terms of developer productivity it seems to be light years ahead of other Java solutions (spring, Struts, EJB, JSF, JSP).

What is Groovy-

Groovy is a dynamic language which runs on the Java platform. It has Java-like syntax, although it’s not fully compatible with Java. (i.e. many Java programs are valid Groovy code, but not all.)Consider Groovy if you’re looking for a scripting language with dynamic language features and must deploy your applications on a JVM.

What is Grails-

Grails is an open source web framework for the Java platform. However, unlike other MVC model framework-

  • Grails domain classes are automatically persistable and can even generate the underlying database schema.
  • Services and other classes can be automatically injected using dependency injection based on naming conventions.
  • Grails controllers are request-scoped, which means a new instance is created for each request.
  • The default view for Grails is Groovy Server Pages (GSP) and typically renders HTML. The view layer also includes a flexible layout, a templating feature, and simple tag libraries.

I found Grails and java as -

  • Groovy is not evolution of Java but groovy compliments java.
  • Grails offers some of the best features with seamless Java, Spring & Hibernate integration allowing you to scale in terms of complexity as your application grows.
  • Groovy and Grails feel very natural to a Java programmer
  • Grails definitely has its own unique features such as dynamic tag libraries, a view technology that is not scriptlet based, the ability to use dynamic finders and persistence methods on any Groovy or Java class and its tight integration with Java, Spring & Hibernate.
  • You can develop a Grails application without knowing anything about Spring, Hibernate or SiteMesh
  • A Java development like Struts & EJB is a very configuration heavy approach, with Grails you can build an entire application and only ever have to configure the data source.
  • Groovy & Grails are very easy on the surface and allow you to be expressive and creative as a developer. They don’t, however, take away the underlying power of the JVM and frameworks like Spring & Hibernate.

Grails and in particular Groovy are very close to Java. A few quick lessons in Groovy and a Java developer and quickly feel at home.

The only real pitfall is that it’s based on Groovy and not on Java are -

  • less powerful IDE support.
  • some errors caught by Java at compile-time appear only at runtime in Groovy – this is by design and not a fault of Groovy.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes