树形菜单的动态实现
用数据库的表[menu]事先存储好菜单的信息,然后将菜单信息从数据库里取出来之后,
在JAVA里通过一个2重循环进行树形菜单Json数据的构建
前端采用EasyUI的tree来接受后端传过来的Json数据
1.数据库的表[menu]的创建(Mysql数据库)
CREATE TABLE `menu` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号 ',
`pid` bigint(20) DEFAULT NULL COMMENT '父节点编号 ',
`url` varchar(200) DEFAULT NULL COMMENT 'URL ',
`iconCls` varchar(50) DEFAULT NULL COMMENT '图标样式 ',
`text` varchar(50) DEFAULT NULL COMMENT '菜单名 ',
`code` varchar(20) DEFAULT NULL COMMENT '权限编号 ',
`level` bigint(10) DEFAULT NULL COMMENT '菜单级别 ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8
2.业务层实现
[MenuService]接口
/**
* 菜单服务接口
* @author fanlikuo
*
*/
public interface MenuService {
/**
* 菜单一览取得
* @return
*/
public List<Menu> getMenuList();
public List<Menu> createMenuTree(List<Menu> menuList);
}
[MenuServiceImp]类
/**
* 菜单服务类
* @author fanlikuo
*
*/
public class MenuServiceImp implements MenuService {
private MenuDao menuDao;
/**
* 菜单一览取得
*/
public List<Menu> getMenuList() {
return menuDao.getMenuList();
}
/**
* @param menuDao the menuDao to set
*/
public void setMenuDao(MenuDao menuDao) {
this.menuDao = menuDao;
}
/**
* 构建菜单树json ☆☆☆☆重要☆☆☆☆
* @param menuList 数据库取得的菜单信息
* @return 菜单树json
*/
public List<Menu> createMenuTree(List<Menu> menuList) {
List<Menu> nodeList = new ArrayList<Menu>();
if(menuList == null) {
return nodeList;
}
for (Menu menu1 : menuList) {
boolean mark = false;
for (Menu menu2 : menuList) {
if (menu1.getPid().equals(menu2.getId())) {
mark = true;
if (menu2.getChildren() == null) {
menu2.setChildren(new ArrayList<Menu>());
}
menu2.getChildren().add(menu1);
break;
}
}
if (!mark) {
nodeList.add(menu1);
}
}
return nodeList;
}
}
[MenuDao]接口
/**
*
* @author fanlikuo
*
*/
public interface MenuDao {
/**
* 菜单一览取得
* @return
*/
public List<Menu> getMenuList();
}
[MenuDao]类
public class MenuDaoImp extends BaseDaoImpl<DataSourceBean> implements MenuDao {
/**
* 菜单一览取得
*/
public List<Menu> getMenuList() {
Criteria proposal = this.getSession().createCriteria(Menu.class);
proposal.addOrder(Order.asc("id"));
return proposal.list();
}
}
[Menu]类
import java.util.List;
/**
* 菜单
* @author fanlikuo
*
*/
public class Menu {
/** 编号 */
private String id;
/** 父节点编号 */
private String pid;
/** URL */
private String url;
/** 菜单名 */
private String text;
/** 图标样式 */
private String iconCls;
/** 子菜单 */
private List<Menu> children;
/** 权限编号 */
private String code;
/** 菜单级别 */
private String level;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the pid
*/
public String getPid() {
return pid;
}
/**
* @param pid the pid to set
*/
public void setPid(String pid) {
this.pid = pid;
}
/**
* @return the text
*/
public String getText() {
return text;
}
/**
* @param text the text to set
*/
public void setText(String text) {
this.text = text;
}
/**
* @return the iconCls
*/
public String getIconCls() {
return iconCls;
}
/**
* @param iconCls the iconCls to set
*/
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
/**
* @return the children
*/
public List<Menu> getChildren() {
return children;
}
/**
* @param children the children to set
*/
public void setChildren(List<Menu> children) {
this.children = children;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the code
*/
public String getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}
/**
* @return the level
*/
public String getLevel() {
return level;
}
/**
* @param level the level to set
*/
public void setLevel(String level) {
this.level = level;
}
}
最终生成的JSON数据结构如下(2级菜单):
[
{"id":"1","text":"菜单1","level":"1",
"children":[
{"id":"11","text":"菜单11","level":"2","children":[],"pid":"1","code":"dbcx","iconCls":"zhangjie","url":"XXXXX.action"},
{"id":"12","text":"菜单12","level":"2","children":[],"pid":"1","code":"xzst","iconCls":"zhangjie","url":"XXXXX.action"},
{"id":"13","text":"菜单13","level":"2","children":[],"pid":"1","code":"stgl","iconCls":"guilei","url":"XXXXX.jsp"},
{"id":"14","text":"菜单14","level":"2","children":[],"pid":"1","code":"sjygl","iconCls":"guilei","url":"XXXXX.jsp"},
{"id":"15","text":"菜单15","level":"2","children":[],"pid":"1","code":"sjycs","iconCls":"guilei","url":"XXXXX.jsp"}],
"pid":"-1","code":"sjcx","iconCls":"database-oper","url":""},
{"id":"2","text":"菜单2","level":"1",
"children":[
{"id":"21","text":"菜单21","level":"2","children":[],"pid":"2","code":"yhgl","iconCls":"zixun","url":XXXXX.jsp"},
{"id":"22","text":"菜单22","level":"2","children":[],"pid":"2","code":"jsgl","iconCls":"user-fun","url":"XXXXX.jsp"},
{"id":"23","text":"菜单23","level":"2","children":[],"pid":"2","code":"qxgl","iconCls":"org","url":"XXXXX.jsp"},
{"id":"24","text":"菜单24","level":"2","children":[],"pid":"2","code":"sjzd","iconCls":"sys-dictionary","url":"XXXXX.jsp"}],
"pid":"-1","code":"xtgl","iconCls":"sys-conf","url":""}]