Thmeleaf 递归调用
场景: 属性菜单html文档结构输出
数据格式:
public class Menu implements Serializable{ /** * */ private static final long serialVersionUID = 1L; /** * 主键 */ private Long id; /** * 父节点Id */ private Long parentId; /** * 枚举 1节点 2菜单 3菜单下功能 */ private String menuType; /** * 菜单名称、 功能名称 */ private String menuName; /** * 菜单地址、 功能地址 */ private String menuUrl; /** * 为菜单下功能时, 对应页面Id属性 或 if条件参数 */ private String theFlag; /** * 是否有子节点 * 1 有 0无 */ private String hasSub; /** * 顺序 */ private int sort; /** * 等级 */ private int grade; /** * 所属系统Id */ private Long sysId; /** * 启用标记 */ private String enFlag; /** * 删除标记 */ private String delFlag; /** * 创建时间 */ private Date createTime; /** * 删除时间 */ private Date delTime; /** * 子菜单 */ private List<Menu> children; }
定义方法:
<th:block th:fragment="row(list, currentMenu, rowId)" > <th:block th:each="menu,menuStat : ${list}" > <tr> <td th:text="${menu.id}"></td> <td th:include="this::sp(${menu.grade}, ${menu.menuName}, ${menu.hasSub})"></td> <td th:if="${menu.menuType == '1'}"> <span class="glyphicon glyphicon-certificate"></span>节点 </td> <td th:if="${menu.menuType == '2'}"> <span class="glyphicon glyphicon-leaf"></span>菜单 </td> <td th:if="${menu.menuType == '3'}"> <span class="glyphicon glyphicon-fire"></span>功能 </td> <td th:text="${menu.hasSub == '1'} ? '存在' : '不存在'"> <td th:text="${menu.menuUrl}"></td> <td th:text="${menu.theFlag}"></td> <td th:text="${menu.grade}"></td> <td th:text="${menu.sort}"></td> <td th:text="${#dates.format(menu.createTime,'yyyy-MM-dd HH:mm:ss')}"></td> <td> <div class="switch switch-mini"> <input type="checkbox" class="enFlag" th:value="${menu.enFlag}" th:menuId="${menu.id}" th:checked="${menu.enFlag == '1'}" /> </div> </td> <td> <span th:unless="${menu.menuType == '3'}" th:onclick="'addSubMenu(\''+ ${menu.id} +'\')'" class="glyphicon glyphicon-plus-sign" title="添加子节点"></span> <span th:onclick="'move(\''+ ${menu.id} +'\')'" class="glyphicon glyphicon-move" title="移动"></span> <span th:onclick="'edit(\'' + (${currentMenu != null} ? ${currentMenu?.menuName} : '顶级菜单') + '\',\''+ ${menu.id} +'\')'" class="glyphicon glyphicon-pencil" ></span> <span th:onclick="'remove(\'' + ${menu.menuName} + '\',\''+${menu.id}+'\')'" class="glyphicon glyphicon-trash"></span> </td> </tr> <th:block th:unless="${#lists.isEmpty(menu.children)}" th:include="this::row(${menu.children}, ${menu}, ${rowId})"/> </th:block> </th:block>
调用方法:
<table class="table table-striped table-bordered table-hover"> <thead> <tr> <th width="25px">ID</th> <th width="200px">菜单/功能名称</th> <th width="40px">类型</th> <th width="40px">子节点</th> <th width="120px">URL</th> <th width="60px">页面标记</th> <th width="30px">层级</th> <th width="30px">顺序</th> <th width="150px">创建时间</th> <th width="60px">状态</th> <th width="60px">操作</th> </tr> </thead> <tbody th:include="this::row(${menuList}, null, 0)"/> </table>