Thymeleaf 递归

Thymeleaf 递归

一.Thymeleaf 递归环境准备

  • Java IDE一枚
  • Spring Boot 项目
  • Thymeleaf 依赖

二.Thymeleaf 递归demo项目结构图

三.Thymeleaf 递归核心实现文件

数据对象

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 递归demo运行演示



 

Thymeleaf 递归

相关文章:

你感兴趣的文章:

标签云: