Oracle GlassFish Server 3.0.1 Scripting Framework Guide

Chapter 2 Developing Grails Applications

This chapter introduces Groovy and Grails, a JavaTM technology-based alternative to scripting.

Introduction to Groovy and Grails

Groovy is a dynamic, object-oriented language for the Java Virtual Machine, which builds on the strengths of Java but has additional features inspired by languages such as Python, Ruby, and Smalltalk. For more information about Groovy, see the Groovy Project page.

Grails is an open source web application framework that leverages the Groovy language and complements Java web development. Grails is a standalone development environment that can hide all configuration details or allow integration of Java business logic. It provides easy-to-use tools to build web applications in Groovy. For more information about Grails, see the Grails Project page.

Installing Grails

Grails is available as an IPS package from GlassFishTM Server Update Tool. To develop and deploy Grails applications on GlassFish Server, you must first install the Grails module.

ProcedureTo Install the Grails Module

  1. Install the Grails add-on component that is available from Update Tool.

    For information about Update Tool, see the Oracle GlassFish Server 3.0.1 Installation Guide.

  2. Create a GRAILS_HOME environment variable that points to the Grails directory, as-install/grails.

  3. Add the as-install/grails/bin directory to the PATH environment variable.

Example 2–1 Setting UNIX Environment Variables

On Solaris, Linux, and other operating systems related to UNIX, use the following commands to set the GRAILS_HOME and PATH environment variables:

export GRAILS_HOME=as-install/glassfish/grails
chmod a+x $GRAILS_HOME/bin/*

Example 2–2 Setting Windows Environment Variables

On the Windows operating system, use the following commands to set the GRAILS_HOME and PATH environment variables:

set GRAILS_HOME=C:\GlassFish\grails

Creating a Simple Grails Application

To create and run a simple helloworld application, perform the following tasks:

For more information on creating Grails applications, see the Grails Quick Start guide.

ProcedureTo Create the helloworld Application

  1. Change to the as-install/grails/samples directory.

  2. Run the grails create-app helloworld command.

    The grails create-app command creates a helloworld application that you can modify.

ProcedureTo Create the hello Controller

  1. Change to the as-install/grails/samples/helloworld directory.

  2. Run the grails create-controller hello command.

    The grails create-controller command creates a controller file that you can modify in the /grails/samples/helloworld/grails-app/controllers directory:

  3. Edit the generated HelloController.groovy file so that it looks as follows:

    class HelloController {
           def world = {
                    render "Hello World!"
        //def index = { }

ProcedureTo Run a Grails Application Using Standard Deployment

  1. Change to the application directory. For example:

    cd as-install/grails/samples/helloworld
  2. Create the WAR file using the following command:

    grails war

    This command creates a WAR file, helloworld-0.1.war in the helloworld application directory. The WAR file contains all the application's dependencies, and various jar files.

  3. Deploy the WAR file in one of the following ways:

    • In the Administration Console, open the Applications component, go to the Web Applications page, select the Deploy button, and type the path to the WAR file.

      For example:


    • Use the asadmin deploy command from command line to deploy the WAR file. For example:

      asadmin deploy helloworld-0.1.war

      Note –

      Depending on the configuration, you might be prompted for the asadmin password at this time.

  4. To test your application, open http://host:port/war-file-name in your browser.

    Do not include the .war extension. For example:


    You should see a screen that shows the following message:

    Welcome to Grails

    Clicking the HelloController link should change the display to the following message:

    Hello World!
See Also

For details about the Administration Console, see the GlassFish Server online help.

For details about the asadmin deploy command, see the Oracle GlassFish Server 3.0.1 Reference Manual.

For details about the Grails commands, see the Grails Quick Start guide.

For general information about deployment, see the Oracle GlassFish Server 3.0.1 Application Deployment Guide.