This page last changed on Mar 15, 2006 by [email protected].

Controller Dynamic Methods & Properties

Properties

actionUri

Description

The relative Uri of the action

controllerUri

Description

The relative Uri of the controller

flash

Description

A map that allows temporary storage of objects for the next request and the next request only. The subequent request will clear the storage of any variables stored in the first
request. This is a convenience map that allows you to store information temporarily, which is very convenient when using redirection or chaining.

Example
flash['message'] = 'hello world!'

grailsAttributes

Description

An instance of org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes that provides convenience methods for the current grails request

Example
def templateUri = grailsAttributes.getTemplateUri("myTemplate",request)

params

Description

A map of the available request parameters and/or controller parameters

Example
def a = Account.get( params["id"] )

request

Description

The HttpServletRequest instance for the request. Request attributes can be accessed with the Map syntax provided by groovy

Example
request['anAttribute']
request.getHeader("user-agent")

response

Description

The HttpServletResponse instance

Example
response.setHeader("myheader", "myvalue")

servletContext

Description

The ServletContext instance as per the servlet API

Example
def myUrl = servletContext.getResource("/path/to/url")

session

Description

Provides access to a map of the HttpSession instance

Example
def loggedUser = session["loggedUser"]

Methods

bindData

Description

Binds data to a target instance performing type conversion if necessary. Useful for binding request parameters to properties of objects

Parameters
  • target - The target object to bind to
  • params - The parameters to bind to the target object
Examples
bindData(target, this.params) // binds request parameters to a target object

chain

Description

Chains the model (The model is retained from one action to the next) from one action to the next allowing you to build up the model from an action chain.

Parameters
  • uri - The full uri to redirect to (example /book/list, book/show/2)
  • controller - The controller to redirect to, defaults to the current controller if not specified
  • action - The action to redirect to, either a string name or a reference to an action within the current controller
  • id - The id to use in redirection
  • params - Parameters to pass to the action redirected to.
  • model (required) - The model to chain to the next action
  • params (optional) - Parameters to pass to the action chained to.
Examples
chain(action:"details",model:[book:new Book(title:'The Shawshank Redemption')])

redirect

Description

Redirects the current action to another action, optionally passing parameters and/or errors

Parameters
  • uri - The full uri to redirect to (example /book/list, book/show/2)
  • controller - The controller to redirect to, defaults to the current controller if not specified
  • action - The action to redirect to, either a string name or a reference to an action within the current controller
  • id - The id to use in redirection
  • params - Parameters to pass to the action redirected to.
Examples
redirect(uri:"book/list")
redirect(action:"show")
redirect(controller:"book",action:"list")
redirect(action:"show",id:4, params:[author:"Stephen King"])

render

Description

A multi-purpose method for rendering responses to the client which is best illustrated with a few examples!

Parameters
  • text (optional) - The text to render
  • builder (optional) - The builder to use when rendering markup
  • view (optional) - The view to delegate the rendering to
  • template (optional) - The template to render
  • var (optional) - The name of the variable to be passed into a template, defaults to the groovy default argument 'it' if not specified
  • bean (optional) - The beanto use in rendering
  • model (optional) - The model to use in rendering
  • collection (optional) - For rendering a template against each item in a collection
  • contentType (optional) - The contentType of the response
  • encoding (optional) - The encoding of the response
Examples
// renders text to response
render "some text"

// renders text for a specified content-type/encoding
render(text:"<xml>some xml</xml>",contentType:"text/xml",encoding:"UTF-8") 

// render a template to the response for the specified model
render(template:"book",model:[book:new Book(title:'The Shining',author:'Stephen King')]) 

// render each item in the collection using the specified template
render(template:"book",collection:[b1, b2, b3]) 

// render a template to the response for the specified bean
render(template:"book",bean:new Book(title:'The Shining',author:'Stephen King')) 

// render the view with the specified model
render(view:"viewName",model:[book:new Book(author:'Stephen King')]) 

// render the view with the controller as the model
render(view:"viewName"
)
// render some markup to the response
render {
    div(id:"myDiv") {
           "some body text"
    }
}

// render some XML markup to the response
render(contentType:"text/xml") {
    books {
         for(b in books) {
            book(title:b.title,author:b.author)
         }
    }
}

// render an OpenRico (http://www.openrico.org) response with the builder attribute:
def b = new Book(title:"Kiss the Girls", author:"James Patterson")
render(builder:"rico") {
     object(id:"bookUpdater") {
            book(author:b.title,author:b.author)
     }
}
Document generated by Confluence on Mar 29, 2006 08:46