(原)Springboot + thymeleaf 自動新增 許可權選單功能
阿新 • • 發佈:2018-12-04
效果圖:
管理員許可權選單使用者許可權選單
程式碼:
Java的:
List<primarySystemMenu> listMenu = null; listJuri = pJurisdictionService.findStoreMenuByStoreId( pAdminUser.getStoreId() ); //根據登入使用者身份 判斷許可權選單及訪問路徑 for (int i = 0; i < listJuri.size(); i++) { if ( String.valueOf( pAdminUser.getStatus() ).equals(listJuri.get(i).getJurName()) ) { String[] JurHave = listJuri.get(i).getJurHave().split(","); //許可權列表選單 listMenu = pSystemMenuService.findMenuByJurHave(pAdminUser.getStoreId(),JurHave); } } modelMap.addAttribute("listMenu", listMenu);
Java的遞迴建樹:
@Override public List<primarySystemMenu> findMenuByJurHave(String storeId,String[] strJurHave) { Specification<primarySystemMenu> spec = JuriUtils.toPredicateValue(strJurHave); List<primarySystemMenu> listMenu = pSystemMenuRepository.findAll(spec); //Java實現遞迴 建立樹形結構 List<primarySystemMenu> treesList = primarySystemMenuServiceImpl.buildByRecursive(listMenu); return treesList; } /** * 使用遞迴方法建樹 * @param listMenu * @return */ public static List<primarySystemMenu> buildByRecursive(List<primarySystemMenu> listMenu) { List<primarySystemMenu> trees = new ArrayList<primarySystemMenu>(); for (primarySystemMenu treeNode : listMenu) { if ( 0 == treeNode.getPid() ) { trees.add(findChildren(treeNode,listMenu)); } } return trees; } /** * 遞迴查詢子節點 * @param listMenu * @return */ public static primarySystemMenu findChildren(primarySystemMenu treeNode,List<primarySystemMenu> listMenu) { for (primarySystemMenu it : listMenu) { if(treeNode.getId() == it.getPid() ) { if (treeNode.getList() == null) { treeNode.setList(new ArrayList<primarySystemMenu>()); } treeNode.getList().add(findChildren(it,listMenu)); } } return treeNode; }
java springboot底層動態拼接在條件中
public static <T> Specification<T> toPredicateValue(String[] strJurHave) { Specification<T> spec = new Specification<T>() { private static final long serialVersionUID = 1L; @Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<Predicate>(); In<Long> in = cb.in(root.get("id")); for (int i = 0; i < strJurHave.length; i++) { in.value( Long.parseLong( strJurHave[i] ) ); } predicates.add(in); return cb.and(predicates.toArray( new Predicate[predicates.size()] )); } }; return spec; }
建立實體模型層方法:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
long id;// id
@Column(name="pid")
int pid;//父id
@Column(name="menu_name")
String menuName;//選單名字
@Column(name="checked")
boolean checked = false;//是否被選中
@Column(name="menu_href")
String menuHref;//選單路徑
@Column(name="menu_icon")
String menuIcon;//選單圖示
@Column(name="desciption")
String desciption;//選單描述
@Column(name="menu_flag")
String menuFlag;//必選選單:0 可選選單:1
@Transient
List<primarySystemMenu> list;//構造樹形結構選單的層級關係
HTML:
<li th:each="list: ${listMenu}">
<a href="javascript:;" th:if="${list.pid==0}">
<i class="iconfont"></i>
<cite th:text="${list.menuName}"></cite>
<i class="iconfont nav_right"></i>
</a>
<ul class="sub-menu" th:each="listMenuTreeNode : ${list.list}">
<li th:if="${listMenuTreeNode.pid!=0}">
<a th:href="${listMenuTreeNode.menuHref}" >
<i class="iconfont"></i>
<cite th:text="${listMenuTreeNode.menuName}"></cite>
</a>
</li>
</ul>
</li>
以上是自己整理的,並測試過,可以直接用
-------------------------------------------------- -------------------------------------------------- ------------
文章中,有問題,可以在評論區評論,一起探討程式設計中奧祕!
-------------------------------------------------- -------------------------------------------------- ------------
來都來了,程式碼看都看了,那就留個言唄,可以互動下!
-------------------------------------------------- -------------------------------------------------- ------------