Grails 重定向会破坏参数类型

发布于 2024-12-01 20:16:15 字数 351 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-12-08 20:16:15

重定向会产生一个新的 GET 请求,其参数位于查询字符串中,例如 /controller/result?foo=bar&baz=123 - 您不能将对象放在那里,因为它只是一个字符串。

您可以将对象的 id 放入参数中,并将它们加载到 result 操作中:

def action1 = {
   def results = Foo.findAllByBar(baz)
   redirect(action: "result", params: [resultIds: results.id.join(',')])
}

def result = {
   def resultIds = params.resultIds.split(',')*.toLong()
   def results = Foo.getAll(resultIds)
}

或者将它们放入 Flash 范围中:

def action1 = {
   flash.results = Foo.findAllByBar(baz)
   redirect(action: "result")
}

def result = {
   def results = flash.results
}

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:

def action1 = {
   def results = Foo.findAllByBar(baz)
   redirect(action: "result", params: [resultIds: results.id.join(',')])
}

def result = {
   def resultIds = params.resultIds.split(',')*.toLong()
   def results = Foo.getAll(resultIds)
}

or put them in Flash scope:

def action1 = {
   flash.results = Foo.findAllByBar(baz)
   redirect(action: "result")
}

def result = {
   def results = flash.results
}
农村范ル 2024-12-08 20:16:15

听起来您想使用链方法而不是重定向方法。 Chain 允许您将模型作为参数传递,类似于渲染。
一个例子是:

chain(action:'result',model:[results:results])

这是一个获取更多信息的链接:
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:

chain(action:'result',model:[results:results])

Heres a link for further information:
http://www.grails.org/doc/latest/ref/Controllers/chain.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文