java freeMarker 模板引擎
程序员文章站
2022-03-07 22:33:43
...
IDEA freeMarker 引入模板引擎+代码补全
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
application.properties文件中加入
#用ftl文件后缀,IDEA会识别此文件出现代码补全提示。
spring.freemarker.suffix=.ftl
java后端写法
@Controller
@RequestMapping("/demo")
public class DemoController {
/**
* freeMarker 遍历 list,map,list<map>
*/
@RequestMapping("/")
public String test(ModelMap modelMap) {
modelMap.addAttribute("test","测试");
List<String> clientSourceDataList=new ArrayList<String>();
clientSourceData.add("field字段");
clientSourceData.add("title标题");
modelMap.addAttribute("clientSourceDataList",clientSourceDataList);
Map<String,String> clientSourceDataMap=new HashMap<String,String>();
clientSourceData.put("field", "字段");
clientSourceData.put("title", "标题");
modelMap.addAttribute("clientSourceDataMap",clientSourceDataMap);
}
}
}
具体.ftl文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<#-- 遍历List-->
<#if clientSourceData?exists>
<#list clientSourceData as key>
<tr>
<td>${key}</td>
</tr>
</#list>
</#if>
<#-- 遍历List也可以这么写-->
<#list list>
<ul>
<#items as key>
<li>${key}</li>
</#items>
</ul>
</#list>
<#-- 遍历MAP -->
<#if clientSourceData?exists>
<#list clientSourceData?keys as key>
<tr>
<td>${key}</td>
<td>${clientSourceData[key]}</td>
</tr>
</#list>
</#if>
<#-- 遍历List<Map>-->
<#list columns as col>
{
field : ${col["field"]},
halign:'center',
title : ${col["title"]},
}
</#list>
</body>
</html>
FreeMarker 英文官方参考手册:https://freemarker.apache.org/
FreeMarker 中文官方参考手册:http://freemarker.foofun.cn/ref_directive_include.html
上一篇: Freemarker 模板引擎
下一篇: 五要素测试