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 7.2, 7.3, 7.4 and 8.0

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

Software or Resource Version Required
NetBeans IDE version 7.2 Java or above
Java Development Kit (JDK) version 7
Grails current version

Notes.

  • You need to use JDK 7 as the Java Platform. Grails currently does not run on JDK 8.
  • You need to activate the Groovy plugin to develop Grails applications in the IDE.
  • Open the Options window of the IDE and set the location of Grails in the Groovy tab in the Miscellaneous category.

    If you want to use Groovy, also set the location of Groovy there.

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

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. Install Grails on your local system and confirm that it is configured correctly.

    You can confirm it is working correctly by running Grails from the command line (i.e., type "grails" at the command prompt).

  2. Choose File > New Project (Ctrl-Shift-N) and then select "Grails Application" from the "Groovy" category. Click Next.
  3. 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:
    screenshot of book class in source editor
  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:
    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 = {
         }
         
    }

    Note. Add an import for bookdemo.Book if you see a warning in the left margin of the source editor.

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":
    screenshot of New Controller dialog box

    Click Finish. The controller is generated.

  3. Comment out the one line generated within the braces and add "def scaffold = Book". You should now see this:
    screenshot of class in editor

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:
    screenshot of 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:
    screenshot of list in browser window
  3. Click New Book and then create a new entry:
    screenshot of create entry page in browser window
  4. When you click Create, note that you can edit or delete the entry:
    screenshot of edit page in browser window
  5. ...and that the entry is reflected in the list of entries:
    screenshot of list page in browser window with new entry

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.

get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo