NetBeans 6.1 M1 was just released, and with it comes rudimentary support for the Groovy language and the Grails framework. I say rudimentary because this is M1, but there's a lot of work in progress as you can see from this task list.

So, of course I'm anxious to compare Grails to Rails. What better way to do than by creating the same blog application I've been blogging about with Rails?

Setting Things Up

Unlike with Rails, the NetBeans Groovy plugin does not come bundled with Groovy and the Grails framework - these must be pre-installed on your system (don't worry, it's easy):

  1. Download andinstallGroovy
  2. Downloadand installGrails
  3. Download and install NetBeans 6.1 M1.
  4. Install the Groovy and Grails plugin (Tools > Plugins > Available Plugins)
  5. Open the NetBeans Options dialog and select the Groovy category. Set the Groovy and Grails home directories.

Creating the Grails Project

  1. Choose File > New Project to create a new Grails Application named GroovyWebLog. Your new Grails application will appear in the IDE:

Creating the Model

Or as Grails prefers to call them, Domain classes.

  1. Right-click the Domain classes node and select "Create new Domain Class":

  2. Name the class Post.
  3. When Post.groovy opens in the editor, add a single field, title:

    class Post {
        String title

Creating the Controller

  1. Right-click the Controllers node and select "Create new controller"
  2. Name the controller Blog.
  3. For now, we'll use dynamic scaffolding to create the application views at runtime. Replace the default contents of BlogController with the following:

    class BlogController {
        def scaffold = Post
    Note, if our controller named matched the domain class name, we could then use def scaffolding = true. I kept the model and controller names different to match what we did with Rails.

Run the Application

  1. Right-click the GrailsWebLog project and select Grails > Run Application.
  2. If all goes well, NetBeans will start the Jetty server that comes bundled with Grails and launch your browser where you will see the Grails welcome page:
  3. Click the BlogController link:

  4. Create a New Post:

    It's not immediately obvious, but the Id is a link which you can use to view the post details. From the detail page you can then edit and/or delete the post:

Adding Another Field

Our blog needs a body field.

  1. Edit Post.groovy and add a body field:

    class Post {
        String title
        String body
  2. Return to the browser and depending on what you do, you may get an error. By default Grails is configured to drop and create the database every time a change is made, so if you tried to edit the existing record you were viewing, you got a NullPointerException. However, if you navigate back to the list, you will see that the body field has been detected and you can add a new record again:

A Friendlier Database Configuration

Surprisingly, we haven't had to talk about databases yet as the Grails framework magically took care of this for us. The Grails framework includes HSQLDB and all of the necessary configuration was setup for us in DataSource.groovy, which you'll find under the Configuration node:

You can also see from this configuration file that Grails supports the same three environments as Rails: development, test and production. It's also easy to switch to another database, just set the driver class name, jdbc url and copy the JDBC driver to the project's lib directory.

Do the following:

  1. Change the dbCreate property from "create-drop" to "update".
  2. Right-click the GrailsWebLog project and choose Grails > Stop Application.
  3. Richt-click the project again and choose Grails > Run Application.
  4. Now experiment with adding and/or deleting fields - you'll notice that the behavior is much more "Railsesque".

Validating Input

Like Rails, validation in Grails is very straightforward.

  1. Open Post.groovy and add the following constraints:

    class Post {
        String title
        String body
        static constraints = {
  2. Attempt to add a new Post without entering any data:

Customizing the View

Okay, enough with this dynamic scaffolding. Let's generate our controller and view code. Unfortunately, for this step I haven't found the menu option in NetBeans yet, so we'll resort to the command line for now.

  1. Open your command prompt and navigate to the GrailsWebLog project directory. If you've forgotten where this is, you can find it in the project's Properties dialog.
  2. Run the command grails generate-all Post:

  3. Now this created a new controller named PostController. I don't see a way to specify a controller name when using generate-all (or generate-controller). That's fine - I'll leave BlogController to do the dynamic scaffolding and use PostController for my customizations.
  4. Also created were a set of GSP (Groovy Server Pages) for managing the view for the PostController:

  5. Open list.gsp and delete everything from from the <h1>Post List<h1> to the </div> following the pagination buttons. In its place put the following:
  6. <h1>The Groovy Blog</h1>
    <g:each in="${postList}" var="post">
      <small><g:link action="show" id="${}">permalink</g:link></small>
  7. Return to the browser and refresh the list page:

  8. To display the blog with the most recent entry first, add .reverse() to the end of postList in list.gsp:

    <g:each in="${postList.reverse()}" var="post">

The Completed Application