问题
I have one service in that i have a method to call and how can i acces this service. I have seen sms plugin and installed it and How can i send sms from my application to the different mobiles.I followed the grails sms plugin but i didn't get any results ecxept ecxeptions
class SipgateService {
static transactional = true
def serviceMethod() {
def sipgateService
//def phoneNumber = 'XXXXXXXXXX' //phoneNumber according to E.164 specification
//working alternative:
println "service"
def phoneNumber = 'XXXXXXXXXX'
def result = sipgateService.sendSMS(phoneNumber, 'This is my Text to send!')
result ? 'Sending Successful':'Sending failed'
println "after service"
}
}
Please explain me with an example. thanks alot in advance.
回答1:
If you want to call the plugin from a service method, you would need to do:
- change the name of your service (so it isn't called
SipgateService
) - Add the
def sipgateService
as a class definition, not a method one
Does this work?
class MySMSService {
static transactional = true
def sipgateService // This will be injected from the SMS plugin
def serviceMethod() {
println "service"
def phoneNumber = 'XXXXXXXXXX'
def result = sipgateService.sendSMS(phoneNumber, 'This is my Text to send!')
result ? 'Sending Successful':'Sending failed'
println "after service"
}
}
Then, from a controller, define the link to MySMSService
at class level, and call your serviceMethod
method ie:
class MyController {
def mySMSService // this will be injected from your service
// then, when you want to use it (from an action)
def someAction = {
...
mySMSService.serviceMethod()
...
}
}
来源:https://stackoverflow.com/questions/6251912/how-to-call-service-in-grails-with-groovy