grails question (sample 1 of Grails To Action book) Controller 和服务问题

标签 grails service controller dns

我正在为第一章做 Grails To Action 示例。在我开始使用服务之前,一切都很好。当我运行应用程序时,出现以下错误:

groovy.lang.MissingPropertyException: No such property: quoteService for class: qotd.QuoteController

at qotd.QuoteController$_closure3.doCall(QuoteController.groovy:14)

at qotd.QuoteController$_closure3.doCall(QuoteController.groovy)

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

这是我的 groovie QuoteService 类,它在 GetStaticQuote 的定义中有一个错误(错误:Groovy:无法解析类引用)
 package qotd

    class QuoteService {

 boolean transactional = false

 def getRandomQuote() {
  def allQuotes = Quote.list()
  def randomQuote = null
  if (allQuotes.size() > 0) {
   def randomIdx = new Random().nextInt(allQuotes.size())
   randomQuote = allQuotes[randomIdx]
  } else {
   randomQuote = getStaticQuote()
  }
  return randomQuote
 }

 def getStaticQuote() {
  return new Quote(author: "Anonymous",content: "Real Programmers Don't eat quiche")
 }
     }

Eclipse 在 getStaticQuote 的定义上显示错误标志:
ERROR: Groovy:unable to resolve class Quote

任何线索?

Controller groovie 类
package qotd

class QuoteController {

   def index = {
     redirect(action: random)
   }

   def home = {
     render "<h1>Real Programmers do not each quiche!</h1>" 
   }

  def random = {
     def randomQuote = quoteService.getRandomQuote()
     [ quote : randomQuote ]
   }

  def ajaxRandom = {
     def randomQuote = quoteService.getRandomQuote()
     render "<q>${randomQuote.content}</q>" +
     "<p>${randomQuote.author}</p>"
   }
}

报价类:
package qotd

class Quote {
   String content
   String author
   Date created = new Date()

   static constraints = {
     author(blank:false)
     content(maxSize:1000, blank:false) 
    }
}

我正在使用 STS 做样本。有什么建议吗?

问候,

弗朗西斯科

最佳答案


def quoteService
在 Controller 的顶部,它将自动注入(inject) Controller

关于grails question (sample 1 of Grails To Action book) Controller 和服务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3057412/

相关文章:

grails - grails 2.5,如何使用ant删除构建脚本中的文件

java - 在 Android 中静态注册自定义广播接收器的正确方法是什么?

angular - 我应该如何在 Angular 4 的组件的 HTML 模板中使用服务中的 BehaviorSubject 类?

javascript - AngularJS 工厂变量变得未定义

java - spring mvc-在调用 Controller 函数之前调用函数

json - 如何用自己的JSON转换器替换

grails - 如何在 Grails 中对域类中的关系施加约束?

html - 从模板中管理标题导航菜单的最佳方式?

windows - tcp 错误代码 10060 调用 WCF 服务

controller - JavaFX 2.2 -fx :include - how to access parent controller from child controller