无法从模型模板呈现日期 [英] Unable to render date from model template

查看:1426
本文介绍了无法从模型模板呈现日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Freemarker一起开发Grails项目,并且无法从数据模型中呈现日期。我开始把一个日期放入模型中。

  def dataModel = [:] 
def dataDate = new Date() .parse(yyyy-MM-dd,2015-08-20)
dataModel.put(someDate,dataDate)

然后我通过dataModel循环来验证数据类型

  dataModel.each {name,value  - > 
println$ {name}:$ {value}(Value is type:$ {value.getClass()})
}

为此,我的输出是: someDate:Thu Aug 20 00:00:00 CDT 2015(值为类型:类java.util.Date)



接下来,我设置配置文件并尝试处理模板



<$ p $配置cfg =新配置(Configuration.VERSION_2_3_22)
cfg.setDefaultEncoding(UTF-8)
def ftlTemplate = new模板(name,新的StringReader( $ {someDate}),cfg)
Writer out = new StringWriter()
ftlTemplate.process(dataModel,out)
output = out.toString()

此时我收到以下错误:

  ERROR freemarker.runtime  - 执行FreeMarker模板时出错
消息:无法将类似日期的值转换为字符串,因为如果它是日期(无时间部分),时间或日期,时间价值。
指责表达:
==> someDate [在第1行第3列的模板名称中]

我找到了一个Freemarker引擎这里在线: http://freemarker-online.kenshoo.com/ ,如果我运行相同的数据模型通过我收到的输出是模板: 2015年8月20日



任何人都可以指出我要去哪里错误?我想呈现像在线引擎那样的输出。



谢谢

解决方案

问题在于(正如错误消息所述),从技术角度来说,如果一个 java.util.Date 表示仅限日期,日期时间或仅时间值。这是FreeMarker之外的技术事实。 FreeMarker提供了这些方法来处理这个问题:


  • 使用?date ?time ?datetime 运算符为模板中的FreeMarker提供一个提示。请参阅: http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_date_datetype < 使用 java.sql.Date java.sql.Timestamp c $ c>和 java.sql.Timestamp ,其中没有这样的歧义。 (注意 Timestamp 的有趣的小数部分,然后处理。)

  • code> TemplateDateModel ,您可以指定它是哪种类型的值。然后将 TemplateDateModel 放入数据模型中。

I'm working on a Grails project with Freemarker and am having trouble rendering a date from the data model. I start put placing a date into the model

def dataModel = [:]
def dataDate = new Date().parse("yyyy-MM-dd","2015-08-20")
dataModel.put("someDate",dataDate)

I then loop through the dataModel to verify the data types

dataModel.each { name, value ->
    println "${name} : ${value} (Value is type: ${value.getClass()})"
}

For this my output is: someDate : Thu Aug 20 00:00:00 CDT 2015 (Value is type: class java.util.Date)

Next I set up my confiuration and try to process the template

Configuration cfg = new Configuration(Configuration.VERSION_2_3_22)
cfg.setDefaultEncoding("UTF-8")
def ftlTemplate = new Template("name", new StringReader("${someDate}"), cfg)
Writer out = new StringWriter()
ftlTemplate.process(dataModel, out)
output = out.toString()

At this point I receive the following error

ERROR freemarker.runtime  - Error executing FreeMarker template
Message: Can't convert the date-like value to string because it isn't known if it's a date (no time part), time or date-time value.
The blamed expression:
==> someDate  [in template "name" at line 1, column 3]

I've found a Freemarker engine online here: http://freemarker-online.kenshoo.com/ and if I run the same datamodel and template through that the ouput I receive is: Aug 20, 2015

Can anyone point out where I am going wrong? I would like to render the output like that online engine does.

Thanks

解决方案

The problem is (as the error message states) is that it's technically impossible to decide in general, if a java.util.Date stands for a date-only, date-time or time-only value. This is a technical fact that's outside FreeMarker. FreeMarker offers these ways to handle this problem:

  • Use ?date, ?time and ?datetime operators to give FreeMarker a hint in the template. See: http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_date_datetype

  • Use java.sql.Date and java.sql.Timestamp and java.sql.Timestamp, where there's no such ambiguity. (Beware with Timestamp's funny fraction second handling though.)

  • Wrap the values manually into TemplateDateModel, where you can specify which kind of value it is. Then drop the TemplateDateModel into the data-model.

这篇关于无法从模型模板呈现日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆