这是我的grails域对象的精简版本:
//this is a lookup table
class PetType {
String description
}
class Family {
static hasMany = [
petTypePreferred:PetType
]
}
在我的 Controller 中,我从gsp返回了一个参数字符串值'petTypePreferred'。了解该字符串值并了解Domain类Family,如何从“petTypePreferred”字符串值确定Domain类?在grails 2.2.4中,即使getPropertyByName(String value)方法在javadoc中,也对域对象无效。
所以我有String petTypePreferred和Class Family,但是我需要根据这两条信息找到Class PetType。
最佳答案
我不确定您到底想要什么:
获取域名类:hasMany
是Map
中的简单静态Family
。 Map
将字段名称存储为键,将目标类型存储为值。因此,您可以使用以下命令从字符串PetType
获取域类petTypePreferred
:
Familiy.hasMany['petTypePreferred']
获取给定
description
实例的PetType
内Family
的值:一个家庭可以具有多种宠物类型(
hasMany
),因此结果必须是一个集合:Family family = ...
List descriptions = family['petTypePreferred']*.description
这为您提供了名为
PetType
的Family
实例的family
描述列表。
关于grails - 如何从字符串获取grails类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18924289/