views:

74

answers:

2

Hi,

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

A: 

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.

Woot4Moo
It seems that in Grails you don't have to.
fegloff
thats quite the unique feature.
Woot4Moo
In Grails, you don't have to *instantiate* the service class, but you do have to *declare* it (as hvgotcodes has answered). Grails (or Spring more specifically) then auto-wires the service into the controller (or whereever you need it).
Daniel Rinser
Woot4Moo where right, I should declare quoteService at the top of the Controller class:def quoteService
fegloff
+2  A: 

do

def quoteService

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

hvgotcodes
Thanks, you're right!But Why I have the following error, on the definition of getStaticQuote method on QuoteService Class?: ERROR: Groovy:unable to resolve class Quote on my QuoteService class?
fegloff
Perhaps your grails-app/domain directory is not defined as a source folder. I definitely recommend using SpringSource Tool Suite with the Grails support rather than pure Eclipse. It's free and the Grails support is pretty solid and getting better all the time.
Peter Ledbrook
Peter's advice seems good. All your stuff is in the same package, so Quote should be visible in the service. Make sure your directory structure conforms to the grails convention -- remember, Grails is 'convention over configuration', and as Peter suggested, make sure in Eclipse that the folder that contains all of your controllers/services/domain classes are source folders.
hvgotcodes