Introduction to the Grails Web Framework

This document gets you started with Grails in NetBeans IDE. From the Grails site: "Grails aims to bring the 'coding by convention' paradigm to Groovy. It's an open-source web application framework that leverages the Groovy language and complements Java Web development. You can use Grails as a standalone development environment that hides all configuration details or integrate your Java business logic. Grails aims to make development as simple as possible and hence should appeal to a wide range of developers not just those from the Java community."

Contents

Content on this page applies to NetBeans IDE 6.7, 6.8, 6.9 and 7.0

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE version 6.5 Java or above
Java Development Kit (JDK) version 6 or
version 5

Creating the Application

In this section, we run the "grails create-app" command from the IDE, by using the "Grails Application" project template to create a new Grails application.

  1. Choose File > New Project (Ctrl-Shift-N) and then select "Grails Application" from the "Groovy" category. Click Next.

  2. In Project Name, type "BookDemo"; in Project Location, select the folder where the application will be created. Click Finish.

The IDE runs the "grails create-app" command, showing the output in the Output window. The Projects window should show you this:


Expand the folders and have a look at the source structure created by the IDE via the Grails scripts. Also look at the generated files and notice that many of them have default values filled in.

Creating the Domain Class

In this section, we use the IDE to run the "grails create-domain-class" script.

  1. Right-click on the Domain Classes node and choose New > Grails Domain Class.
  2. Name the domain class "Book" and click Finish. The "Book.groovy" domain is created in the Domain Classes node.

  3. Open the Book class and then fill in two Strings, "title" and "author". You should now see this:

  4. Create some initial values in the Bootstrap.groovy class, which is within the Configuration node. The code added to the class is shown in bold below:
  5. class BootStrap {
    
         def init = { servletContext ->
             new Book(author:"Stephen King",title:"The Shining").save()
             new Book(author:"James Patterson",title:"Along Came a Spider").save()
         }
         
         def destroy = {
         }
         
    }

Creating the Controller

In this section, we use the "grails create-controller" script to create a controller for our domain class:

  1. Right-click the Controllers node and choose New > Grails Controller.
  2. Type "Book" in Class Name and notice that you are shown that the generated class will be called "BookController":

  3. Click Finish. The controller is generated.

  4. Comment out the one line generated within the braces and add "def scaffold = Book". You should now see this:

Running the Application

Our simple Grails application is now complete. In this section, we deploy it.

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

  2. The URL is printed to the Output window. If the browser does not open automatically, paste the URL into a browser and then you'll see your application. Click the "BookController" link and you'll see this:

  3. Click New Book and then create a new entry:

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

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


Send Feedback on This Tutorial

See Also

This concludes the introduction to Grails in NetBeans IDE. You now know how to create a basic application in the Grails framework using the IDE.

For more information about Grails and Groovy technology on netbeans.org, see Introduction to Groovy.