Saturday, February 12, 2011

Adding a method to a domain class

I have a domain class containing a couple of fields. I can access them from my .gsps. I want to add a method to the domain class, which I can call from the .gsps (this method is a kind of virtual field; it's data is not coming directly from the database).

How do I add the method and how can I then call it from the .gsps?

  • To add a method, just write it out like you would any other regular method. It will be available on the object when you display it in your GSP.

    def someMethod() {
    return "Hello."
    }
    

    Then in your GSP.

    ${myObject.someMethod()}
    
    From Hates_
  • If you want your method to appear to be more like a property, then make your method a getter method. A method called getFullName(), can be accessed like a property as ${person.fullName}. Note the lack of parentheses.

0 comments:

Post a Comment