我试图在模板激活时打印当前日期。我已经读过,我必须将新的Date()Java对象传递给模板,但我不知道如何做或者将它放在代码中。模板中的Freemarker打印日期
在这种情况下,有人知道如何将Java对象传递给模板吗?
谢谢!
我试图在模板激活时打印当前日期。我已经读过,我必须将新的Date()Java对象传递给模板,但我不知道如何做或者将它放在代码中。模板中的Freemarker打印日期
在这种情况下,有人知道如何将Java对象传递给模板吗?
谢谢!
其实你不必通过一个new Date()
到您的模板,因为放置一个时间戳到模板的输出是相当普遍的,因此FreeMarker的提供了一个名为special variable其中.now
返回当前的日期和时间。您可以在模板中使用这样的:
Page generated: ${.now}
(FreeMarker的还包含不同的内置插件格式化日期:http://freemarker.org/docs/ref_builtins_date.html)
更新:只与FreeMarker的,2.3.17最新版本的作品。
使用了Freemarker的ObjectConstructor API来创建日历对象和格式化对象,然后将二者结合起来,以打印日期:
<#-- Create constructor object -->
<#assign objectConstructor = "freemarker.template.utility.ObjectConstructor"?new()>
<#-- Call calendar constructor -->
<#assign clock = objectConstructor("java.util.GregorianCalendar")>
<#-- Call formatter constructor -->
<#assign mmddyy = objectConstructor("java.text.SimpleDateFormat","MM/dd/yyyy")>
<#-- Call getTime method to return the date in milliseconds-->
<#assign date = clock.getTime()>
<#-- Call format method to pretty print the date -->
<#assign now = mmddyy.format(date)>
<#-- Display date -->
${now}
The
?new
built-in, as it was implemented, was a security hole. Now, it only allows you to instantiate a java object that implements thefreemarker.template.TemplateModel
interface. If you want the functionality of the ?new built-in as it existed in prior versions, make available an instance of thefreemarker.template.utility.ObjectConstructor
class to your template. For example:
myDataModel.put("objConstructor", new ObjectConstructor());
and then in the template you can do this:
<#assign aList = objConstructor("java.util.ArrayList", 100)>)
参考
感谢它为我工作 – Mateen 2015-01-08 11:30:23
@mateen没问题。希望它节省了你的时间。 – 2015-01-08 15:36:29
${.now}
是完美的答案。只是想补充一些其他的方法可以从最新获得的直接价值
#-- Predefined format names: -->
${openingTime?string.short}
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}
${openingTime?string.xs} <#-- XSD xs:time -->
${openingTime?string.iso} <#-- ISO 8601 time -->
${.now?string.short}
${.now?string.medium}
${.now?string.long}
${.now?string.full}
${.now?string.xs} <#-- XSD xs:date -->
${.now?string.iso} <#-- ISO 8601 date -->
${.now?string.short}
${.now?string.medium}
${.now?string.long}
${.now?string.full}
${.now?string.medium_short} <#-- medium date, short time -->
${.now?string.xs} <#-- XSD xs:dateTime -->
${.now?string.iso} <#-- ISO 8601 combined date and time -->
<#-- Programmer-defined named format (@ + name): -->
${[email protected]}
<#-- Advanced ISO 8601 and XSD formatting: -->
${.now?string.iso_m_u}
${.now?string.xs_ms_nz}
<#-- SimpleDateFormat patterns: -->
${.now?string["dd.MM.yyyy, HH:mm"]}
${.now?string["EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'"]}
${.now?string["EEE, MMM d, ''yy"]}
${.now?string.yyyy} <#-- Same as ${.now?string["yyyy"]} -->
将输出
01:45 PM
01:45:09 PM
01:45:09 PM PST
01:45:09 PM PST
13:45:09-08:00
13:45:09-08:00
2/20/07
Apr 20, 2007
April 20, 2007
Friday, April 20, 2007
2007-02-20-08:00
2007-02-20
2/20/07 01:45 PM
Feb 20, 2007 01:45:09 PM
February 20, 2007 01:45:09 PM PST
Friday, February 20, 2007 01:45:09 PM PST
Feb 8, 2003 9:24 PM
2007-02-20T13:45:09-08:00
2007-02-20T13:45:09-08:00
Apr/20/2007 13:45
2007-02-20T21:45Z
2007-02-20T13:45:09.000
08.04.2003 21:24
Tuesday, April 08, 2003, 09:24 PM (PDT)
Tue, Apr 8, '03
2003
不错(实际上不是)从官方文档复制粘贴。您至少可以插入[link](http://freemarker.org/docs/ref_builtins_date。html) – CodeMonkey 2017-09-12 15:42:47
我把它从教程,我通常把链接,不知道为什么我错过了这一个 – 2017-09-12 16:52:41
感谢您UR答案,但我收到此错误:ParseException的:未知内置变量:现在 – user763222 2011-05-23 13:55:12
我这样做的方式是$ {content.metaData.modificationDate?string.short}。 content.metaData.modificationDate为我提供了该模板的最后修改,这几乎是我所需要的。 – user763222 2011-05-23 15:32:54
2.3.17版本中引入了特殊变量'.now'(请参阅http://freemarker.org/docs/versions_2_3_17.html),该文件于一周前发布。如果无法更新,则必须将当前日期传递到数据模型中,例如作为根哈希映射的一部分(详情请参阅http://freemarker.org/docs/pgui_quickstart_createdatamodel.html和http://freemarker.org/docs/pgui_quickstart_merge.html),如果您还不知道这些内容)。 – Chaquotay 2011-05-23 19:20:21