2.4 A Hello World Example
Let’s now take the new project and turn it into the classic "Hello world!" example. First, change into the "helloworld" directory you just created and start the Grails interactive console:
$ cd helloworld
$ grails
You should see a prompt that looks like this:
What we want is a simple page that just prints the message "Hello World!" to the browser. In Grails, whenever you want a new page you just create a new controller action for it. Since we don’t yet have a controller, let’s create one now with the create-controller command:
grails> create-controller hello
Don’t forget that in the interactive console, we have auto-completion on command names. So you can type "cre" and then press <tab> to get a list of all create-*
commands. Type a few more letters of the command name and then <tab> again to finish.
The above command will create a new controller in the grails-app/controllers/helloworld
directory called HelloController.groovy
. Why the extra helloworld
directory? Because in Java land, it’s strongly recommended that all classes are placed into packages, so Grails defaults to the application name if you don’t provide one. The reference page for create-controller provides more detail on this.
We now have a controller so let’s add an action to generate the "Hello World!" page. In any text editor, edit the new controller — the HelloController.groovy
file — by adding a render line. The edited file’s code should look like this:
package helloworld
class HelloController {
def index() {
render "Hello World!"
}
}
The action is simply a method. In this particular case, it calls a special method provided by Grails to render the page.
Job done. To see your application in action, you just need to start up a server with another command called run-app:
grails> run-app
This will start an embedded server on port 8080 that hosts your application. You should now be able to access your application at the URL http://localhost:8080/ - try it!
To set a context path for your application, you can add a configuration property in grails-app/conf/application.yml
:
server:
servlet:
context-path: /helloworld
With the above configuration in place the server will instead startup at the URL http://localhost:8080/helloworld/.
Alternatively, you can also set the context path via the command line:
grails> run-app -Dgrails.server.servlet.context-path=/helloworld
If you see the error "Server failed to start for port 8080: Address already in use", then it means another server is running on that port. You can easily work around this by running your server on a different port using run-app -port=9090 . '9090' is just an example: you can pretty much choose anything within the range 1024 to 49151. |
The result will look something like this:
This is the Grails intro page which is rendered by the grails-app/view/index.gsp
file. It detects the presence of your controllers and provides links to them. You can click on the "HelloController" link to see our custom page containing the text "Hello World!". Voila! You have your first working Grails application.
One final thing: a controller can contain many actions, each of which corresponds to a different page (ignoring AJAX at this point). Each page is accessible via a unique URL that is composed from the controller name and the action name: /<appname>/<controller>/<action>. This means you can access the Hello World page via /helloworld/hello/index, where 'hello' is the controller name (remove the 'Controller' suffix from the class name and lower-case the first letter) and 'index' is the action name. But you can also access the page via the same URL without the action name: this is because 'index' is the default action. See the end of the controllers and actions section of the user guide to find out more on default actions.