Introduction to the Grails Web Framework
This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
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."
To follow this tutorial, you need the following software and resources.
Software or Resource | Version Required |
---|---|
version 7.2 Java or above |
|
version 7 |
|
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.
-
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).
-
Choose File > New Project (Ctrl-Shift-N) and then select "Grails Application" from the "Groovy" category. Click Next.
-
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.
-
Right-click on the Domain Classes node and choose New > Grails Domain Class.
-
Name the domain class "Book" and click Finish. The "Book.groovy" domain is created in the Domain Classes node.
-
Open the Book class and then fill in two Strings, "title" and "author". You should now see this:
-
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:
-
Right-click the Controllers node and choose New > Grails Controller.
-
Type "Book" in Class Name and notice that you are shown that the generated class will be called "BookController":
Click Finish. The controller is generated.
-
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.
-
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. 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:
-
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:
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.