简体   繁体   中英

Grails invokeMethod doesn't work

I have 2 method which get the same params:

 void removeJob(def user,def company,String job){}
 void addJob(def user,def company,String job){}

I want to use the invokeMethod() but I'm not sure how. I try:

def methodName = selectedChange[2]=='true'?'add':'remove'
roleService.invokeMethod(methodName+'Job',[userSelected,      selectedCompany,selectedChange[1]])

But I got errors

You can call the method dynamically using a GString for the method name:

String methodName = selectedChange[2] == 'true' ? 'add' : 'remove'
roleService."${methodName + 'Job'}"(userSelected, selectedCompany, selectedChange[1])

This also works with properties, eg

String propertyName = 'fooCount'
int count = person."$propertyName"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM