首页> 文章> 详情

Thymeleaf 递归

教程分享 > Java教程 (2069) 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运行演示

运行结果
 
https://www.leftso.com/article/582.html

相关文章
Thymeleaf 递归,Thymeleaf模板引擎递归展示如评论留言等场合适用
SSH/SSM项目中如何集成thymeleaf?本文将讲解SSH/SSM项目中如何集成thymeleaf模板引擎
Java如何复制目录,Java基础教程系列,如果要将目录及其包含的所有子文件夹和文件从一个位置复制到另一个位置,请使用下面的代码,该代码使用递归遍历目录结构,然后使用Files.copy()函数...
本文说一下在thymeleaf模板引擎中,如何给 textarea 赋值
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 ...
       快速排序算法是最常用的排序算法之一,尤其是对大型列表/数组进行排序。快速排序是一种分而治之算法,这意味着原始数组被分成两个数组,每个数组单独排序,然后合并排序输出以生成排序数组。平...
json-path 组件使用java 版 jsonpath引入依赖        &lt;dependency&gtl;            &lt;groupId&gtl;com.jay...
出现@Transactional事务不生效原因shiro 的Realm 中注入了用到事务的service,例如下面的​ /** * 自定义权限认证器 * 自定义实现Realm,实现自定义获取...
       学习使用Gson JsonReader类,这是一个基于拉的流式JSON解析器,它有助于将JSON作为标记流来读取​GSON1. JsonReader是什么JsonReader是流式...
spring boot 开发技巧,在开发web项目中跳过thyemeleaf模板/js/css等缓存避免每次修改资源文件都需要重启服务器