grails从石英工作中调用taglib

时间:2016-05-11 08:04:14

标签: grails quartz-scheduler

我有一个调用joda taglib的石英作业(但可能是其他任何一个)。

我这样做:

def joda = grailsApplication.mainContext.getBean('grails.plugin.jodatime.taglib.FormattingTagLib')

def formatDate = joda.format(value:event.startDate, style:"SS", locale:user.locale, zone:user.timeZone)

但我很高兴:

org.quartz.JobExecutionException: java.lang.IllegalStateException: 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. [See nested exception: java.lang.IllegalStateException: 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.]

这绝对有意义,因为石英作业不受请求约束,因为它是由时间而非请求触发的。

我四处寻找,却找不到适当的溶剂。 somebdy可以给我一个提示吗?

1 个答案:

答案 0 :(得分:0)

问题是joda.format()尝试获取当前请求,如您所述,这是不可能的。

但更重要的是,taglibs适用于GSP。在GSP之外调用它们并不是一个好习惯,因为它超出了taglib的用例。幸运的是,解决方案很简单:直接使用joda time formatter:

import org.joda.time.format.DateTimeFormat

def formatter = DateTimeFormat.forStyle('SS').withLocale(user.locale).withZone(user.timeZone)
def formatDate = formatter.print(event.startDate)

https://github.com/gpc/joda-time/blob/grails2/grails-app/taglib/grails/plugin/jodatime/taglib/FormattingTagLib.groovy

如何从石英作业调用taglib的答案根本不是这样做的。