Skip to main content

Hello Grails!

Posted by bleonard on January 30, 2008 at 6:37 PM PST

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 and install Groovy
  2. Download and install Grails
  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

    Post List

    to the

following the pagination buttons. In its place put the following:
<h1>The Groovy Blog</h1>

<g:each in="${postList}" var="post">
  <small><g:link action="show" id="${}">permalink</g:link></small>
  • Return to the browser and refresh the list page:

  • 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

    Related Topics >>


    Hi *, the problem with having grails and the project on different drives is fixed, see: Please file all issues within issuezilla (category groovy) and give feedback what's missing.

    Brian, I have done some extra tries and found out that when I create my project on my D:-drive (in Windows) the error occurs. I tried to create it on my C:-drive as well and then the error does not occur. Seems like a bug to me...

    Brian, Sorry for my late response. I just tried to create the Domain Class from the command line, so this works.

    (on OS X 10.4 btw)

    Doesn't work for me :-( I install M1, and it works fine... Then install the Groovy plugin, and it won't start up again

    Netbeans throws the following

    java.lang.NoSuchMethodError: org.netbeans.Stamps.getModulesJARs()Lorg/netbeans/Stamps;

    Ben, are you able to successfully run the command "grails create-domain-class Post" from the command line (make sure you're in the project directory). -Brian

    Hi Aaron. For problem #1, typically that occurred for me when there was some kind of build error, but if the app runs okay from the command line, I don't know what's going on. For problem #2, interesting find - edit and delete do not work for me either until I generate the view code. This appears to be because the Domain Class and Controller are named differently. I just filed this as an issue with Grails. /Brian

    These steps don't work for me. Installing went fine, but when creating the domain class, I receive the following error message: java.lang.IllegalArgumentException: Called DataObject.find on null. I cannot seem to find a way to get around this. I already started over with a fresh userdir, but keep getting this error.

    Hi, I have a couple problems. #1 Runnin ghte application stops at "Running Grails application..". No server is available at port 8080 and the web browser does not launch. Manually executing the command from the windows console works. Once I have the server running with the scaffolding. Should edit and delete work? Both result in a Error 404 for me. Is that default behaviour for editing not to work right away with a scaffold or is my grails broken? Thanks, Aaron Broad

    Yes, Grails does include Groovy already, but like you, I couldn't get it to work w/out also installing Groovy separately. NetBeans may be dependant on something additional that ships with Groovy. A better place for this question would probably be the scripting e-mail alias.

    In other words, is step 1 under "Setting Things Up" really needed? What i did was download only Grails(which should already include Groovy), and then for what you have done under step 5, i set both home directories to the location of the grails installation, but all what i got was a lot of exceptions when trying to use the grails actions in NB. Is what I did incorrect?

    Why is installing Groovy needed? Doesn't Grails include Groovy already?

    Ah, I finally sorted through the CSS that controls the data entry forms. Sorry for the clutter.

    I apologize if this isn't an appropriate question. In the create.gsp, the data entry boxes are a standard width. I can't find a place to edit this width to sizes that fit the data field length. Can you help with this or suggest a more appropriate place to ask the question? Thanks

    Tim, good catch. The issue's being tracked here in case you want to watch it. -Brian

    Got it :-) I needed the "All" install, not just the Java SE one... Something must be missing from Java SE that the Groovy stuff requires :-) As you were people... ;-)

    Tim, I had the same problem with 6.1M1 for WinXP. Try the latest build (I installed todays) and it should work just fine.

    Yeah, I've tried removing my entire ~/.netbeans folder, but still no joy... I'll try it again, it's probably me being an idiot...

    Tim, by chance have you used development builds of NetBeans in the past? If so, it would be a good idea to delete your user directory (~/.netbeans/dev). After doing so you'll need to install the Groovy and Grails plugin again. I'm also on OS X 10.4 so it should work for you. -Brian

    Yes, I was wondering that as well. I installed the development updated center into the Beta (Tools > Plugins > Settings > Add). Here's the URL: . It appears to be working fine - I can't vouch for any of the other plugins on that site :-). /Brian

    Wheres the plugin for 6.1 Beta?