<< GRuby on GRails: Tonight At 6:30 | Home | OK Or Cancel? >>

Jeff Brown: An Introduction To Building Groovy Web Applications With Grails

Groovy may be the one.

I'll try to live blog today's St. Louis JUG meeting. I have done this for the OCI internal Java and C++ lunches in the past. Since the auditorium at One City Place, where the JUG meets, has excellent WIFI, I thought I'll try to do the live blog here.

The JUG usually starts with a Q&A session:

Q: What's new in Java?

Kyle Cordes: We are on the other side of the adoption curve. There are enormous amount of money to be made. Scripting languages is a playground where you can learn some of the new constructs, like closures. These constructs are coming to a future version of Java.

Jay Meyer: The scripting support in Java 6 allows me to setup an enterprise ready server where you can deploy your applications written in a scripting language, such as Groovy, or JavaScript. Now you have transactions, you have security, etc.

Q: What is a good Java job?

Kyle Cordes: Software product work is more prestigious than IT work. There are good jobs in both categories here in St. Louis.

Jeff Brown is on stage to give today's Grails presentation. First person is Jeff now.

Ruby on Rails is new and fun. A lot of Java developer looked into Ruby on Rails and asked if it is possible to duplicate it in the Java platform. Grails meets that challenge.

Groovy is not a perfect programming language. But there are a lot of things in groovy that's cool.

(Jeff starts Eclipse.) Let me warn you that the Groovy plugin for Eclipse is flaky on a good day.

Groovy

In groovy, everything is an object. The number 5 is an object, you can call times on it:

class GroovyTest {
  static void main(args) {
    5.times { println it }
  }
}

In Groovy strings are surrounded in single quotes. Groovy also has GStrings. They are surrounded by double quotes. You can do variable substitution within GStrings.

Groovy support named parameter lists:

class GroovyTest {
  String firstName
  String lastName
  Integer age

  static void main(args) {
    def gt = new Groovytest(firstName: 'Jeff', lastName: 'Brown')
    println gt.firstName
  }
}

All the places where your IDE will generate getter and setter code for you are suspicious. In Groovy you don't need the boilerplate code. When you declare a field, a getter and a setter is written for you.

I can still write a getter, but only if I want to do something different.

There are no package level members in groovy.

No semi-colon is required.

There is a really easy way to generate markups.

Grails

All right, on to the meat of the presentation. What is Grails? It's a MVC web framework for web apps. It exploits the awesome power of Groovy. It leverages proven staples: Hibernate, Spring, and Sitemesh. It is excellent for those apps in the sweet spot. And it is Fun, Fun, Fun

The user mailing list is very active. They are not hostile to Rails at all. The communities are awesome.

In the spirit of fun, I'm going to give away an early access copy of a book that has not yet been written—Grails In Action, by Graeme Rocher, the main developer of Grails, to be published by Manning in January 2007. Four early access chapters are available already. [I completely messed up the details here. See Jeff Brown's comment at the end of this post for the correct information. My apologies to Jeff and Craeme. –Weiqi]

Famous people loves grails.

John Lennen:

"Imagine no config files
It's easy if you try
No action mappings
Man, that Grails is going to be fly."

(Jeff runs a command "grails create-app" and things flow on the command shell. Jeff then runs "grails create-domain-class" and entered "Person" and a class is generated.)

Apparently Rails does something that makes it difficult to create your own domain classes.

(Jeff runs the "grails create-controller" and entered "PersonControler".)

One of the things that is created is Eclipse project files. (Jeff then imports the whole thing into Eclipse.)

I'm going to add some attributes to Person. (Jeff adds firstName, lastName and age.) I'm going to add an attribute called scaffold

def scaffold = true

This tells grails to generate our controller at runtime.

(Starting Jetty web server, surfing to localhost:8080/javasigdemo, and things appear.) Just like JSPs, the pages in Grails are written in GSPs, Groovy Server Pages. They also need to be compiled the first time they are executed, just like JSPs.

(Mario Aquino: Jeff, are you using a database?) I'm using HSQL DB. I'll get to it in a moment.

(Jeff adds several persons in the web app.)

(Audience Member: What about the mixture of statically and dynamically typed variables? Jeff Grigg: Static types makes the code run faster.)

Famous people loves grails.

Batman and Robin:

"Holy productivity Batman! What are we going to do with the free time."

The scaffolding in Grails is interesting. The one we have been using all happens at runtime and are just like magic. I can also tell Grails to generate all, in which case it will generate all the code beforehand.

Looking at the controller: All the code blocks are mapped to actions:

def list = { [personList: Person.list(params) ] }

These code blocks are closures.

The "M"

The "M" in MVC are the domain classes.

In your domain class, you can stick with POGOs (plain old Groovy objects. You want your domain class to be free of view stuff. However you may want validation logic in your domain classes.

(Audience Member: When you change the code, what changes can be automatically picked up by the application?) Grails will dynamically pick up a lot of changes in your application. But not everything is picked up. Domain model changes are not picked up.

(Jeff shows validation at work. Invalid fields are highlighted.)

The "C"

The "C" (controller) is the traffic cop. It defines actions and navigates to the views.

We don't see anything in the list code block that tells us where to go. By convention, we go to list.gsp. If you don't want the automatic thing to happen, you can call redirect. (Jeff shows the URL associated with the delete button, something like http://localhost:8080/javasigdemo/person/delete/42)

(Kyle Cordes: Doing a GET on your delete URL deletes stuff. Doesn't that make you go eeeeh?)

The "V"

The "V" is GSP—Groovy Server Page.

In GSP you can easily define custom tags. You just define a closure. And the tag can be used in your GSP pages. No taglib XML files.

Sitemesh is at play here. Grails uses Sitemesh to layout the pages.

There are a lot of tags built into Grails. Logical, iterative, Ajax, form, etc.

Famous people loves grails.

Mr. T:

"I pity the fool who has to maintain all of those TLD files."

Hey Dude, Where Is My Data?

Grails generates configuration files *DataSource.groovy where * are: Development, Production, Test

Grail uses HSQL DB's in memory relational database as the default database. This can be easily changed.

You can use a ApplicationBootStrap.groovy class to to populate the database with sample data.

Grails hooks your domain objects with Hibernate, which in turn hooks up with the actual database. You don't have to write your Hibernate config files.

(Jeff writes an ApplicationBootStrap class.) This class is picked up when the application starts. It populates the database.

GORM is Groovy Object Relational Mapping. Currently it is Hibernate based. JPA support slated for 0.4.

Questions And Other Tidbits

(I asked about production or public websites running Grails.) Every 7 minutes somebody asks this question on the mailing list. It doesn't really make sense. If what you need is in there, you can use it. If not, not.

Grails 0.3 will be out in a month or so. It will have Sprint 2.0 integration.

(Kyle Cordes: How do you measure the relative merits of Grails vs. JRuby on Rails?) Groovy runs on the JVM, you can access all the Java core classes, leverage to use all the java classes you have written. JRuby is a tool that allows Ruby to be run on the JVM. Having alternatives is good.

(Jeff Grigg: There is a mismatch between Ruby and the JVM. The JRuby guys have to do a lot of tricky things to support Ruby language features such as adding methods to classes at run time.)

Just this week, at Sun days, according to one source, Sun announced that Groovy is going to be a module in Java 7.

Jeff wrapped up by giving away four ebooks on Grails. First person is Weiqi now.

What To Think

I think Jeff's talk is excellent. The audience participation makes it that much more interesting and lively.

I'm a little bit surprised by me own reaction. I came into the meeting thinking Grails is nothing but a knock-off of Ruby on Rails, and my reaction to Ruby on Rails hasn't been all that positive ("Rails is full of magic that I don't understand, will fall down somehow, etc.")

However, after the talk, I feel that Grail is a reasonable web framework that learned a lot from Ruby on Rails yet remained true to the Java platform. It makes use of the best ORM and other frameworks that the Java platform has to offer. And has a reasonable chance of picking up mind share among the rank and file Java developers. And it has a reasonable chance at succeeding with what it attempts to do.

To be sure, Groovy is not the cleanestly designed language. However it does mesh well with Java. And it does offer features that makes Grail possible. And most importantly, someone did something nice with Groovy.



Re: Jeff Brown: An Introduction To Building Groovy Web Applications With Grails

Thanks for all the details Weiqi. Graeme just posted to the Grails User list about a mistake in your report here that I initially missed. The post here says that I gave away copies of Grails In Action by Graeme Rocher, published by Manning. That is not correct. I gave away MEAP copies of Groovy In Action. The Grails book that is in development is The Definitive Guide To Grails. Thanks to Graeme for catching that.

Re: Jeff Brown: An Introduction To Building Groovy Web Applications With Grails

Don't forget the link to the Java User Group page for this talk, which includes the PowerPoint presentation: http://www.ociweb.com/javasig/knowledgebase/2006-09/index.html (And thanks to you, for consistently creating such pages! ;-)

Add a comment Send a TrackBack