2011-12-28 72 views
0

我想這個工作對讀消息包(Grails的2.0,石英0.4.2):使用應用程序上下文

class Job1Job { 
def timeout = 5000l // execute job once in 5 seconds 
def grailsApplication 

def execute() { 
    def ctx = grailsApplication.mainContext 
    def bean = ctx.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib') 
    println bean.message(code:'default.home.label') 
} 
} 

而得到錯誤:

Error 2011-12-28 14:30:14,021 [quartzScheduler_Worker-5] ERROR listeners.ExceptionPrinterJobListener - Exception occured in job: GRAILS_JOBS.testappcontext.Job1Job 
Message: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 

代碼在控制器和服務中完美工作。我做錯了什麼? 我懷疑這個代碼的Grails 1.3.7

回答

8

你周圍發生的很長的路要走完美運行 - 只需使用什麼標籤庫使用的messageSource豆:

class Job1Job { 
    long timeout = 5000 // execute job once in 5 seconds 
    def messageSource 

    void execute() { 
     println messageSource.getMessage('default.home.label', null, Locale.default) 
    } 
} 
+0

謝謝。是否可以傳遞消息參數?我的代碼不起作用:messageSource.getMessage('default.list.label',[「!」],Locale.default) – 2011-12-28 15:19:29

+1

第二個arg是一個Object數組,因此應該是'messageSource.getMessage('default。 list.label',['!']爲Object [],Locale.default)' – 2011-12-28 15:32:19

相關問題