Grails 文档定义了一个“count”静态方法,在文档中定义如下:
Description Counts the number of instances in the database and returns the result Parameters None Example def noOfBooks = Book.count()
However, whenever I call it, I get this error! I simple added a call to the name of my Domain Class (Company) like this to a working Controller
def companies = Company.count()
当它执行该行时,它会因以下错误而崩溃
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass
Controller 中的其他工作代码(使用静态脚手架命令预先生成)访问 Company.get(...) 等,没有错误
我做错了什么?
最佳答案
这个
class HomeController {
def companies = Company.count()
def index = {
render(view:"index")
}
}
失败是因为正如 lael 指出的那样,应用程序启动时 count() 方法不可用。 count() 方法是 GORM 添加到域类中的动态方法。 Spring(底层框架)在启动时为每个工件( Controller /服务/域类等)创建一个对象。 GORM 会在这之后。
Spring正在尝试创建一个HomeController类,HomeController的构建会在启动时将company.count的值赋给Company,但是GORM还没有启动,所以还没有添加动态方法。
无论如何,这段代码都是不正确的,如果它确实有效,那么公司变量将只有应用程序启动时公司数量的值。一旦您“习惯”了 groovy 和 grails,我想您将会欣赏到开发的速度。
希望这有帮助。
关于grails - 我无法让 Domain.count() 静态方法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1228673/