Sunday Jul 20, 2008

Grails: This Time With Tools

Here's how to get started with Grails in NetBeans IDE 6.5.

  1. Prepare the Environment

    • Read the Grails Quick Start, which presents the scenario that we will reproduce in the sections that follow.

    • Download and install the "Web & Java EE" distribution of NetBeans IDE 6.5 Milestone 1 or later. (Everything done below is done with the latest development build, which is probably the best thing to get for getting to know the IDE's Grails support, if you're okay with unexpected errors now and then.)

    • Download and unzip Grails.

    • In the IDE, go to the Options window and set the location of Grails in the "Groovy" panel. If you want to use Groovy, also set the location of Groovy there.

  2. Create the Application

    Choose the Grails Application project in the New Project wizard (Ctrl-Shift-N):

    Click Next and type the name of the project:

    Click Finish and you should see this:

    Expand the folders and have a look around:

    Also look at the generated files and notice that many of them have default values filled in:

  3. Create the Domain Class

    Right-click on the Domains classes node:

    Name the domain class "Book" and then fill in the following two lines within the generated class:

    String title
    String author

    You should now see this:

    Create some initial values in the Bootstrap.groovy class:

    new Book(author:"Stephen King",title:"The Shining").save()
    new Book(author:"James Patterson",title:"Along Came a Spider").save()

    Use Ctrl-Space to call up code completion and then have a look at the other ways of completing the statements above:

    You should now see this:

  4. Create the Controller

    Right-click the Controllers node:

    Type "Book" and notice that you are shown that the generated class will be called "BookController":

    Then comment out the one line generated within the braces and add this one:

    def scaffold = Book

    You should now see this:

  5. Run the Application

    Right-click the application and choose "Run". The application is deployed to Jetty, as you can see in the Services window:

    The URL is printed to the Output window. Paste it into a browser and then you'll see your application. Click the "BookController" link and you'll see this:

    Click New Book and then create a new entry:

    When you click Create, note that you can edit or delete the entry:

    ...and that the entry is reflected in the list of entries:

There are several other Grails-specific features, such as a Grails shell, support for installing Grails plugins, and menu items for things like Grails stats, upgrade, and WAR-file creation. I will blog about these soon.


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2008 »