Grails 控制器可以从基类扩展吗?如何让 grails 不会爆炸?

发布于 2024-09-03 10:30:19 字数 959 浏览 0 评论 0原文

我编写了一个基类来帮助更快地构建控制器并消除重复。它提供了一些辅助方法、默认操作和一些元编程,使这些东西更容易构建。

基类中的这些方法之一是这样的:

def dynamicList(Class clazz) {
    def model = new LinkedHashMap()
    model[getMapString(clazz) + "s"] = list(clazz)
    model[getMapString(clazz) + "sTotal"] = count(clazz)

    model
}

调用它的操作(也在基类中)是这样的:

def list = {
    dynamicList(clazz)
}

不幸的是,当我在部署应用程序时去继承基类的控制器子类中列出操作时,我遇到了这个例外:

groovy.lang.MissingMethodException: No signature of method: groovy.lang.MissingMethodException.dynamicList() is applicable for argument types: (java.lang.Class) values: [class project
.user.User]

    at project.user.UserController$_closure1.doCall(UserController.groovy:18)

    at project.user.UserController$_closure1.doCall(UserController.groovy)

    at java.lang.Thread.run(Thread.java:619)

我怎样才能把 grails 放在头上并告诉它做我想要它做的事情?我的控制器单元测试运行得很好,所以 grails 的运行时完全是错误的:/

Ken

I wrote a base class to help build my controllers more quickly and to remove duplication. It provides some helper methods, default actions and some meta programming to make these things easier to build.

One of those methods in the base class is like this:

def dynamicList(Class clazz) {
    def model = new LinkedHashMap()
    model[getMapString(clazz) + "s"] = list(clazz)
    model[getMapString(clazz) + "sTotal"] = count(clazz)

    model
}

The action that calls it, also in the base class, is this:

def list = {
    dynamicList(clazz)
}

Unfortunately, when I go to list action in the controller subclass that inherits the base class when my application is deployed, I get this exception:

groovy.lang.MissingMethodException: No signature of method: groovy.lang.MissingMethodException.dynamicList() is applicable for argument types: (java.lang.Class) values: [class project
.user.User]

    at project.user.UserController$_closure1.doCall(UserController.groovy:18)

    at project.user.UserController$_closure1.doCall(UserController.groovy)

    at java.lang.Thread.run(Thread.java:619)

How can I hit grails over the head and just tell it do what I want it to do? My controller unit tests run just fine, so grails' run-time is totally at fault :/

Ken

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

污味仙女 2024-09-10 10:30:19

您确定您的继承一切正常并且您已经运行了干净的 grails 等吗?你所描述的情况应该可以正常工作。

Are you sure your inheritance is all proper and you've run grails clean etc.? The situation you've described should work just fine.

帅冕 2024-09-10 10:30:19

这是整个代码吗?您可能会在 dynamicList() 中调用 list() 时遇到问题,因为它与操作匹配。换句话说,list()list.call() 的简写,它将调用 list 闭包。

当然,发生了一些非常奇怪的事情,因为异常表明它无法在 MissingMethodException 类上找到 dynamiclist() 方法。

你有一个可重现的例子吗?

Is that the whole of the code? You're likely to run into problems with the call to list() in dynamicList() because it matches the action. In other words, list() is shorthand for list.call(), which will invoke the list closure.

Certainly something very strange is happening because the exception is saying it can't find the dynamiclist() method on the MissingMethodException class.

Do you have a reproducible example?

倾城花音 2024-09-10 10:30:19

我认为控制器是通过 grails 注入框架启动的,因此它可能不是在这里使用奇怪的继承逻辑的最佳方法。

尽管您可以使用组合而不是扩展基类。
有一个功能可以让您轻松地将服务注入控制器,这样您就可以按功能将服务分组。 在此处输入链接说明

这是一个老问题,您可能会找到答案或者技术进步了一些。

自动注入适用于服务和控制器以及标签库。

I think Controllers are initiated via grails injection framework, so it may not be the best way to use strange inheritance logic here.

Although you can use composition instead of extending a base class.
There is a feature which allows you to inject services easily into controllers, so you can group by functionality into Services. enter link description here

This is an old question, you may found the answer or technology evolved a bit.

Automatic injection works for Services and Controllers and tag libraries.

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