如何用Groovy调用Grails中的服务

时间:2011-06-06 12:22:59

标签: grails groovy grails-plugin

我有一项服务,我有一个方法可以打电话,我该如何访问这项服务。 我已经看过sms插件并安装了它如何从我的应用程序发送短信到不同的mobiles.I跟着grails sms插件但我没有得到任何结果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"

  }
}

请以一个例子解释我。 非常感谢。

1 个答案:

答案 0 :(得分:5)

如果要从服务方法调用插件,则需要执行以下操作:

  1. 更改服务名称(因此不会将其命名为SipgateService
  2. def sipgateService添加为类定义,而不是方法一
  3. 这有用吗?

    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()
        ...
      }
    }