rod mclaughlin


Grails - another web development environment (19 jan 10)

 

Groovy is a language for making Java like Ruby. Grails is Ruby on Rails in Groovy. Reading the book on the subject 'The Definitive Guide to Grails', it's like Microsoft books about .NET which give hardly any credit to Java, on which their platform is based. The Definitive Guide gives little credit to Rails, though it is obviously a direct copy. It is almost as good, but orders of magnitude slower. Furthermore, the instructions are wrong.

Before doing

grails run-app

you need to edit grails-app/conf/Config.groovy and change

grails.enable.native2ascii = true

to - guess what? -

grails.enable.native2ascii = false

Hat tip : http://devav2.blogspot.com/2009/08/grails-native2ascii-error.html

I had a few classpath issues as well, but, well, it's based on Java...

It makes you laugh out loud how long it takes to get started compared to Rails, but hey, it's an Enterprise Solution Based On The Java Platform, so I'm sure it's worth waiting for.

The book says run

grails run-app and go to http://localhost:8080/app/

and you'll be able to edit the things you just created (Song.rb) but it goes to another page

I guess it's in rapid development - I hope it doesn't turn out like Merb

I'm a silly *unt! I hadn't read the instructions. SongController.rb should look like this:

package com.gzone.gtunes

class SongController {

def scaffold = Song
}


Now I can go to http://localhost:8080/app/song/list and add songs...

Then it tells you how to create Albums and create a one-to-many relation between Albums and Songs. It's just like Ruby on Rails, you just add a couple of lines of Groovy and the database is done for you. The book says to improve the way Albums are displayed in the Create Song page by asking you to write a toString method, but doesn't tell you how. This is how:

    public String toString() {
            return title
    }

Another fun thing about Grails is it is based on Java, so you have time to make a cup of tea while it is compiling your application.

It has helpful error messages for those rare occasions when I make a mistake too, courtesy of the Spring Framework :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException

 

If you change a file and save the file, Grails notices the file has changed, and tries to recompile your Groovy code into Java bytecodes and run it as a web application inside Tomcat. If you make a mistake, it gives you a long and mostly irrelevant error message. If you then fix your mistake and save the file, Grails sometimes fails to notice this, and you have to stop and restart it.

It has good generators though. If you create a simple class called Album.groovy and then run

grails generate-all Album

it creates the simple view, view all, edit and new html pages for adding and seeing albums, and a controller which connects the view to the model (domain, as the authors call it) and then the database.

To make the list of objects have a link to pages to show each object, I had to edit

views/abum/list.gsp and change this

  <g:link action="show" id="${albumInstance.id}">${fieldValue(bean: albumInstance, 
field: "id")}</g:link></td>

to this

    <a href="show/${albumInstance.id}">${fieldValue(bean: albumInstance, field: "title")}</a>

- it could be browser issues - I'm tesing on Firefox and Opera on Linux, about the least popular combination you could find...

Another great thing about Grails is, unlike Rails, if you leave it running for a few hours and come back, it disconnects from the database, and instead of seeing a list of 'controllers' you see a vast stack trace winging about a broken pipe. This means you have to start the app again, giving you time to make another cup of tea.

 

 



Back
Portland London