阅读完需:约 2 分钟
在FreeMarker中,使用FTL标签来使用指令,FreeMarker有3种FTL标签,这和HTML标签是完全类似的.
1,开始标签:<#directivename parameter>
2,结束标签:</#directivename>
3,空标签:<#directivename parameter/>
实际上,使用标签时前面的符号#也可能变成@,如果该指令是一个用户指令而不是系统内建指令时,应将#符号改成@符号.
使用FTL标签时, 应该有正确的嵌套,而不是交叉使用,这和XML标签的用法完全一样.如果全用不存在的指令,FreeMarker不会使用模板输出,而是产生一个错误消息.FreeMarker会忽略FTL标签中的空白字符.值得注意的是< , /> 和指令之间不允许有空白字符.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>用户编号</td>
<td>用户名称</td>
<td>用户地址</td>
<td>性别</td>
</tr>
<#list users as user>
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.address}</td>
<td>
<#-- <#if user.gender==0>-->
<#-- 男-->
<#-- <#elseif user.gender==1>-->
<#-- 女-->
<#-- <#else>-->
<#-- 其他-->
<#-- </#if>-->
<#switch user.gender>
<#case 0>男<#break >
<#case 1>女<#break >
<#default>未知
</#switch>
</td>
</tr>
</#list>
</table>
</body>
</html>
效果:

更多指令:
<!--模板包含某个页面-->
<#include "header.ftl">
.....
<#include "footer.html">
<!--输出值不为null的标题-->
${question.title!}
<!--判断followed 为true-->
<#if followed>
<button>取消关注</button>
<#else>
<button>关注问题</button>
</#if>
<!--判断followed 不为null且存在值-->
<#if vo.followed ??>
<button>取消关注</button>
<#else>
<button>关注</button>
</#if>
<!--输出个数-->
<a href="">${followUsers?size}</a>人关注该问题
<!--循环list-->
<#list followUsers as vo>
<img src="${vo.headUrl}">
</#list>
<!--输出日期格式-->
${(comment.comment.createdDate)!?string("yyyy-MM-dd HH:mm:ss")}
<!--list中循环调用宏-->
<#macro comment_question vo>
${vo.questionTitle}
</#macro>
<#macro follow_question vo>
${vo.questionTitle}
</#macro>
<#list feeds as feed>
<#if feed.type ==1>
<@comment_question vo=feed />
<#elseif feed.type==4>
<@follow_question vo=feed/>
</#if>
</#list>
https://blog.csdn.net/java_leejin/article/details/97499922
https://blog.csdn.net/xiangcaoyihan/article/details/80930179