(Quick Reference)

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 } … } }

Internally the redirect method uses the HttpServletResponse object's 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")

Parameters can optionally be passed from one action to the next using the params argument of the method:

redirect(action: 'myaction', params: [myparam: "myvalue"])

These parameters are made available through the params dynamic property that accesses request parameters. If a parameter is specified with the same name as a request parameter, the request parameter is overridden and the controller parameter is used.

Since the 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)

Finally, you can also include a fragment in the target URI:

redirect(controller: "test", action: "show", fragment: "profile")

which will (depending on the URL mappings) redirect to something like "/myapp/test/show#profile".

重定向

操作可以使用控制器的redirect方法进行重定向:

class OverviewController {

def login() {}

def find() { if (!session.user) redirect(action: 'login') return } … } }

本质上redirect方法是使用HttpServletResponse对象的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来实现的。如果一个参数跟请求参数同名,那么请求参数将被覆盖,控制器的参数将被优先使用。

由于这个params对象就是一个Map,因此你可以使用它将当前的请求参数从一个操作传递到下个操作:

redirect(action: "next", params: params)

最后,你还可以在目标URI中包含片段(fragment):

redirect(controller: "test", action: "show", fragment: "profile")

将重定向到类似于 "/myapp/test/show#profile" 的目标(依赖于URL映射)。

Chaining

Actions can also be chained. Chaining allows the model to be retained from one action to the next. For example calling the first 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]) } }

results in the model:

[one: 1, two: 2, three: 3]

The model can be accessed in subsequent controller actions in the chain using the chainModel map. This dynamic property only exists in actions following the call to the chain method:

class ChainController {

def nextInChain() { def model = chainModel.myModel … } }

Like the 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"])