ssm 實現動態選單 手動加入許可權
阿新 • • 發佈:2018-12-15
思路一:在許可權內查出所有能用的選單列表然後進行聯絡上父子關係 package com.mpn.service.imp.sys; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.mpn.dao.TMenuMapper; import com.mpn.dao.TPowerMapper; import com.mpn.dao.TRoleMapper; import com.mpn.model.TCostExample.Criteria; /* * 動態選單 實現 */ import com.mpn.model.TMenu; import com.mpn.model.TPower; import com.mpn.model.TPowerExample; import com.mpn.model.TUser; import com.mpn.util.MenuRelation; @Service public class MenuManageServiceimpl { @Resource private TMenuMapper tMenuMapper; @Resource private TPowerMapper tPowerMapper; @Resource private TRoleMapper tRoleMapper; /* * 思路:通過登入的使用者 在session中的資訊來獲取當前人物的角色 並 查出此角色下的所有的選單 第一步:使用者表中有roleId * 步驟一:可以得到後直接去許可權表中進行查詢 查出所有的選單id * 步驟二:根據所有選單id查出選單 */ public List<TMenu> getAllMenuList(TUser tUser) { List<TMenu> result = new ArrayList<TMenu>(); // 查出所有的選單id TPowerExample example = new TPowerExample(); com.mpn.model.TPowerExample.Criteria criteria = example.createCriteria(); criteria.andRoleIdEqualTo(tUser.getUserRold()); List<TPower> tpowers = tPowerMapper.selectByExample(example); // 根據所有選單id查出選單 if (tpowers == null) return null; for (TPower tPower : tpowers) { TMenu tmenu=tMenuMapper.selectByPrimaryKey(tPower.getMenuId()); if(tmenu!=null){ result.add(tmenu); } } return result; } /* * 找出所有的一級選單 並放到封裝的MenuRelation中去 * 新思路待驗證 :同樣是查出父級來 查出來的所有的選單中子父關係 ,所有用到的都應該寫在許可權表中 */ public List<MenuRelation> getbaseMenuRelation(List<TMenu> tmenuList){ //存放最終結果 List<MenuRelation> result=new ArrayList<MenuRelation>(); //單個父級 MenuRelation parentMenuRelation=new MenuRelation(); if(tmenuList==null) return null; for (TMenu tmenu : tmenuList) { if(tmenu.getMenuParentId()==null){ //刪掉拿出來封裝好的選單 tmenuList.remove(tmenu); parentMenuRelation.parent=null; parentMenuRelation.self=tmenu; //搜尋孩子需要自己的id也就是父級id和範圍 parentMenuRelation.children=getMenuRelation(tmenu,tmenuList); result.add(parentMenuRelation); } } return result; } /* * 寫出一個除了父級可以依次遞迴找到孩子的方法 */ public List<MenuRelation> getMenuRelation(TMenu parentTmenu,List<TMenu> tmenuList){ //定義一個返回結果 和 下一級搜尋 List<MenuRelation> result=new ArrayList<MenuRelation>(); MenuRelation newMenuRelation=new MenuRelation(); //迴圈找出下一級 if(tmenuList==null||tmenuList.size()<=0)return null; for (TMenu tMenu : tmenuList) { if(parentTmenu.getMenuId()==tMenu.getMenuParentId()){ //刪除當前已存進去的內容 tmenuList.remove(tMenu); newMenuRelation.parent=parentTmenu; newMenuRelation.self=tMenu; newMenuRelation.children=getMenuRelation(tMenu,tmenuList); result.add(newMenuRelation); } } return result; } }
選單的controller:
package com.mpn.controller.Account; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.mpn.model.TMenu; import com.mpn.model.TUser; import com.mpn.service.imp.basic.TUserServiceimpl; import com.mpn.service.imp.sys.MenuManageServiceimpl; import com.mpn.util.MenuRelation; @Controller @RequestMapping("/index") public class IndexController { @Resource private TUserServiceimpl tuserservice; @Resource private MenuManageServiceimpl menumanageServiceimpl; /* * 獲取當前登陸使用者的動態選單 * */ @RequestMapping("/menus.do") @ResponseBody public List<MenuRelation> menuRelationList(HttpServletRequest request, HttpSession httpSession){ if(httpSession==null){ return null; } TUser user=new TUser(); user=(TUser) httpSession.getAttribute("logi_nuser"); if(user==null){ return null; } List<TMenu> tmenus = menumanageServiceimpl.getMenusByLoginUser(user); List<MenuRelation> result = menumanageServiceimpl.getbaseMenuRelation(tmenus); return result; } }
選單生成的js:
//載入選單 getMenuListR(); //設定選單樣式 function setCssForMenu(){ jQuery(document).ready(function(a) { a("ul.main-menu li a").each(function() { if (a(a(this))[0].href == String(window.location)) { a(this).parent().addClass("active") } }); a("ul.main-menu li ul li a").each(function() { if (a(a(this))[0].href == String(window.location)) { a(this).parent().addClass("active"); a(this).parent().parent().show() } }); a(".dropmenu").click(function(b) { b.preventDefault(); a(this).parent().find("ul").slideToggle() }); }); jQuery(document).ready(function(b) { var a = true; b("#main-menu-toggle").click(function() { if (b(this).hasClass("open")) { b(this).removeClass("open").addClass("close"); var f = b("#content").attr("class"); var e = parseInt(f.replace(/^\D+/g, "")); var c = e + 2; var d = "span" + c; b("#content").addClass("full"); b(".brand").addClass("noBg"); b("#sidebar-left").hide() } else { b(this).removeClass("close").addClass("open"); var f = b("#content").attr("class"); var e = parseInt(f.replace(/^\D+/g, "")); var c = e - 2; var d = "span" + c; b("#content").removeClass("full"); b(".brand").removeClass("noBg"); b("#sidebar-left").show() } }) }); jQuery(document).ready(function(a) { if (a(".boxchart")) { if (retina()) { a(".boxchart").sparkline("html", { type : "bar", height : "60", barWidth : "8", barSpacing : "2", barColor : "#ffffff", negBarColor : "#eeeeee" }); a(".boxchart").css("zoom", 0.5) } else { a(".boxchart").sparkline("html", { type : "bar", height : "30", barWidth : "4", barSpacing : "1", barColor : "#ffffff", negBarColor : "#eeeeee" }) } } }); jQuery(document) .ready( function(a) { a(".todo-actions > a") .click( function() { if (a(this).find("i").attr("class") == "icon-check-empty") { a(this).find("i").removeClass( "icon-check-empty") .addClass("icon-check"); a(this).parent().parent().find( "span").css({ opacity : 0.25 }); a(this).parent().parent().find( ".desc").css( "text-decoration", "line-through") } else { a(this).find("i").removeClass( "icon-check").addClass( "icon-check-empty"); a(this).parent().parent().find( "span").css({ opacity : 1 }); a(this).parent().parent().find( ".desc").css( "text-decoration", "none") } return false }); a(function() { a(".todo-list").sortable(); a(".todo-list").disableSelection() }) }); (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); ga('create', 'UA-9510961-22', 'clabs.co'); ga('send', 'pageview'); } /* * 獲取選單 */ // getMenuList(); /** * 根據根節點的名稱生成一級選單 * * @param menuName * @returns {String} */ function getBMenuHtml(menuName) { var bstr = '<li >' + '<a class="dropmenu" ><i class="icon-bar-chart"></i>' + '<span class="hidden-tablet" >' + menuName + '</span></a>'; return bstr; } /** * 根據選單物件生成該選單的子選單 * * @param obj * @returns {String} */ function getChildMenuHtml(obj) { var bstr = ''; var url = basePath + obj.menuUrl; // alert("url:"+url); bstr = bstr + '<li><a class="submenu" href="' + url + '"><i class="icon-hdd"></i><span class="hidden-tablet">' + obj.menuName + '</span></a></li>'; return bstr; } /** * 獲取選單列表 */ function getMenuListR() { $.ajax({ type : 'post', url : basePath + '/index/menus.do', dataType : 'json', success : function(data) {// 獲取選單列表後前端展示 if (data == null) { return; } var html = ''; if (localStorage.getItem("menu") == null) { $.each(data, function(i, item) { html += getBMenuHtml(item['self'].menuName); var childs = item['children']; if (childs != null) { // 拼接孩子-- html = html + '<ul>'; // --具體孩子的html $.each(childs, function(j, clItem) { var str = getChildMenuHtml(clItem['self']); // alert("str="+str); html = html + str; }); html += '</ul>'; } html += '</li>'; }); localStorage.setItem("menu", html); } $('#ul_mid').html(localStorage.getItem("menu")); // =================================== setCssForMenu(); }, error : function(data, type, err) { alert("錯誤型別:" + type + "; 錯誤資訊:" + err); } }); }