(Quick Reference)

afterInterceptor

Purpose

Allows interception of actions after they have executed, but before the view has rendered at the controller level.

Examples

Using a block:

class BookController {
	def afterInterceptor = { model ->
		model.foo = "bar"
	}
}

In the above example the first argument to block is model (a map of key value pairs passed to the view)

Using a map with a method reference:

class BookController {
	def afterInterceptor = [action:this.&invokeMe, only:['list']]	
	def invokeMe(model) {
		model.foo = "bar"
	}	
	def list = {
		[bookList:Book.list()]
	}
}

In this example we use the map syntax to constraint the {{afterInterceptor}} to execute only for the {{list}} action.

Description

To define an interceptor that is executed after an action use the afterInterceptor property:

def afterInterceptor = { model ->
       println "Tracing action ${actionUri}"
}

The after interceptor takes the resulting model as an argument and can hence perform post manipulation of the model or response.

An afterInterceptor may also modify the Spring MVC ModelAndView object prior to rendering. In this case, the above example becomes:

def afterInterceptor = { model, modelAndView ->
       println "Current view is ${modelAndView.viewName}"
       if(model.someVar) modelAndView.viewName = "/mycontroller/someotherview"
       println "View is now ${modelAndView.viewName}"
}

This allows the view to be changed based on the model returned by the current action. Note that the modelAndView may be null if the action being intercepted called redirect or render.