Question

Hi there i am still very new to grails and I have not been able to figure out why this is happening.

I have a domain class:

package scheduler

class Client {
  String name

  static constraints = {}
}

And a controller:

package scheduler
class AdminController {
  def create() {
    def client = new Client(name:"John")
    println client
  }
}

Currently I am always getting null for client. Originally the above was a little more complex on the domain class side but I systematically dumbed it down to see if it was a problem there. I still can not get the above working.

The output is always scheduler.Client : null

Please let me know if I need to provide anymore information.

Était-ce utile?

La solution

It's not null, that's just the default output of the toString method that Grails adds. It prints the class name and the id. Since you haven't saved the instance, the id is null. If the instance was null the output would have been null, not scheduler.Client : null

If you want to see the data in the instance, use the Groovy dump() method, e.g.

def client = new Client(name:"John")
println client.dump()

You could also add a toString method that includes the name attribute, e.g.

package scheduler

class Client {
   String name

   String toString() { name }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top