freemarker循环遍历List – list指令
//IndexController.java
List<String> colorList = Arrays.asList(new String[] {"RED", "GREEN", "BLUE"});
model.addAttribute("colorList", colorList);
//news.ftl
<#list colorList as color>
Color ${color_index}/${colorList?size}: ${color}}
</#list>
freemarker循环遍历Map – list指令
//IndexController.java
Map<String , String> numMap = new HashMap<String, String>();
for(int i = 0; i < 4; ++i) {
numMap.put(String.valueOf(i), String.valueOf(i * i));
}
model.addAttribute("numMap", numMap);
//news.ftl
<#list numMap?keys as key>
Number ${key}: ${numMap[key]}
</#list>
freemarker属性访问
${user.name}
${user.getName()}
include指令
对于include引入的文件内容,freemarker将解析其中的freemarker语法并移交给模板,同时assign的值可以互相调用
parent.ftl
<html>
<body>
我是公共的页面,${who}引用了我!
</body>
</html>
include.ftl
<#assign who="include.ftl">
<#include "parent.ftl">
测试结果: