Grails vs. Rails vs. Roo!?

There are some great choices out there to choose from when starting a new web development project. Whether you are just doing a simple site or starting your own business. The frameworks, very thankfully, are getting so good. Which one will manage to stand the test of time? The graph below doesn’t mean that the most trend is always going to be the highest one, especially since roo is so fresh and in the past year has been improved dramatically.

Error starting Sun’s native2ascii: In Grails

If you are seeing this error what I would recommend is to add the JDK jars to your eclipse “Installed JRE”. This will then use the JDK for developing Grails rather then using the standard JRE.

To change to using the JDK in Eclipse

  • Go to Window -> Preferences.
  • Select Java -> Installed JREs
  • Click “Add” button
  • Go through the wizard to add your downloaded JDK
  • Select your newly added JDK back at the main window

Then you should be good to go!

JSON Output In Grails Controller

I wanted to be able to output an object with all the domain properties. This is obviously very easy in Grails:

1
MyObject as JSON

What if you wanted to be able to add to the MyObject? There are a bunch of ways that Grails provides in order to allow you to let you do this. Unfortunately, I didn’t know all of them at first so I had to do a bit of research.

Since I just wanted to add a simple property to render I added a transient value to my object:

1
static transients = ["link"]

Then I was able to set this value in the controller by iterating over my objects:

1
myObjectList.each() { it.href = createLink(action:'show', id:it.id).encodeAsHTML() };

Then I could just do the following:

1
render json as JSON

Bang! I have my extra attribute! Depending on what you want to do there might be a better way for your particular situation. As far as the code is concerned, this was a nice short change.

If more is needed I might need to start using the ObjectMarshaller. You can read about that and other ways to work with JSON by visiting the link below. They were probably the best resources I found out there at the moment.

Excellent blog:
Rending JSON Part 2: http://manbuildswebsite.com/2010/02/08/rendering-json-in-grails-part-2-plain-old-groovy-objects-and-domain-objects/

Rending JSON Part 3: http://manbuildswebsite.com/2010/02/15/rendering-json-in-grails-part-3-customise-your-json-with-object-marshallers/

Grails findBy for hasMany Relationship

Interesting that this is not built into grails. As many people know who use grails you automatically get dynamic “findBy” methods on persisted objects. It is incredibly useful and provides easy access to attributes of a persisted class. The only problem is when you try to do the same thing on a “hasMany” relationship of that class. For example:

1
2
3
4
class Airport {
    String name
    static hasMany = [flights:Flight]
}

You can do:

1
Airport.findByName("MSP")

You cannot do:

1
Airport.findByFlights(flight)

What do you do? In my case, I decided to make it.

Here is my implementation:

1
2
3
4
5
6
7
8
9
10
class Airport {
   static Airport findByFlight(Flight flight)
   def c = Airport.createCriteria()
   def result = c.get {
      flights {
         idEq(flight.id)
      }
   }
   return result;
}

Connect To HSQLDB Grails Database

This is how you can connect to use the DBVisualize to connect to grails using the HSQL database. I wanted to learn more about GORM and to understand how it was generating the tables.

Datasource.groovy

1
2
3
4
5
6
 development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:hsqldb:file:devDB;shutdown=true"
}
}
  • Choose HSQLDB embedded as your Driver using the hsqldb.jar located under your Grails home directory ($GRAILS_HOME/lib)
  • Update the Database URL to include your grails app base directory (jdbc:hsqldb:file:<grails app base>/devDB). Window users don’t have to specify the drive (ie. “C:”).

Groovy Links

Here are some useful links for getting your hands wet with Groovy/Grails programming:

Programming Resources

Groovy resource: http://pleac.sourceforge.net/pleac_groovy/index.html

Plug-ins: Grails Plugins

Forum: http://grails.1312388.n4.nabble.com/

Blogs: http://groovyblogs.org/entries/recent

Book references: Recipes!

Design Ideas

Get the ideas flowing: 99designs.com

Quickly generate rounded corners: http://www.roundedcornr.com/