思路一:在权限内查出所有能用的菜单列表然后进行联系上父子关系
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);
}
});
}