2
votes

Hi I am just trying out grails and trying to learn more on domain class. I have two simple domain classes:

Domain Class 1

package grailtest

class Person {

    String  firstName
    String  lastName
    int age
    String  email

    static constraints = {
    }

}

Domain Class 2

package grailtest

class Customer {

    String  customerId
    Person  personInCharge

    static constraints = {
    }

}

When I do a run-app, I can only see

grailtest.Person : 1

as the Person. How can I default it to a particular value, for instance firstName + lastName, to make the application more user friendly?

4

4 Answers

2
votes

in the domain override toString method to what you wanted to be display. restart the app

2
votes

You can use @ToString in case you want an elaborative way of logging or printing in standard out.

import groovy.transform.ToString

@ToString(includeNames=true, includeFields=true)
class Person {
    String  firstName
    String  lastName
    int age
    String  email
}

For example,

def person = new Person(firstName: 'Test', lastName: 'hola', 
                          age: 10, email: '[email protected]')

would give

Person(firstName:Test, lastName:hola, age:10, email:[email protected])
1
votes

Find the view where it displays grailstest.Person: 1 and update it to:

${personInstance.firstName} ${personInstance.lastName}

By default this view should be in "views/person"

1
votes

You put this code in the view .gsp

${personInstance?.firstname} ${personInstance?.lastname}