Grails Cookbook - A collection of tutorials and examples

Grails Tutorial for Beginners - Starting with Controllers

When learning Grails or any other framework, I recommend to study each components separately first. Rather than tackling multiple topics at the same time. For Grails beginners, I recommend to start with controllers. This is one of the simplest to understand and you can see results right away!

You can follow this guide if you have not setup yet your computer for Grails development. Create a Grails project called "sample" for following the examples below.

Create a Controller

To create a controller, right click on your project name and select New » Controller.
c1
Next we need to specify the name of your controller. In this case we will use "Hello". But it is best practice to always use packages when creating a class. We will use the package "asia.grails.tutorial". You need to combine the package name and the controller name to come up with the complete name for the controller.
c2

If you are new to Java packages, it is a way of grouping related classes together. You can read this entry in Wikipedia to understand more about Java packages.
The IDE will create the Controller. Notice that the suffix "Controller" was appended to the name of the class. Notice also in the view section that a folder "hello" was created.
c3

Hello World and Routing Basics

Edit the controller with the these contents:

package asia.grails.tutorial
class HelloController {
	def index() { 
		render "Hello World"
	}
	def hi() { 
		render "How are you?"
	}
}

Run the application and visit the URL http://localhost:8080/sample/hello
c4
Visit the URL http://localhost:8080/sample/hello/index will render the same result
c5
Visit the URL http://localhost:8080/sample/hello/hi will render the output of the second controller method
c6
As you can see, this is the normal routing for Grails applications "http://localhost:8080/context/controller/action". You don't have to configure anything as this is the default behavior. If you want for example to have a URL like "http://localhost:8080/sample/blog/view", then you need to create a controller named "Blog" and create a method "View". Note that controller's methods are also called actions.
The method index is a special one. If you did not specify the method in the URL, the index is the defult action that will be invoked.
Notice also the code render "Hello World". This is the simplest method to return results to your browser.

Rendering Results Using GSPs

It is not practical to write results inside your controller's actions. The normal way is to utilize GSPs. The term GSP and view can be interchangeable.

Edit the controller code and remove the render code:

package asia.grails.tutorial
class HelloController {
	def index() { 
	}
	def hi() {
	}
}

and under hello directory in views, create index.gsp and hi.gsp.
c7

index.gsp

<!DOCTYPE html>
<html>
	<head>
		<meta name="layout" content="main"/>
		<title>Welcome to Grails</title>
	</head>
	<body>
		Hello World
	</body>
</html>

hi.gsp

<!DOCTYPE html>
<html>
	<head>
		<meta name="layout" content="main"/>
		<title>Welcome to Grails</title>
	</head>
	<body>
		How are you?
	</body>
</html>

Visiting the URLs http://localhost:8080/sample/hello/index and http://localhost:8080/sample/hello/hi
c8
c9
As you could see, when your action's code does not contain or execute "render", the GSP of the same name as the action is invoked from the view directory of the same name as the controller. Again, there is no configuration required since this is the default behavior.
Notice also that there is already a default styling and design. This will be discussed in future tutorials.

Rendering a Specific GSP


Sometimes you need to specify the GSP you want to be rendered and not follow the convention. For example, if you are reusing the same GSP for different actions.
You can invoke a specific GSP within the same controller just by invoking the method render and specifying a view parameter.
Modify your "hi" action such that:
package asia.grails.tutorial
class HelloController {
	def index() { 
	}
	def hi() {
		render (view:'index.gsp')
	}
}
When "hi" action is invoked, the index.gsp view will be displayed.

Remarks

Thanks to Grails' adherence to convention over configuration principles, working with it is pain free. As shown above, working with controllers is also straightforward and intuitive.

List of Tutorials for Beginners

Tags: action, beginner, controller, gsp, tutorial