2016-05-11 3 views
0

私はjoda taglibを呼び出すクォーツジョブを持っていますが、他のものでも構いません。grailsはquartzジョブから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) 

しかし、私はgettngています::

私はこのようにそれを行うことは、時間ではなくによってトリガーされるよう石英ジョブがバインド要求ではないので、絶対に理にかなって

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.] 

要求。

私は周りを検索しましたが、解決策の解決策は見つかりませんでした。誰かが私にヒントを与えることができますか?

答えて

0

問題はjoda.format()現在のリクエストを取得しようとしましたが、これはあなたが指摘したように可能ではありません。

もっと基本的には、taglibsはGSPのためのものです。 GSPの外でそれらを呼び出すことは、taglibのユースケースの外にあるため、良い方法ではありません。幸いにも、解決策は単純です:直接ジョダの時刻フォーマッタを使用します。

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を呼び出す方法に対する答えがすべてでそれを行うことはありません。

+0

あなたの答えをありがとう。私はまた、DateTimeFormatterを使って解決策を思いついた。しかし、私はあなたの答えを受け入れることができません(良い)私の問題に解決策を与えたが、質問は私が石英の仕事の中でTagLibを呼び出すことができた方法でした – Bernhard

+0

また、現在の要求を取得しようとしているjoda TagLib私はロケール属性を渡しているので、要求は使用されません)。すべてのTagLibに対する一般的な問題です。彼らはコードブロックに入っていません。もし私がTagLibメソッドの最初のところでlog.debugを実行した場合のイベント – Bernhard

+3

そして私の答えは、あなたが単純にそうしていないということでした。私はそれが可能だと確信していますが、taglibが通常の状況下で動作していると思うようにリクエストを注入する必要があるため、歯を引っ張るようなものです。カスタムタグライブラリの場合、少なくともサービスのような別の場所に共通コードを置き、それをjobとtaglibから呼び出す方がよいでしょう。サードパーティのtaglibの場合、残念ながらコードの重複があります。もう一度、石英の仕事*からtaglibを呼び出す方法は、まったくそれをすることではありません。 –

関連する問題