【问题标题】:How to call Service in Grails with groovy如何使用 groovy 在 Grails 中调用服务
【发布时间】:2011-06-06 12:22:58
【问题描述】:
我有一个服务,因为我有一个方法可以调用,我如何才能访问这个服务。
我已经看到了短信插件并安装了它,我如何将短信从我的应用程序发送到不同的手机。我遵循了 grails 短信插件,但我没有得到任何结果 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"
}
}
请举例说明。
非常感谢。
【问题讨论】:
标签:
grails
groovy
grails-plugin
【解决方案1】:
如果你想从服务方法调用插件,你需要这样做:
- 更改您的服务名称(因此它不称为
SipgateService)
- 将
def sipgateService 添加为类定义,而不是方法之一
这行得通吗?
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"
}
}
然后,从控制器中,在类级别定义到 MySMSService 的链接,并调用您的 serviceMethod 方法,即:
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()
...
}
}