Java树形菜单 递归实现

实体类:

public class ActionBO {

private String id;
private String parentId;
private String level;
private String menuTypeId;
private String menuType;
private String name;
private String url;
private Integer sort;
private String description;
private String commandName;
private Date created;
private String recordOperationFlag;
private String userId;

private List<ActionBO> actionBOs;


public ActionBO() {
super();
}


public ActionBO(Action action) {
this.id = action.getId();
this.parentId = action.getParentId();
this.name = action.getName();
this.level = action.getLevel();
this.menuTypeId = action.getMenuTypeId();
this.menuType = action.getMenuType();
this.url = action.getUrl();
this.sort = action.getSort();
this.description = action.getDescription();
this.recordOperationFlag = action.getRecordOperationFlag();
this.created = action.getCreated();
this.commandName = action.getCommandName();
}


public ActionBO(String id, String parentId, String level,
String menuTypeId, String menuType, String name, String url,
Integer sort, String description, String commandName, Date created,
String recordOperationFlag, String userId, List<ActionBO> actionBOs) {
super();
this.id = id;
this.parentId = parentId;
this.level = level;
this.menuTypeId = menuTypeId;
this.menuType = menuType;
this.name = name;
this.url = url;
this.sort = sort;
this.description = description;
this.commandName = commandName;
this.created = created;
this.recordOperationFlag = recordOperationFlag;
this.userId = userId;
this.actionBOs = actionBOs;
}

}


public class Action {

private String id;
private String parentId;
private String level;
private String menuTypeId;
private String menuType;
private String name;
private String url;
private Integer sort;
private String description;
private String commandName;
private Date created;
private String recordOperationFlag;

public Action() {
super();
}
public Action(String id, String parentId, String level, String menuTypeId,
String menuType, String name, String url, Integer sort,
String description, String commandName, Date created,
String recordOperationFlag) {
super();
this.id = id;
this.parentId = parentId;
this.level = level;
this.menuTypeId = menuTypeId;
this.menuType = menuType;
this.name = name;
this.url = url;
this.sort = sort;
this.description = description;
this.commandName = commandName;
this.created = created;
this.recordOperationFlag = recordOperationFlag;
}


public Action(JSONObject object) {
this.id = object.getString("id");
this.parentId = StringUtils.isEmpty(object.getString("parentId")) ? null : object.getString("parentId");
this.level = object.getString("level");
this.menuTypeId = object.getString("menuTypeId");
this.menuType = object.getString("menuType");
this.name = object.getString("name");
this.url = object.getString("url");
this.sort = object.getInteger("sort");
this.description = object.getString("description");
this.commandName = object.getString("commandName");
this.created = object.getDate("created");
this.recordOperationFlag = object.getString("recordOperationFlag");
}

}


主要方法:

@Override
public List<ActionBO> findAll(){
List<ActionBO> actionBOs = new ArrayList<ActionBO>();
List<Action> actions = actionDao.findAll();


for (Action Action : actions) {
if (StringUtils.isEmpty(Action.getParentId())) {
ActionBO actionBO = new ActionBO(Action);
setChildNode(actionBO, actions);
actionBOs.add(actionBO);
}
}
return actionBOs;
}


/**
* 设置树的子节点
*
* @param actionBO
* @param actions
*/
private void setChildNode(ActionBO actionBO, List<Action> actions) {
List<ActionBO> actionBOs = new ArrayList<ActionBO>();
for (Action Action : actions) {
if (!actionBO.getId().equals(Action.getId())
&& actionBO.getId().equals(Action.getParentId())) {
ActionBO actionBo = new ActionBO(Action);
if (isParent(actions, actionBo)) {
setChildNode(actionBo, actions);
}
actionBOs.add(actionBo);
}
}
actionBO.setActionBOs(actionBOs);
}


/**
* 是否为父节点
*
* @param actions
* @param actionBo
* @return
*/
private boolean isParent(List<Action> actions, ActionBO actionBo) {
boolean isParent = false;
for (Action Action : actions) {
if (!actionBo.getId().equals(Action.getId())
&& actionBo.getId().equals(Action.getParentId())) {
isParent = true;
break;
}
}
return isParent;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值