Grails g、 formatDate从作业执行时引发错误?

Grails g、 formatDate从作业执行时引发错误?,grails,groovy,Grails,Groovy,我使用Grails作业启动一个使用g.formatDate的服务。当直接从控制器使用服务时,我没有问题。当我使用作业中的服务时,会出现以下错误: ERROR listeners.ExceptionPrinterJobListener - Exception occurred in job: Grails Job Message: java.lang.NullPointerException Line | Method ->> 111 | execute in grails.p

我使用Grails作业启动一个使用
g.formatDate
的服务。当直接从控制器使用服务时,我没有问题。当我使用作业中的服务时,会出现以下错误:

ERROR listeners.ExceptionPrinterJobListener  - Exception occurred in job: Grails Job
Message: java.lang.NullPointerException
   Line | Method
->> 111 | execute in grails.plugins.quartz.GrailsJobFactory$GrailsJob
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   202 | run     in org.quartz.core.JobRunShell
^   573 | run . . in org.quartz.simpl.SimpleThreadPool$WorkerThread
Caused by NullPointerException: null
->> 245 | $tt__sendReportCompanyWeekly in Test.SendMailService$$EPPc274K
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    47 | doCall  in Test.ReportCompanyWeeklyJob$_execute_closure1
|    31 | execute in Test.ReportCompanyWeeklyJob$$EPPc2HTU
|   104 | execute in grails.plugins.quartz.GrailsJobFactory$GrailsJob
|   202 | run . . in org.quartz.core.JobRunShell
^   573 | run     in org.quartz.simpl.SimpleThreadPool$WorkerThread
这是我的工作:

class ReportCompanyWeeklyJob {  

  SendMailService sendMailService


  static triggers = {
    cron name: 'scheduledReportCompanyWeeklyJob', cronExpression: "0 15 22 ? * *" 
  }

  def execute() {
    sendMailService.sendReportCompanyWeekly(company.id, user.id)
  }

}
这是我的服务:

@Transactional
class SendMailService {

    def gspTagLibraryLookup  // being automatically injected by spring
    def g

  def sendReportCompanyWeekly(String companyId, String userId) {    
    g = gspTagLibraryLookup.lookupNamespaceDispatcher("g")

    Date today = new Date()
    Locale locale = // some locale

    // Line 245
    def test = g.formatDate(date: today, formatName: 'date.format.long.no.year', locale: locale)  

 }

}

编辑:我使用
groovyPageRenderer.render(模板:viewPathHTML,模型:myModel)
在服务中呈现GSP


从作业运行时,如何使
g.formatDate
工作?

grails.gsp.PageRenderer
在渲染内容上受到限制,因为它与从控制器渲染gsp页面的上下文不同

使用formatName时,
g.formatDate()
尝试从
GrailsWebRequest
获取区域设置。区域设置用于从中检索“date.format.long.no.year”。问题是
grails.gsp.PageRenderer
没有
GrailsWebRequest
。您可以看到
g.formatDate()
源代码

变通 您可以通过格式化服务中的日期并通过模型将其传递给GSP来解决此问题。大概是这样的:

import java.text.SimpleDateFormat

@Transactional
class SendMailService {

    ...
    def messageSource // Injected to look up 'date.format.long.no.year'

    def sendReportCompanyWeekly(String companyId, String userId) {    
        Date today = new Date()
        Locale locale = // some locale

        def formattedDate = new SimpleDateFormat(messageSource.getMessage('date.format.long.no.year', null, locale), locale)
            .format(today)

        /* 
          1. Add the formattedDate to the PageRenderer model, 
          2. Update the GSP code to use the value from the model instead of g.formatDate()
        */
    }

}
更好的主意-创建formatDate闭包
你为什么要从服务中调用gsp标签?因为我必须为电子邮件提供gsp模板?没有完全错误的答案。我使用
groovyPageRenderer.render(模板:viewPathHTML,模型:myModel)
在服务中呈现GSP。你还有其他想法吗?我怎么称呼结束?为它创建一个标记库不是更好吗?下面是一个示例
${myFormatDate(somedate)}
如果您创建一个标记库,您必须传入足够的参数(例如区域设置),以便它可以使用MessageSource。可能,但更复杂。你也有标记库的例子吗?
import java.text.SimpleDateFormat

@Transactional
class SendMailService {

    ...
    def messageSource // Injected to look up 'date.format.long.no.year'

    def sendReportCompanyWeekly(String companyId, String userId) {    
        Date today = new Date()
        Locale locale = // some locale

        def formatter= new SimpleDateFormat(messageSource.getMessage('date.format.long.no.year', null, locale), locale)

        def formatDate = { Date date -> formatter.format(date) }

        /* 
          1. Add the formatDate Closure to the PageRenderer model, 
          2. Update the GSP code to use the Closure from the model instead of g.formatDate()
        */
    }

}