Java树形菜单 递归实现

时间:2021-06-27 12:39:47

实体类:

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;
}