Grails 重定向会破坏参数类型
我的 Grails 代码有一个搜索函数,该函数在执行 findAllBy 查询后重定向到另一个控制器操作:
def results = Foo.findAllByBar(baz)
redirect(action: "result", params: [results: results])
findAllByBar 按预期返回带有模型的 ArrayList,但在重定向之后,接收操作会获取一个 String 数组。更糟糕的是,当只有一个结果时,它甚至没有得到一个数组,它只是得到一个字符串。
鉴于我必须迭代接收视图中的结果,在字符串上执行此操作将仔细地单独打印每个字母。我们都同意这可能不是理想的行为。
My Grails code has a search function that redirects to another controller action after performing a findAllBy query:
def results = Foo.findAllByBar(baz)
redirect(action: "result", params: [results: results])
findAllByBar returns an ArrayList with models, as expected, but after the redirect the receiving action gets a String array. Worse, when there is only one result it doesn't even get an array, it just gets a String.
Given that I have to iterate through the results in the receiving view, doing it on a string will meticulously print every letter individually. We can all agree that that's probably not the ideal behaviour.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重定向会产生一个新的 GET 请求,其参数位于查询字符串中,例如 /controller/result?foo=bar&baz=123 - 您不能将对象放在那里,因为它只是一个字符串。
您可以将对象的 id 放入参数中,并将它们加载到
result
操作中:或者将它们放入 Flash 范围中:
A redirect results in a new GET request with the parameters in the querystring, e.g. /controller/result?foo=bar&baz=123 - you can't put objects there since it's just a string.
You could put the ids of the objects in the params and load them in the
result
action:or put them in Flash scope:
听起来您想使用链方法而不是重定向方法。 Chain 允许您将模型作为参数传递,类似于渲染。
一个例子是:
这是一个获取更多信息的链接:
http://www.grails.org/doc/latest/ref/Controllers /chain.html
It sounds like you want to use the chain method instead of the redirect method. Chain lets you pass a model as a parameter similar to render.
An example would be:
Heres a link for further information:
http://www.grails.org/doc/latest/ref/Controllers/chain.html