Grains 控制器中操作的重复代码
我已经发布了这个问题,但我意识到答案不是我想要的。想象一下这个控制器:
class exampleController{
def action1 = {
...
[lala: lala, lele: lele]}
...
}
def action15 = {
...
[lala: lala, lele: lele]
}
我希望能够在此控制器中的所有操作中返回相同的参数。想象一下:
def book = Book.findAllByIsbn(Isbn.get(1))
[book: book]
除了在所有操作上编写相同的代码之外,还有什么方法可以做到这一点吗?我已经尝试过这种方法,但它不起作用:
def action5 = {getModel()}
private getModel() {
def book = Book.findAllByIsbn(Isbn.get(1))
[book: book]
}
}
它不起作用,因为我的想法是,他不接受多个 [return1: aaa, return2: bbb]。请问有什么建议吗?我还尝试过如下过滤器: Grails 控制器对所有操作重复代码 但我无法让它发挥作用。如果可能的话,我希望获得有关任何解决方案的详细解释:p 提前致谢, VA
I have already posted this question, but i realised the aswer was not what i was looking for. Imagine this controller:
class exampleController{
def action1 = {
...
[lala: lala, lele: lele]}
...
}
def action15 = {
...
[lala: lala, lele: lele]
}
I want to be able to return in all the action in this controller the same params. Imagining this:
def book = Book.findAllByIsbn(Isbn.get(1))
[book: book]
Is there any way of doing this, besides writing all the same code on all the actions? I have tried this method and it isnt working:
def action5 = {getModel()}
private getModel() {
def book = Book.findAllByIsbn(Isbn.get(1))
[book: book]
}
}
It is not working because, and my thought is, he doest accept multiple [return1: aaa, return2: bbb]. Any suggestion please ? I have also tried filters like in here: Grails controllers repeated code for all actions
but i couldnt managed to make it work. I would apreciated a detailed explanaintion about any of the solutions if possible:p Thanks in advanced,
VA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以它不是同一个模型,而是一个有重复部分的模型。
你应该知道返回值是一个普通的
Map
。因此,返回值可以像
return getCommonModel() + [book: currentBook]
一样构造,其中getCommonModel()
返回另一个 Map。So it's not the same model, but a model with a repeated part.
You should know that the return value is an ordinary
Map
.So, return value can be constructed like
return getCommonModel() + [book: currentBook]
wheregetCommonModel()
returns another Map.如果您想从所有操作返回相同的模型,则此方法应该可行:
如果您想从所有操作返回相同的模型并渲染相同的视图,则可以返回相同的
ModelAndView
来自每个操作,但是我会问,如果它们都在做完全相同的事情,为什么需要单独的操作?我不太明白你的假设
如果您建议
getModel()
只能返回一个模型仅凭一个条目,我发现很难相信。您能否对此进行详细说明,或者发布一些更多信息(例如堆栈跟踪、单元测试)来显示它如何/为什么不起作用?更新
阅读下面的评论后,我想我终于明白了您想要实现的目标,即将
getModel()
(上面)返回的模型附加到各种其他操作返回的模型中。这是否有效:只有当您想在单个控制器中添加书籍模型时,此方法才有效。如果您想在多个控制器中添加书籍模型,可以通过以下方式执行此操作:
addBookModel
放入控制器扩展的抽象类中addBookModel
放入混合的类中 -与控制器一起使用(使用@Mixin
),addBookModel
放入在控制器操作之后执行的过滤器中If you want to return the same model from all your actions, this approach should work:
If you want to return the same model and render the same view from all your actions, you could return the same
ModelAndView
from each action, but then I would ask why do you need separate actions if they're all doing exactly the same thing?I don't really understand your hypothesis
If your suggesting that
getModel()
can only return a model with a single entry, I find that very hard to believe. Can you elaborate a bit on this, or post some more information (e.g. stacktrace, unit test) that shows how/why it's not working?Update
After reading your comments below I think I finally understand what you want to achieve, which is to append the model returned by
getModel()
(above) to the model returned by various other actions. Does this work:This approach will only work when you want to add the book model within a single controller. If you want to add the book model in several controllers you can do this by:
addBookModel
in an abstract class that the controllers extendaddBookModel
in a class that is mixed-in with the controllers (using@Mixin
)addBookModel
in a filter that is executed after the controller actions如果您在多个页面中使用完全相同的模型。我建议您使用标签库。
If you are using exact same model in multiple pages. I would recommend you use a taglib for it.