数据对象
package com.example.demothymeleafrecursive;
import java.util.List;
public class TreeItem {
Integer id;
Integer pid;
String name;
List<TreeItem> childs;
public TreeItem() {
}
public TreeItem(Integer id, Integer pid, String name) {
this.id = id;
this.pid = pid;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<TreeItem> getChilds() {
return childs;
}
public void setChilds(List<TreeItem> childs) {
this.childs = childs;
}
}
展示主页:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf递归实现</title>
</head>
<style type="text/css">
.item-level{margin-left: 40px;}
</style>
<body>
<div class="box" >
<th:block th:include="recursive::tree(${items},1)"></th:block>
</div>
</body>
</html>
递归模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:fragment="tree(its,level)">
<div class="item" th:class="${level eq 1 ? 'item':'item item-level'}" th:each="it:${its}" >
<label th:text="${it.name}"></label>
<div th:unless="${#lists.isEmpty(it.childs)}" th:include="this::tree(${it.childs},${level+1})"></div>
</div>
</div>
</body>
</html>
另外本页面定义递归片段本页面调用参考 thymeleaf 递归显示【本页面申明和调用
http://blog.xqlee.com/article/582.html