Last updated by c.knoblauch 2 weeks ago

Grails Quick Start in NetBeans IDE

Here's how to get started with Grails in NetBeans IDE in 5 simple steps.
  1. Prepare the Environment
  2. Create the Application
  3. Create the Domain Class
  4. Create the Controller
  5. Run the Application

Step 1: Prepare the Environment

  • Read the official Grails Quick Start. Go here to do so. That document presents the scenario that we will reproduce in the sections that follow.
  • Download and install the "Java" (or "All") distribution of NetBeans IDE. Go here to do so. Nothing other than that is required, i.e., no plugins, unless you have a "Java SE" distribution of NetBeans IDE, in which case you need to install the "Groovy and Grails" plugin from Tools | Plugins in the IDE. Grails support (and Groovy support) is native to NetBeans IDE, from the 6.5 release of the "Java" (or "All") distributions onwards.
  • Download, unzip, and set up Grails. Go here to do so. Set everything up as described in the Grails installation instructions. Make sure it works from the command line (i.e., run "grails" on the command line). Do not continue with these instructions until Grails works from the command line. Make sure that you download Grails using an ISP that is in a country that speaks the language that you want your Grails application to be localized in. It is unknown what happens if an ISP is in a country that has multiple official languages (e.g., Switzerland).

Step 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:
\

Step 3: Create the Domain Class

  • Right-click on the Domain classes node:

  • Name the domain class "Book" and (not shown in the screenshots below, alas), in every PACKAGE dropdown in the sceeens to follow, write BookDemo as the name of the package. Look at the Quick Start sample in Grails for the CMD-version of GRails. There the package is called org.example, here it is called BookDemo. NetBeans will not run the application if the correct package is not named in each sourcefile. Before running, check that the first line is "package BookDemo" in: BookController.groovy, BookControllerTests.groovy, Book.groovy, BookTests.groovy.
Afterwards, edit the class to fill in two Strings, "title" and "author", in the generated class. 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()

You should now see this:


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

Step 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 "def scaffold = Book". You should now see this:

Step 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:

  • You can also edit or delete an existing entry by clicking on the entry ID in the list view:

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


Congratulations! You've completed your first Grails application in NetBeans IDE. NetBeans IDE also supports syntax coloring and other related features for Groovy files and GSP files, as well as the ability to install Grails plugins. You can also use the Grails shell and you can run menu items that call the Grails tasks for creating WARs, creating stats, and upgrading Grails. You can also open existing Grails projects without needing to do anything special, i.e., no import process and no NetBeans-specific metadata is added to Grails projects in order to open them or to work with them.