assign指令
assign指令用于在页面上定义一个变量
(1)定义简单类型
<#assign linkman="张先生">
联系人:${linkman}
(2)定义对象类型
<#assign info={"mobile":"13812345678",'address':'深圳'} >
电话:${info.mobile} 地址:${info.address}
<html>
<head>
<meta charset="utf-8">
<title>Freemarker入门</title>
</head>
<body>
<#assign linkman="张先生">
联系人:${linkman}
</br>
<#assign info={"mobile":"13812345678",'address':'深圳'} >
电话:${info.mobile} 地址:${info.address}
</body>
</html>
生成的文件:
include指令
include指令用于模板文件的嵌套, 用于页面布局, 页面公共内容的抽取
(1)创建模板文件head.ftl
<h1>我是头</h1>
(2)修改入门案例中的ftl.ftl,在ftl.ftl模板文件中使用include指令引入上面的模板文件
<#include "head.ftl"/>
head.ftl:
<h1>我是头</h1>
在上边的案例中引入此模板:
<html>
<head>
<meta charset="utf-8">
<title>Freemarker入门</title>
</head>
<body>
<#include "head.ftl"/>
<#assign linkman="张先生">
联系人:${linkman}
</br>
<#assign info={"mobile":"13812345678",'address':'深圳'} >
电话:${info.mobile} 地址:${info.address}
</body>
</html>
生成的页面:
if指令
if指令用于判断
(1)在模板文件中使用if指令进行判断
<#if success=true>
你已通过实名认证
<#else>
你未通过实名认证
</#if>
(2)在java代码中为success变量赋值
map.put("success", true);
生成的网页:
list指令
list指令用于遍历
(1)在模板文件中使用list指令进行遍历
<#list goodsList as goods>
商品名称: ${goods.name} 价格:${goods.price}<br>
</#list>
(2)在java代码中为goodsList赋值
List<Map<String,String>>maps=new ArrayList<>();
Map map1=new HashMap();
map1.put("name","你大爷的鞋子");
map1.put("price","6666.99");
Map map2 = new HashMap();
map2.put("name", "套猴子呢");
map2.put("price", "9999.99");
maps.add(map1);
maps.add(map2);
map.put("goodsList",maps);
输出文件: