8.1.4 Redirects and Chaining - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.1.4
8.1.4 Redirects and Chaining
Redirects
Actions can be redirected using the redirect controller method:class OverviewController { def login() {} def find() { if (!session.user) redirect(action: 'login') return } … } }
sendRedirect
method.The redirect
method expects one of:
- Another closure within the same controller class:
// Call the login action within the same class redirect(action: login)
- The name of an action (and controller name if the redirect isn't to an action in the current controller):
// Also redirects to the index action in the home controller redirect(controller: 'home', action: 'index')
- A URI for a resource relative the application context path:
// Redirect to an explicit URI
redirect(uri: "/login.html")
- Or a full URL:
// Redirect to a URL
redirect(url: "http://grails.org")
params
argument of the method:redirect(action: 'myaction', params: [myparam: "myvalue"])
params
object is a Map, you can use it to pass the current request parameters from one action to the next:redirect(action: "next", params: params)
redirect(controller: "test", action: "show", fragment: "profile")
Chaining
Actions can also be chained. Chaining allows the model to be retained from one action to the next. For example calling thefirst
action in this action:class ExampleChainController { def first() { chain(action: second, model: [one: 1]) } def second () { chain(action: third, model: [two: 2]) } def third() { [three: 3]) } }
[one: 1, two: 2, three: 3]
chainModel
map. This dynamic property only exists in actions following the call to the chain
method:class ChainController { def nextInChain() { def model = chainModel.myModel … } }
redirect
method you can also pass parameters to the chain
method:chain(action: "action1", model: [one: 1], params: [myparam: "param1"])