Learn Java for Web Development

(Tina Meador) #1
CHAPTER 7: Rapid Web Development with Grails 331

The first thing you will notice in the BookController code illustrated in Figure 7-28 is that a Grails
controller is a plain Groovy class that does not extend any class or implement any interface. The
next thing you will notice is that the BookController has eight actions.


   Create action
 Delete action
 Edit action
 Index action
 List action
 Save action
 Show action
 Update action

These actions are the closure properties of the controller. All the work in the controller is done in the
action. Every closure declared in a controller is an action and can be accessed via a URL that, by
default, is mapped to controller actions. The first part of the URL represents the controller name,
and the second part of the URL represents the action name. In the sections that follow, you will
look deeper into each of these actions, but before that, it is necessary to understand how to exit a
controller action. There are three options to properly exit a controller action.


   Calling the render() method
 Calling the redirect() method
 Returning a model or null

In the section that follows, you will look into these three options before probing each of the actions in
the BookController shown in Figure 7-28.


Calling the render( ) Method

The first option to exit a controller action is to call the render() method to render a view or a textual
response. For the sake of understanding how render() method works, Listing 7-13 shows a simple
Grails controller that, when invoked, greets you.


Listing 7-13. Rendering a Textual Response


package chapter5
class HelloController {
def index() {
render 'hello'
}
defshow(){}
def someOtherAction(){}
}

Free download pdf