编程语言
首页 > 编程语言> > JavaWeb || FreeMarker语法

JavaWeb || FreeMarker语法

作者:互联网

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">

测试结果:
在这里插入图片描述

标签:JavaWeb,freemarker,ftl,语法,numMap,include,colorList,FreeMarker,String
来源: https://blog.csdn.net/qq_40283353/article/details/89036074