我是Grails的新手,一直在研究从服务调用的域属性上的变量插值。
我的域如下所示:
class MonthlyResult {
FilterType type
String typeId
Integer year
Integer january = 0
Integer february = 0
Integer march = 0
Integer april = 0
Integer may = 0
Integer june = 0
Integer july = 0
Integer august = 0
Integer september = 0
Integer october = 0
Integer november = 0
Integer december = 0
我正在尝试在我的MonthlyResult MonthlyResult中执行此操作:
monthResult。$ {monthName}
其中monthName是一个字符串,其中包含我想要的月份的名称。
最佳答案
就像
String monthName = 'january'
assert 42 == new MonthlyResult( january:42 )[ monthName ]
另外,我宁愿在此类域类中使用
map
,并以一些枚举值作为键:class MonthlyResult {
Enum Month { january, february, .... }
Map<Month,Integer> months
}
关于variables - 如何在Grails中对域属性进行变量插值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44337026/