struts2.0 标签+ftl标签 (转1)

上一篇 / 下一篇  2012-09-21 16:48:21 / 个人分类:框架应用

FreeMarker中文API手册(完整http://blog.csdn.net/junjun16818/article/details/6990068

拆分字符数组
<#if (lineInfo.lineDate?exists)&&(lineInfo.lineDate?length>10)>
<#list lineInfo.lineDate?split(",") as d>
<input type="text" name="lineInfo.lineDate" id="lineDate" value="${d}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd', skin:'whyGreen'})"/>
</#list>
</#if>

1.注释:
包含在<#---->

2.注意: 
由于Freemarker会将>解释成FTL标记的结束字符,所以对于>>=可以使用括号来避免这种情况,例如

<#if (x > y)>

3.<#local y = "test"> 定义局部变量

4.<#import "/lib/my_test.ftl" as my> 导入模板文件 指定名字空间 my

5. if指令

<#if animals.python.price < animals.elephant.price>

  Pythons are cheaper than elephants today.

<#else>

  Pythons are not cheaper than elephants today.

</#if>

6. list指令以及列表序号

  <#list animals as being>

  第${being_index+1}<#--默认是0开始-->

  <tr><td>${being.name}<td>${being.price} Euros

  </#list>

  <#list ["winter", "spring", "summer", "autumn"] as x>

       ${x}

  </#list>

<#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>

- ${user}

</#list>

输出结果是:

- Joe

- Fred

- Julia

- Kate

include指令

<#include "/copyright_footer.html">

7.判断是否为空
${userInfo.userName?if_exists}

<#if ((user.sex)!'')=='1'><#elseif ((user.sex)!'')=='2'></#if>
<#if searchType ? exists && searchType=='on'>checked</#if>
<#if time ? exists && (time!'')=='y' || (time!'')=='m' || (time!'')=='d'>统计条件<#else>保证金返款数量</#if>
<#if ((time)!'')=='y'>按年统计<#elseif ((time)!'')=='m'>按月统计<#elseif ((time)!'')=='d'>按日统计<#else>默认按日统计</#if>

8.截取字符串
${carInfo.carNum.substring(0,1)}

9.freemarkerreplace功能
替换字符串 replace
线路标签:${lineInfo.lineLableDescribe?replace('#','')}
${s?replace(baXY’ )}
${s?replace(baXY’ 规则参数)}s里的所有的ba替换成xy 规则参数包含: i r m s c f 具体含义如下:
· i: 大小写不区分.
· f: 只替换第一个出现被替换字符串的字符串
· r:  XY是正则表达式
· m: Multi-line mode for regular expressions. In multi-line mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the string. By default these expressions only match at the beginning and the end of the entire string.
· s: Enables dotall mode for regular expressions (same as Perl singe-line mode). In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
· c: Permits whitespace and comments in regular expressions.


10.三目运算
${true?string('5','7')} 

11.string格式化单个Interpolation,下面是一个例子:

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

输出结果是:

$42.00
$42.00
42
$42.00
4,200%

12.插入日期值:根据缺省格式(由#setting指令设置)将表达式结果转换成文本输出;可以使用内建函数string格式化单个Interpolation,下面是一个使用格式模式的例子:


TAG: ftl

 

评分:0

我来说两句

Open Toolbar