Getting your Grails on: Grails Demo

 

This is an excerpt from my upcoming book Getting your Grails on, based on my presentation from Kansas City Developers Conference.

In this last part, we will get your machine ready to use Grails and demonstrate what you can do with the Grails and the Groovy/Grails Tool Suite.

Install Java

Step 1: To install Grails 3.0, you need to install Java JDK 1.7 or higher. We can go here and download the latest version (Java 8u51) and install.

Download_Java

 

Once you have it downloaded you can install the Java Development kit and then move on to Grails.

Install Grails

The next step to installing Grails is to download the correct installation. You can follow along with my installation on my Windows machine.

Step 2: Download the current version of Grails (currently 3.0.4).

Download_Grails

Setup Environment

Step 3: Setup up environment for Java and Grails.

JAVA_HOME is set to the directory that you installed the JDK to C:\Java\Java8

GRAIL_HOME is set to the directory that you copied Grails to C:\Java\grails-3.0.4

PATH add the two bin directories from the JAVA_HOME and GRAILS_HOME to your PATH variable.

These both can be tested with the java -version command and the grails -version command.

Installing Groovy/Grails Tool Suite

Next up we want to install the Groovy/Grails Tool Suite (GGTS). This is essentially Eclipse with some additional plug ins for Groovy and Grails.

Step 4: Download and install Groovy/Grails Tool Suite

Creating your first Grails application

Next we can create a Grails application, GGTS makes this really easy.

Step 5: Create Grails application.

Running your Grails application

To test out the application, we can run it our first step is to click on the Grails button to launch the command window.

 

Then we enter the “run-app” command to run the application.

 

You should see the output in the console.

 

And your first application should look something like this.

 

Grails Domain classes

To create a domain class you simply select that from the menu.

 

Then enter the name of the Controller and it should create two files for you. The first file is the domain file in my case Firstdomain:

package myfirstgrailsapp

class Firstdomain {

     static constraints = {
     }
}

And the second one is a unit test file appending Spec onto the file name and putting it in the test folder.

Grails Controllers

Once again the controller can be created by selecting it from the menu. Then enter the information on the screen.

Then GGTS creates a simple controller for our application in the controller folder. And the controller file looks like this.

package myfirstgrailsapp

class FirstcontrollerController {

     def index() { }
}

Grails Server Pages

To create a GSP we simply need to open the New dialog and select GSP.

Then we can select the folder under views folder for our first controller.

 

Paste in the following code to your GSP.

<!DOCTYPE html> <html> <head> <meta name=“layout” content=“main”/> <title>Welcome to Grails</title> </head> <body> My first GSP! </body> </html>

Open your browser to view your page and you should see this.

Grails Services

Service’s are created in a similar fashion. Here is the screen to create them.

It will create the two files for you. Here is what the test file looks like.

package myfirstgrailsapp

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
* See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
*/
@TestFor(FirstserviceService)
class FirstserviceServiceSpec extends Specification {

     def setup() {
     }

     def cleanup() {
    }

     void “test something”() {
     }
}

Conclusion

I hope you enjoyed this book. This was originally a presentation I did for the Kansas City Developers Conference. If I can help you out in any way please let me know. Contact me at my website for MyITCareerCoach.com, or on Twitter, Facebook or LinkedIn, I am always happy to help people out.

No comments yet.

Leave a Reply

Powered by WordPress. Designed by Woo Themes