2011-05-20 79 views
9

我試圖在模板激活時打印當前日期。我已經讀過,我必須將新的Date()Java對象傳遞給模板,但我不知道如何做或者將它放在代碼中。模板中的Freemarker打印日期

在這種情況下,有人知道如何將Java對象傳遞給模板嗎?

謝謝!

回答

10

其實你不必通過一個new Date()到您的模板,因爲放置一個時間戳到模板的輸出是相當普遍的,因此FreeMarker的提供了一個名爲special variable其中.now返回當前的日期和時間。您可以在模板中使用這樣的:

Page generated: ${.now} 

(FreeMarker的還包含不同的內置插件格式化日期:http://freemarker.org/docs/ref_builtins_date.html

更新:只與FreeMarker的,2.3.17最新版本的作品。

+0

感謝您UR答案,但我收到此錯誤:ParseException的:未知內置變量:現在 – user763222 2011-05-23 13:55:12

+0

我這樣做的方式是$ {content.metaData.modificationDate?string.short}。 content.metaData.modificationDate爲我提供了該模板的最後修改,這幾乎是我所需要的。 – user763222 2011-05-23 15:32:54

+3

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

3

使用了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 the freemarker.template.TemplateModel interface. If you want the functionality of the ?new built-in as it existed in prior versions, make available an instance of the freemarker.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)>) 

參考

+0

感謝它爲我工作 – Mateen 2015-01-08 11:30:23

+0

@mateen沒問題。希望它節省了你的時間。 – 2015-01-08 15:36:29

0

${.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 
+0

不錯(實際上不是)從官方文檔複製粘貼。您至少可以插入[link](http://freemarker.org/docs/ref_builtins_date。html) – CodeMonkey 2017-09-12 15:42:47

+0

我把它從教程,我通常把鏈接,不知道爲什麼我錯過了這一個 – 2017-09-12 16:52:41