2011-06-06 53 views
2

我有一項服務,我有一種方法可以調用,並且如何訪問此服務。 我看到短信插件,並安裝了它,我怎樣才能從我的應用程序不同mobiles.I發送短信遵循Grails的短信插件,但我沒有得到任何結果ecxept ecxeptions如何使用Groovy在Grails中調用服務

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" 

    } 
} 

請解釋我一個例子。 提前感謝。

+0

的dublicate:http://stackoverflow.com/questions/6250914/如何在Grails中調用控制器服務? – elCapitano 2011-06-06 12:24:31

回答

5

如果你想調用從服務方法的插件,你需要做的:

  1. 改變你的服務的名稱(所以它不叫SipgateService
  2. 添加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() 
    ... 
    } 
}