Freemarker简单使用


以#开头的指令是freemarker的内置指令,用户自动的的指令以@开头
Freemarker区分大小写
指令之间有包裹内容时需要结束标记,没有内容时不需要结束标记<#if condition> xxx </#if> <#include "/file"> ;与内置指令不同用户自定义指令以@开头,并且在没有包裹内容时,需要增加'/'标记,如<@myDirection condition />
Freemarker指令列表大全 点我
Freemarker内置变量大全 点我 ,访问内置变量使用'.' ,如 当前时间${.now}
Freemarker内置引用大全 点我 ,使用内置引用很简单,如处理字符串的内置引用${name?low_case?trim}
FreeMarker never converts a string to a number automatically, but it may convert a number to a string automatically.
An attempt to print boolean values with interpolation causes an error and aborts template processing. For example this will cause an error: ${a == 2} and will not print ''true'' or something like that.

变量显示
${user.name}

${user.name!"Anonymous"}
当name为空时,设置默认值为Anonymous;此处有个问题是,如果user也为空,会抛出错误。

${(user.name)!"Anonymous"}
可避免上面的问题,无论user为空还是user.name为空都没关系。

${(user.name)!}
当没有指定默认值时,等同于${(user.name)!""},如,"Welcome_${(user.name)!}_" 将输出 Welcome__

控制结构指令
<#if user.name=="PC">welcome ${user.name}</#if>

<#if user.name==1>
price 1
<#else>
price bigger than 1
</#if>

<#if user.name??>user.name undefined</#if>
<#if (user.name)??>user or user.name undefined</#if>

列表循环指令
<#list users as user>
${user.name}  ${user.age}
</#list>

页面包含指令
<#include "/footer.jsp">

你可能感兴趣的:(freemarker)