thymeleaf 递归显示

教程分享 > Java教程 > Thymeleaf (199) 2024-08-06 15:09:59

设置数据源

将数据写入到 Model

    @RequestMapping("/menulist")
   public String MenuManagerList(Model model){
       List<MenuManagerDO> menuManagerList = menuManagerService.MenuManagerList();
       model.addAttribute("menuManagerList", menuManagerList);
       return "menulist";
   }

在html文件中设置html 头

<html xmlns:th="http://www.thymeleaf.org">

 

 

设置递归模板

<th:block th:fragment="row(menuManagers)">
        <ul th:each="menu,index:${menuManagers}">
            <li th:if="${#lists.isEmpty(menu.subMenuManager)}">
                <p th:text="${menu.menu_name}"></p>
            </li>
            <li th:if="not ${#lists.isEmpty(menu.subMenuManager)}" >
                <p th:text="${menu.menu_name}"></p>
                <th:block  th:include="this::row(${menu.subMenuManager})"></th:block>
            </li>
        </ul>
</th:block>

 

引用递归模板

<div th:include="this::row(${menuManagerList})"/>

提示:this::表示递归模板在本页其他文件则需要指定路径

完整模板代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/login.css" />
       <script type="text/javascript" src="js/jquery.min.js"></script>
       <script type="text/javascript" src="js/login.js"></script>
</head>
<body>
    <div th:include="this::row(${menuManagerList})"/>
    <th:block th:fragment="row(menuManagers)">
        <ul th:each="menu,index:${menuManagers}">
            <li th:if="${#lists.isEmpty(menu.subMenuManager)}">
                <p th:text="${menu.menu_name}"></p>
            </li>
            <li th:if="not ${#lists.isEmpty(menu.subMenuManager)}" >
                <p th:text="${menu.menu_name}"></p>
                <th:block  th:include="this::row(${menu.subMenuManager})"></th:block>
            </li>
        </ul>
    </th:block>

</body>
</html>

列表呈现实体类

public class MenuManagerDO {
   private Integer id;
   private Integer pid;
   private String menu_name;
   private List<MenuManagerDO> subMenuManager = new ArrayList<>();
   public List<MenuManagerDO> getSubMenuManager() {
       return subMenuManager;
   }
   public void setSubMenuManager(List<MenuManagerDO> subMenuManager) {
       this.subMenuManager = subMenuManager;
   }
   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 getMenu_name() {
       return menu_name;
   }
   public void setMenu_name(String menu_name) {
       this.menu_name = menu_name;
   }
}

 

mybaties Map xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.demoproject.mapper.MenuManager">
   <resultMap id="baseMenuManagerResultMap" type="com.demo.demoproject.pojo.MenuManagerDO">
       <id column="id" property="id" />
       <result column="pid" property="pid" />
       <result column="menu_name" property="menu_name"/>
       <collection property="subMenuManager" ofType="com.demo.demoproject.pojo.MenuManagerDO" column="{pid=id}" select="selectAll"/>
   </resultMap>
   <select id="selectAll" resultMap="baseMenuManagerResultMap">
           select  * from `menu_manager` where `pid` = #{pid}
   </select>
</mapper>

dao 接口

public interface MenuManager {
    List<MenuManagerDO> selectAll(@Param("pid") Integer pid);
}

数据表结构

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for menu_manager
-- ----------------------------
DROP TABLE IF EXISTS `menu_manager`;
CREATE TABLE `menu_manager`  (
 `id` int(11) NOT NULL,
 `menu_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
 `pid` int(11) NULL DEFAULT NULL,
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

 

https://www.leftso.com/article/2408061245091060.html

相关文章
Thymeleaf 递归,Thymeleaf模板引擎递归展示如评论留言等场合适用
设置数据源将数据写入到 Model 中 @RequestMapping("/menulist") public String MenuManagerList(M
thymeleaf 设置不校验html标签
SSH/SSM项目中如何集成thymeleaf?本文将讲解SSH/SSM项目中如何集成thymeleaf模板引擎
使用thymeleaf 模板创建时间 使用thymeleaf 创建指定日期为:2023-10-18 #dates.create(2023,10,18) 使用thymeleaf ...
本文说一下在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