Sun GlassFish Enterprise Server v3 Prelude Developer's Guide

Developing Grails Applications

This section shows you how to get started using Groovy on Grails on the Enterprise Server by covering the following topics:

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 Groovy.

Grails is an open-source web application framework that leverages the Groovy language and complements Java web development. Grails is a stand-alone development environment that can hide all configuration details or allow integration of Java business logic. For more information about Grails, see Grails.

Installing Grails

To develop and deploy Grails applications on the Enterprise Server, first install the Grails plug-in module.

ProcedureInstalling the Grails Plug-in Module

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

    For information about the Update Tool, see the Sun GlassFish Enterprise Server v3 Prelude 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 7–1 Setting UNIX Environment Variables

On Solaris, Linux, and other operating systems related to UNIX, use the following commands for steps 2 and 3:


set GRAILS_HOME=~/glassfish/grails
export GRAILS_HOME
cd $GRAILS_HOME
set PATH=$GRAILS_HOME/bin:$PATH
export PATH
chmod a+x $GRAILS_HOME/bin/*


Example 7–2 Setting Windows Environment Variables

On the Windows operating system, use the following commands for steps 2 and 3:


set GRAILS_HOME=C:\GlassFish\grails
set PATH=%GRAILS_HOME%\bin;%PATH%

Creating a Simple Grails Application

To create the helloworld application, perform both these tasks:

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

ProcedureCreating the helloworld Application

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

  2. Run the grails create-app helloworld command.

    The grails create-app command creates an application framework that you can modify.

ProcedureCreating the hello Controller

  1. Go 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.

  3. Edit the generated HelloController.groovy file so it looks like this:

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

Deploying and Running a Grails Application

To deploy and run your application, perform one of these tasks:

ProcedureRunning a Grails Application Using run-app

  1. Go to the application directory.

    For example, go to the as-install/grails/samples/helloworld directory.

  2. Run the grails run-app command.

    The grails run-app command starts the Enterprise Server in the background and runs the application in one step. You don't need to create a WAR file or deploy your application.

  3. To test your application, point your browser to http://host:port/app-dir-name.

    For example, point to http://localhost:8080/helloworld. You should see a screen that begins, “Welcome to Grails.” Selecting the HelloController link should change the display to, “Hello World!”

See Also

For details about the grails run-app command, see the Sun GlassFish Enterprise Server v3 Prelude Reference Manual.

ProcedureRunning a Grails Application Using Standard Deployment

  1. Go to the application directory.

    For example, go to the as-install/grails/samples/helloworld directory.

  2. Create the WAR file in one of the following ways:

    • Run the grails war command.

      This command creates a large WAR file containing all the application's dependencies.

    • Run the grails shared-war command.

      This command creates a small WAR file, but requires referencing of the Grails library JAR at deployment.

    In the helloworld application, this step creates the helloworld-0.1.war file.

  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.

      The path to the helloworld WAR file is as-install/grails/samples/helloworld/helloworld-0.1.war.

      If you used the grails shared-war command, specify the as-install/grails/lib/glassfish-grails.jar file in the Libraries field.

    • On the command line, use the asadmin deploy command and specify the WAR file. For example:


      asadmin deploy helloworld-0.1.war

      If you used the grails shared-war command, specify the libraries using the --libraries option. For example:


      asadmin deploy --libraries $GRAILS_HOME/lib/glassfish-grails.jar helloworld-0.1.war
  4. To test your application, point your browser to http://host:port/war-file-name. Do not include the .war extension.

    For example, point to http://localhost:8080/helloworld-0.1. You should see a screen that begins, “Welcome to Grails.” Selecting the HelloController link should change the display to, “Hello World!”

See Also

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

For details about the asadmin deploy command, see the Sun GlassFish Enterprise Server v3 Prelude Reference Manual.

For details about the grails war and grails shared-war commands, see the Grails Quick Start.

For general information about deployment, see the Sun GlassFish Enterprise Server v3 Prelude Application Deployment Guide.