blob: 9a58e95c781d843c1ffd72bffa30c8dff1a84af3 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= Introduction to the Grails Web Framework
:jbake-type: tutorial
:jbake-tags: tutorials
:jbake-status: published
:icons: font
:syntax: true
:source-highlighter: pygments
:toc: left
:toc-title:
:description: Introduction to the Grails Web Framework - Apache NetBeans
:keywords: Apache NetBeans, Tutorials, Introduction to the Grails Web Framework
This document gets you started with link:http://www.grails.org[+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."
image::images/netbeans-stamp-80-74-73.png[title="Content on this page applies to the 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
|link:https://netbeans.org/downloads/index.html[+NetBeans IDE+] |version 7.2 Java or above
|link:http://www.oracle.com/technetwork/java/javase/downloads/index.html[+Java Development Kit (JDK)+] |version 7
|link:http://www.grails.org/Installation[+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 link:http://grails.org/Quick%20Start[+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).
. 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:
image::images/grails-65-4.png[]
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:
image::images/grails-65-8.png[]
. 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:
[source,java]
----
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":
image::images/grails-65-11.png[]
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:
image::images/grails-65-13.png[]
== 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:
image::images/grails-65-19.png[]
. 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:
image::images/grails-65-14.png[]
. Click New Book and then create a new entry:
image::images/grails-65-15.png[]
. When you click Create, note that you can edit or delete the entry:
image::images/grails-65-17.png[]
. ...and that the entry is reflected in the list of entries:
image::images/grails-65-18.png[]
link:/about/contact_form.html?to=3&subject=Feedback: NetBeans IDE 6.5 Grails Quick Start Guide[+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 link:https://netbeans.org/[+netbeans.org+], see link:../java/groovy-quickstart.html[+Introduction to Groovy+].