Grails - Creating an Application



Description

We can do Grails development with just JDK and Grails installed on our machine. In this chapter, we will see how to create our first Grails application. For that, you must be familier with the usage of Grails command that is defined in the following way:

grails <<command_name>>

To create an application, open the command line and type the following command:

grails create-app helloworld

The above command will create a directory named helloworld.

Now, let's display a simple message "Welcome to Grails" on the browser. The important point in Grails application that a browser talks to is a Controller. To create a controller, navigate to your directory and type the below commands:

C:\helloworld> grails 
grails> create-controller hello

This will create a controller in the grails-app/controllers/helloworld directory named HelloController.groovy.

Now, let's edit the generated HelloController.groovy file so it looks like this:

package helloworld
class HelloController {
    def index() { 
        render "Welcome to Grails"
    }
}

Go to your command line and type the below command:

grails> run-app

The prompt should look like this:

Prompt

The run-app command will host your application by starting an embedded server on port 8080. Now, you can access your application at the URL http://localhost:8080/.

The below image shows an intro page of Grails provided by grails-app/view/index.gsp file.

Creating Application

To see our custom page displaying "Welcome to Grails" message, click on the HelloController link.

Advertisements