我正在为第一章做 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/