Thymeleaf 递归

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(1468)   2023-03-28 11:29:14

一.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 递归
地址:https://www.leftso.com/article/582.html

相关阅读

Thymeleaf 递归,Thymeleaf模板引擎递归展示如评论留言等场合适用
SSH/SSM项目中如何集成thymeleaf?本文将讲解SSH/SSM项目中如何集成thymeleaf模板引擎
本文说一下在thymeleaf模板引擎中,如何给 textarea 赋值
Java如何复制目录,Java基础教程系列,如果要将目录及其包含的所有子文件夹和文件从一个位置复制到另一个位置,请使用下面的代码,该代码使用递归遍历目录结构,然后使用Files.copy()函数...
thymeleaf模板 报错信息:​​​​​​​org.thymeleaf.exceptions.TemplateInputException: Error resolving template...
thymeleaf 设置不校验html标签
springboot 使用thymeleaf 模板引擎中url中的&引起的org.xml.sax.SAXParseException: 对实体 "uid" 的引用必须以 ';' 分隔符结尾。问题解决
本文将讲述什么是自旋锁?自旋锁的使用场景,什么情况适合使用自旋锁?Java 怎么使用自旋锁?
一、项目环境Spring Boot 2.1.2.RELEASEshiro-spring 1.4二、去掉URL jsessionid在shiro配置中,配置关闭url中显示sessionId ...