6. 输出
T text, and the params: a, b, 23
est
T text, and the params: a, b, -1
est
T text, and the params: a, Bar, 23
est
T text, and the params: a, Bar, -1
est
定义循环输出的宏
Ftl代 码
1. <#macro list title items>
2. ${title?cap_first}:
3. <#list items as x>
4. *${x?cap_first}
5.
6. </#list>
7. </#macro>
8.
9. <@list items=["mouse", "elephant", "python"] title="Animals"/>
输出结果:
Animals:
*Mouse
*Elephant
*Python
包含body 的宏
Ftl代 码
1. <#macro repeat count>
2. <#list 1..count as x>
3. <#nested x, x/2, x==count>
4. </#list>
5. </#macro>
6.
7. <@repeat count=4 ; c halfc last>
8. ${c}. ${halfc}<#if last> Last!</#if>
9. </@repeat>
输出
1. 0.5
2. 1
3. 1.5
4. 2 Last!
t, lt, rt
语法
Freemarkder代 码
1. <#t> 去掉左右空白和回车换行
2.
3. <#lt> 去掉左边空白和回车换行
4.
5. <#rt> 去掉右边空白和回车换行
6.
7. <#nt> 取消上面的效果
C 一些常用方法或注意事项
一些常用方法或注意事 项
表达式转换类
${expression} 计算expression 并输出
#{ expression } 数字计算#{ expression ;format} 安格式输出数字 format 为 M 和 m
M 表示小数点后最多的位数,m 表示小数点后最少的位数如 #{121.2322;m2M2} 输出 121.23
数字循环
1..5 表示从1 到 5 ,原型 number..number
对浮点取整数
${123.23?int} 输出 123
给变量默认值
${var?default("hello world")?html} 如果var is null 那么将会被 hello world 替代
判断对象是不是 null
Ftl代 码
1. <#if mouse?exists>
2. Mouse found
3. <#else>
converted by Web2PDFConvert.com
7. 也可以直接${mouse?if_exists})输出布尔形
--------------------------------------------
(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 的标签会在性能上有所损失
(4)freemarker的标签种类:
${..}:FreeMarker will replace it in the output with the actual value of the thing in the curly brackets. They are called interpolation
s.
# ,代表是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:foobar"}
<#list ["winter", "spring", "summer", "autumn"] as x>${x}</#list>
?引出内置指令
String处理指令:
html:特殊的html字符将会被转义,比如"<",处理后的结果是<
cap_first 、lower_case 、upper_case
trim :除去字符串前后的空格
sequences处理指令
size :返回sequences的大小
numbers处理指令
int:number的整数部分,(e.g. -1.9?int is -1)
(6)对于null,或者miss value,freemarker会报错
?exists:旧版本的用法
!: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
Ftl代 码
1. <#if mouse??>
2. Mouse found
3. <#else>
4. No mouse found
5. </#if>
converted by Web2PDFConvert.com
8. 6. Creating mouse...
7. <#assign mouse = "Jerry">
8. <#if mouse??>
9. Mouse found
10.
11. <#else>
12. No mouse found
13. </#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"/>
<#setting number_format="0"/>
<#setting number_format="#"/>
${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}
注意:
converted by Web2PDFConvert.com