(1)解決輸出中文亂碼問題:
freemarker亂碼的原因:
- 沒有使用正確的編碼格式讀取模版文件,表現為模版中的中文為亂碼
解決方法:在classpath上放置一個文件freemarker.properties,在里面寫上模版文件的編碼方式,比如
default_encoding=UTF-8
locale=zh_CN
注意:eclipse中除了xml文件、java文件外,默認的文件格式iso8859-1
- 數據插入模版時,沒有使用正確的編碼,表現出模版中的新插入數據為亂碼
解決方法:在result的配置中,指定charset,s2的FreemarkerResult.java會將charset傳遞freemarker
<action name="ListPersons" class="ListPersons">
<result type="freemarker">
<param name="location">/pages/Person/view.ftl</param>
<param name="contentType"> text/html;charset=UTF-8
</param>
</result>
</action>
(2)提高freemarker的性能
在freemarker.properties中設置:
template_update_delay=60000
避免每次請求都重新載入模版,即充分利用cached的模版
(3)盡量使用freemarker本身的提供的tag,使用S2 tags 的標簽會在性能上有所損失
Freemarker has support for iterating lists, displaying properties, including other templates, macro's, and so on. There is a small performance cost when using the S2 tags instead of the Freemarker equivalent (eg. <s:property value="foo"/> should be replaced by ${foo}).
(4)freemarker的標簽種類:
- ${..}:FreeMarker will replace it in the output with the actual value of the thing in the curly brackets. They are called interpolations.
- # ,代表是FTL tags(FreeMarker Template Language tags),hey are instructions to FreeMarker and will not be printed to the output
- <#if ...></#if>
- <#list totalList as elementObject>...</#list>
- @ ,代表用戶自定義的標簽
- <#-- --> 注釋標簽,注意不是<!-- -->
(5)一些特殊的指令:
- r代表原樣輸出:${r"C:\foo\bar"}
- <#list ["winter", "spring", "summer", "autumn"] as x>${x}</#list>
- ?引出內置指令
- String處理指令:
- html:特殊的html字符將會被轉義,比如"<",處理后的結果是<
- cap_first、lower_case、upper_case
- trim:除去字符串前后的空格
- sequences處理指令
- numbers處理指令
- int:number的整數部分,(e.g. -1.9?int is -1)
(6)對于null,或者miss value,freemarker會報錯
- !:default value operator,語法結構為:unsafe_expr!default_expr,比如 ${mouse!"No mouse."} 當mouse不存在時,返回default value;
- (product.color)!"red" 這種方式,能夠處理product或者color為miss value的情況;
- 而product.color!"red"將只處理color為miss value的情況
- ??: Missing value test operator ,測試是否為missing value
- unsafe_expr?? :product.color??將只測試color是否為null
- (unsafe_expr)??:(product.color)??將測試product和color是否存在null
- ?exists:舊版本的用法
比如:
<#if mouse??>
Mouse found
<#else>
No mouse found
</#if>
Creating mouse...
<#assign mouse = "Jerry">
<#if mouse??>
Mouse found
<#else>
No mouse found
</#if>
(7)模版值插入方式(interpolation)
- 通用方式(Universal interpolations):${expression}
- 對于字符串:只是簡單輸出
- 對于數值,會自動根據local確定格式,稱為human audience,否則稱為computer audience,可以"?c",比如,<a href="/shop/details?id=${product.id?c}">Details...</a>,因此這里的id是給瀏覽器使用的,不需要進行格式化,注意?c只對數值有效
- 對于日期,會使用默認的日期格式轉換,因此需要事先設置好默認的轉換格式,包括date_format, time_format,atetime_format
- 對于布爾值,不能輸出,會報錯并停止模版的執行,比如${a = 2}會出錯,但是可以string built-in來進行轉換
數值處理,具體參考:Built-ins for numbers
http://freemarker.org/docs/ref_builtins_number.html#ref_builtin_string_for_number
數值處理的例子:
<#setting number_format="currency"/>
<#assign answer=42/>
${answer}
${answer?string} <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}
除了使用內置的formate,可以使用任何用Java decimal number format syntax書寫的formate,比如
<#setting number_format="0.###E0"/>
${1234}
${12345?string("0.####E0")}
更加方便的格式:
<#setting locale="en_US">
US people writes: ${12345678?string(",##0.00")}
<#setting locale="hu">
Hungarian people writes: ${12345678?string(",##0.00")}
日期處理,參考Built-ins for dates
http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_string_for_date
日期處理的例子:
${openingTime?string.short}
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}
${nextDiscountDay?string.short}
${nextDiscountDay?string.medium}
${nextDiscountDay?string.long}
${nextDiscountDay?string.full}
${lastUpdated?string.short}
${lastUpdated?string.medium}
${lastUpdated?string.long}
${lastUpdated?string.full}
注意:
由于java語言中的Date類型的不足,freemarker不能根據Date變量判斷出變量包含的部分(日期、時間還是全部),在這種情況下,freemarker
不能正確顯示出${lastUpdated?string.short} 或者 simply ${lastUpdated},因此,可以通過?date, ?time and ?datetime built-ins
來幫助freemarker來進行判斷,比如${lastUpdated?datetime?string.short}
除了使用內置的日期轉換格式外,可以自己指定日期的格式,使用的是Java date format syntax,比如:
${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}
- 數值專用方式(Numerical interpolations):#{expression} or #{expression; format},這是數值專用的輸出方式,但是最好使用通用方式的string built-in或者number_format來完成轉換,Numerical interpolations方式將會被停用
(8)創建自定義模版
創建:
<#macro greet>
<font size="+2">Hello Joe!</font>
</#macro>
使用:<@greet></@greet> 或者<@greet/>