1. Object creation:
package com.demo.Model; import lombok.Getter; import lombok.Setter; import lombok.ToString; import java.io.Serializable; import java.util.List; @Getter @Setter @ToString public class PriMenu implements Serializable { private static final long serialVersionUID = -7458870847408160287L; private Long id; private Long pId; // 父节点id private String menuName;// 菜单名称 privateMenuUrl String; // access address Private String priLevel; // menu hierarchy Private String priPath; // permissions menu hierarchy path Private String Status; // status 0 Available 1 disabled Private Long creatorId; // Created the above mentioned id Private String createTime; Private List <PriMenu> sonList; public PriMenu () { }
2. Object Initialization
package com.demo.Common.constants; import com.demo.Model.PriMenu; import java.util.ArrayList; import java.util.List; public class MenuDemo { //菜单假数据 public static List<PriMenu> Initialization(){ List<PriMenu> list = new ArrayList<PriMenu>(); PriMenu priMenu1 = new PriMenu(); priMenu1.setMenuName("系统管理"); List<PriMenu> list1 = new ArrayList<PriMenu>(); PriMenuSon1 PriMenu = new new PriMenu (); priMenuSon1.setMenuName ( "Menu Manager" ); PriMenu priMenuSon2 = new new PriMenu (); priMenuSon2.setMenuName ( "Role Management" ); PriMenu priMenuSon3 = new new PriMenu (); priMenuSon3.setMenuName ( "permission management " ); PriMenu priMenuSon4 = new new PriMenu (); priMenuSon4.setMenuName ( " user management " ); list1.add (priMenuSon1); list1.add (priMenuSon2); list1.add (priMenuSon3); list1.add (priMenuSon4); priMenu1.setSonList (List1); list.add (priMenu1); PriMenu2 PriMenu = new PriMenu (); priMenu2.setMenuName ( "列表管理" ); A List <PriMenu> List2 = new an ArrayList <PriMenu> (); PriMenuSon5 PriMenu = new PriMenu (); priMenuSon5.setMenuName ( "借款列表" ); PriMenuSon6 PriMenu = new PriMenu (); priMenuSon6.setMenuName ( "日志管理" ); list2.add (priMenuSon5); list2.add(priMenuSon6); priMenu2.setSonList (list2); list.add (priMenu2); PriMenu priMenu3 = new new PriMenu (); priMenu3.setMenuName ( "management function" ); List <PriMenu> list3 = new new the ArrayList <PriMenu> (); PriMenu priMenuSon7 = new new PriMenu ( ); priMenuSon7.setMenuName ( "send a message" ); priMenuSon7.setMenuUrl ( "toUEditor.jhtml" ); PriMenu priMenuSon8 = new new PriMenu (); priMenuSon8.setMenuName ( "export table" ); list3.add (priMenuSon7); list3.add (priMenuSon8); priMenu3.setSonList (list3); list.add (priMenu3); return statement list; } }
3.Controller
package com.demo.Controller; import com.demo.Common.constants.MenuDemo; import com.demo.Model.PriMenu; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j @Controller public class ModelAndViewController { @RequestMapping("toMain.jhtml") public String mainView(HttpServletRequest request, ModelMap model) { log.info("跳转到后台首页"); Map<String, Object> param = new HashMap<String, Object>(); List<PriMenu> list = new ArrayList<PriMenu>(); list=MenuDemo.Initialization(); model.addAttribute("menus", list); return "pri/main"; } }
4.thymeleaf use (mainly Code)
<li class="layui-nav-item layui-this" th:each="menu:${menus}"> <a href="javascript:;" th:text="${menu.menuName}">系统管理</a> <ul class="layui-nav-child" style="background: #2b2e37;"> <li th:each="child:${menu.sonList}"> <a th:if="${#lists.isEmpty(child.sonList)}" th:text="${child.menuName}" th:href="@{'/'+${child.menuUrl}}" href="javascript:;" style="margin-left: 20px;">用户管理</a> </li> </ul> </li>
NOTE: portions to be configured (as long as the introduction of automatic loading pom.xml dependent)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.1.1</version> </dependency>