【问题标题】:View grails domain class property in another domain class在另一个域类中查看 grails 域类属性
【发布时间】:2014-01-26 17:42:05
【问题描述】:

您好,我只是在尝试 grails 并尝试了解有关域类的更多信息。我有两个简单的域类:

域类 1

package grailtest

class Person {

    String  firstName
    String  lastName
    int age
    String  email

    static constraints = {
    }

}

域类 2

package grailtest

class Customer {

    String  customerId
    Person  personInCharge

    static constraints = {
    }

}

当我做一个run-app时,我只能看到

grailtest.Person : 1

作为人。如何将其默认为特定值,例如 firstName + lastName,以使应用程序更加用户友好?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    在域中将 toString 方法覆盖为您想要显示的内容。重启应用

    【讨论】:

      【解决方案2】:

      您可以使用@ToString,以防您想要一种详细的方式来记录或打印标准输出。

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

      例如,

      def person = new Person(firstName: 'Test', lastName: 'hola', 
                                age: 10, email: 'abc@xyz.com')
      

      会给

      Person(firstName:Test, lastName:hola, age:10, email:abc@xyz.com)
      

      【讨论】:

        【解决方案3】:

        找到显示 grailstest.Person: 1 的视图并将其更新为:

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

        默认情况下这个视图应该在"views/person"

        【讨论】:

        • 谢谢!也会试试的!干杯!
        【解决方案4】:

        你把这段代码放在视图.gsp中

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-01
          • 1970-01-01
          • 2012-07-06
          相关资源
          最近更新 更多