Thymeleaf 递归

编程教程 > Java > Thymeleaf (2389) 2024-11-26 14:39:04

一.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 递归显示【本页面申明和调用

 


评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
Thymeleaf 递归,Thymeleaf模板引擎递归展示如评论留言等场合适用
设置数据源将数据写入到 Model 中 @RequestMapping("/menulist") public String MenuManagerList(M
SSH/SSM项目中如何集成thymeleaf?本文将讲解SSH/SSM项目中如何集成thymeleaf模板引擎
使用thymeleaf 模板创建时间 使用thymeleaf 创建指定日期为:2023-10-18 #dates.create(2023,10,18) 使用thymeleaf ...
thymeleaf 设置不校验html标签
本文说一下在thymeleaf模板引擎中,如何给 textarea 赋值
thymeleaf模板 报错信息:​​​​​​​org.thymeleaf.exceptions.TemplateInputException: Error resolving template...
环境Thymeleaf 3.0循环5次需求:输出5个li编码参考:&lt;ul&gtl; &lt;li th:each="index:${#numbers.sequence(1,
springboot 使用thymeleaf 模板引擎中url中的&引起的org.xml.sax.SAXParseException: 对实体 "uid" 的引用必须以 ';' 分隔符结尾。问题解决
配置在resources目录下面创建i18n目录,然后创建几个文件messages.properties 默认显示语言内容messages_en_US.prop