(1)解決輸出中文亂碼問(wèn)題:

     freemarker亂碼的原因:

  • 沒(méi)有使用正確的編碼格式讀取模版文件,表現(xiàn)為模版中的中文為亂碼

解決方法:在classpath上放置一個(gè)文件freemarker.properties,在里面寫上模版文件的編碼方式,比如

default_encoding=UTF-8
locale=zh_CN

注意:eclipse中除了xml文件、java文件外,默認(rèn)的文件格式iso8859-1

  • 數(shù)據(jù)插入模版時(shí),沒(méi)有使用正確的編碼,表現(xiàn)出模版中的新插入數(shù)據(jù)為亂碼

解決方法:在result的配置中,指定charset,s2的FreemarkerResult.java會(huì)將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中設(shè)置:

template_update_delay=60000

避免每次請(qǐng)求都重新載入模版,即充分利用cached的模版

(3)盡量使用freemarker本身的提供的tag,使用S2 tags 的標(biāo)簽會(huì)在性能上有所損失

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的標(biāo)簽種類:

    • ${..}: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>
    • @ ,代表用戶自定義的標(biāo)簽
    • <#-- --> 注釋標(biāo)簽,注意不是<!-- -->

(5)一些特殊的指令:

    • r代表原樣輸出:${r"C:\foo\bar"}
    • <#list ["winter", "spring", "summer", "autumn"] as x>${x}</#list>
    • ?引出內(nèi)置指令
      • String處理指令:
        • html:特殊的html字符將會(huì)被轉(zhuǎn)義,比如"<",處理后的結(jié)果是&lt;
        • cap_firstlower_case、upper_case
        • trim:除去字符串前后的空格
      • sequences處理指令
        • size:返回sequences的大小
      • numbers處理指令
        • int:number的整數(shù)部分,(e.g. -1.9?int is -1)

(6)對(duì)于null,或者miss value,freemarker會(huì)報(bào)錯(cuò)

    • !:default value operator,語(yǔ)法結(jié)構(gòu)為:unsafe_expr!default_expr,比如 ${mouse!"No mouse."} 當(dāng)mouse不存在時(shí),返回default value;
      • (product.color)!"red"  這種方式,能夠處理product或者color為miss value的情況;
      • 而product.color!"red"將只處理color為miss value的情況
    • ??: Missing value test operator ,測(cè)試是否為missing value
      • unsafe_expr?? :product.color??將只測(cè)試color是否為null
      • (unsafe_expr)??:(product.color)??將測(cè)試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}
      • 對(duì)于字符串:只是簡(jiǎn)單輸出
      • 對(duì)于數(shù)值,會(huì)自動(dòng)根據(jù)local確定格式,稱為human audience,否則稱為computer audience,可以"?c",比如,<a href="/shop/details?id=${product.id?c}">Details...</a>,因此這里的id是給瀏覽器使用的,不需要進(jìn)行格式化,注意?c只對(duì)數(shù)值有效
      • 對(duì)于日期,會(huì)使用默認(rèn)的日期格式轉(zhuǎn)換,因此需要事先設(shè)置好默認(rèn)的轉(zhuǎn)換格式,包括date_format, time_format,atetime_format
      • 對(duì)于布爾值,不能輸出,會(huì)報(bào)錯(cuò)并停止模版的執(zhí)行,比如${a = 2}會(huì)出錯(cuò),但是可以string built-in來(lái)進(jìn)行轉(zhuǎn)換

 

  數(shù)值處理,具體參考:Built-ins for numbers

http://freemarker.org/docs/ref_builtins_number.html#ref_builtin_string_for_number

數(shù)值處理的例子:

<#setting number_format="currency"/>
<#assign answer=42/>
${answer}
${answer?string}  <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent} 

除了使用內(nèi)置的formate,可以使用任何用Java decimal number format syntax書(shū)寫的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語(yǔ)言中的Date類型的不足,freemarker不能根據(jù)Date變量判斷出變量包含的部分(日期、時(shí)間還是全部),在這種情況下,freemarker
不能正確顯示出${lastUpdated?string.short} 或者 simply ${lastUpdated},因此,可以通過(guò)?date, ?time and ?datetime built-ins
來(lái)幫助freemarker來(lái)進(jìn)行判斷,比如${lastUpdated?datetime?string.short}
 
除了使用內(nèi)置的日期轉(zhuǎn)換格式外,可以自己指定日期的格式,使用的是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')'")} 
    • 數(shù)值專用方式Numerical interpolations):#{expression} or #{expression; format},這是數(shù)值專用的輸出方式,但是最好使用通用方式的string built-in或者number_format來(lái)完成轉(zhuǎn)換,Numerical interpolations方式將會(huì)被停用

(8)創(chuàng)建自定義模版

創(chuàng)建:
<#macro greet>
  <font size="+2">Hello Joe!</font>
</#macro>

使用:<@greet></@greet> 或者<@greet/>