2017-06-02 23 views
1

我是Grails的新手,一直在研究如何對服務調用的域屬性進行變量插值。如何對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: monthlyResult $ {MONTHNAME} 其中MONTHNAME是該月的名稱的字符串,我想。

回答

1

就這麼簡單

String monthName = 'january' 
assert 42 == new MonthlyResult(january:42)[ monthName ] 

而且,我寧願在這樣的域中使用map類,鍵入一些枚舉值:

class MonthlyResult { 

    Enum Month { january, february, .... } 

    Map<Month,Integer> months 
} 
+0

噢,好的,謝謝你的提示! – rxa

1

你只需要添加雙引號:

def monthName = 'january' 
def monthlyResult = new MonthlyResult​() 
println monthlyResult."​${monthName}​​"​ 

輸出:

0 
+0

啊是的,謝謝你! – rxa

相關問題