6.1.4 重定向和链(Chaining) - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.1.4 重定向和链(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")
重定向
操作可以使用控制器的redirect方法进行重定向:class OverviewController { def login() {} def find() {
if (!session.user)
redirect(action: 'login')
return
}
…
}
}sendRedirect方法来工作的。redirect方法的重定向目标用法如下:
- 同一控制器类的另外一个闭包:
// Call the login action within the same class redirect(action: login)
- 操作的名称(如果要重定向的操作名不在同一个控制器内,还需要制定控制器名):
// Also redirects to the index action in the home controller redirect(controller: 'home', action: 'index')
- 一个相对于应用上下文路径的URI
// Redirect to an explicit URI
redirect(uri: "/login.html")- 或者一个完整的URL:
// Redirect to a URL
redirect(url: "http://grails.org")params来实现:redirect(action: 'myaction', params: [myparam: "myvalue"])params对象就是一个Map,因此你可以使用它将当前的请求参数从一个操作传递到下个操作: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"])
链(Chaining)
操作同样可以作为一个链。在从一个操作传递到下个操作的时候,链一直保留着其模型。比如下面调用first操作的示例: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映射来访问。此动态属性只存在于调用chain方法的操作中:class ChainController { def nextInChain() {
def model = chainModel.myModel
…
}
}redirect方法一样,你可以传递参数给chain方法:chain(action: "action1", model: [one: 1], params: [myparam: "param1"])

