grails 问题(Grails To Action 书的示例 1)控制器和服务的问题

发布于 2024-09-06 02:56:51 字数 1947 浏览 2 评论 0原文

我正在为第一章做 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:无法解析类 Quote)

 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

有任何线索吗?

控制器 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>"
   }
}

Quote 类:

package qotd

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

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

我正在使用 STS 进行示例。有什么建议吗?

问候,

弗朗西斯科

I'm doing Grails To Action sample for chapter one. Every was just fine until I started to work with Services. When I run the app I have the following error:

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)

Here is my groovie QuoteService class, which has an error within the definition of GetStaticQuote (ERROR: Groovy:unable to resolve class Quote)

 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 show me an error flag on the definition of getStaticQuote:

ERROR: Groovy:unable to resolve class Quote

Any Clues?

Controller groovie class

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>"
   }
}

Quote Class:

package qotd

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

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

I'm doing the samples using STS. Any advice?

Regards,

Francisco

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

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

发布评论

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

评论(3

抱猫软卧 2024-09-13 02:56:51

执行

def quoteService ,它将自动注入到控制器中

在控制器顶部

do

def quoteService

at the top of your controller and it will be injected into the controller automatically

铁憨憨 2024-09-13 02:56:51

groovy.lang.MissingPropertyException:没有这样的属性:类的 quoteService:qotd.QuoteController

我不在 grails 中编码,但似乎您需要在控制器中的某个位置声明 quoteService。

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

I dont code in grails but it appears as though you need to declare quoteService somewhere in the controller.

深居我梦 2024-09-13 02:56:51

我做到了

def quoteService = new QuoteService()

它解决了我的问题

I did

def quoteService = new QuoteService()

and it solved my problem

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