gpt4 book ai didi

java - 来自数据库的 Jsf2 primefaces 菜单栏

转载 作者:行者123 更新时间:2023-11-29 07:40:47 26 4
gpt4 key购买 nike

我是 Java 新手。我正在从数据库表生成一个菜单栏,我能够生成三个级别的子菜单,但我想知道如果有一天我们决定添加一个级别会怎样。我知道我的菜单生成逻辑很糟糕,但我找不到在primefaces上生成 child 的逻辑。这是我查询的sql结果。有人可以帮助获得解决方案吗?或者有什么想法可以创建一个循环而不是重复这两个查询?任何教程或任何源代码示例都会非常感激。 enter image description here

这是我生成 3 级菜单的源代码

connexion c1 = new connexion();
Connection con = c1.getConnected();

PreparedStatement ps = con
.prepareStatement("SELECT * FROM menu_options where parent_option_id is null");

// get first level menu options from database
ResultSet result = ps.executeQuery();

while (result.next()) {
DefaultSubMenu firstSubmenu = new DefaultSubMenu(
result.getString("name"));


PreparedStatement ps2 = con
.prepareStatement("SELECT * FROM menu_options where parent_option_id="
+ result.getInt("id"));

// get menu second level options from database
ResultSet result2 = ps2.executeQuery();
DefaultMenuItem item = null;

while (result2.next()) {

PreparedStatement ps3 = con
.prepareStatement("SELECT * FROM menu_options where parent_option_id="
+ result2.getInt("id"));

// get menu third level options from database
ResultSet result3 = ps3.executeQuery();
int rowcount = 0;
if (result3.last()) {
rowcount = result3.getRow();
result3.beforeFirst();
}

if (rowcount == 0) {
item = new DefaultMenuItem(result2.getString("name"));
item.setUrl(result2.getString("url"));
item.setIcon("ui-icon-arrowreturnthick-1-e");
// item.setId("fils");
firstSubmenu.addElement(item);

} else {
DefaultSubMenu firstSubmenu2 = null;
firstSubmenu2 = new DefaultSubMenu(result2.getString("name"));
while (result3.next()) {

item = new DefaultMenuItem(result3.getString("name"));
item.setUrl(result3.getString("url"));
item.setIcon("ui-icon-arrowreturnthick-1-e");
// item.setId("fils");
firstSubmenu2.addElement(item);

}
firstSubmenu.addElement(firstSubmenu2);
}
}
menu.addElement(firstSubmenu);
}

最佳答案

一个可能的解决方案可能是实现这些方法:

// Returns the item with no parent
private List<Item> searchParentlessItems();

// Returns the item with the given item as parent
private List<Item> searchItemChildren(Item item);

// Returns the children count
private Long countItemChildren(Item item);

// Build a SubMenu for the specified item
private SubMenu createSubMenu(Item item);

// Build a MenuItem for the specified item
private MenuItem createMenuItem(Item item);

那么一代可能看起来像

private MenuModel createMenuModel() {
MenuModel model = new DefaultMenuModel();
List<Item> rootItems = searchParentlessItems();
for (Item item : rootItems) {
boolean hasChildren = countItemChildren(item) > 0;
if (hasChildren) {
SubMenu subMenu = createSubMenu(item);
model.addElement(subMenu);
appendChildren(subMenu, item);
} else {
MenuItem menuItem = createMenuItem(item);
model.addElement(menuItem);
}
}
}

private void appendChildren(MenuGroup parentMenuItem, Item parentItem) {
List<Item> children = searchItemChildren(parentItem);
for (Item child : children) {
boolean hasChildren = countItemChildren(child) > 0;
if (hasChildren) {
SubMenu subMenu = createSubMenu(child);
parentMenuItem.addElement(subMenu);
appendChildren(subMenu, child);
} else {
MenuItem menuItem = createMenuItem(child);
parentMenuItem.addElement(menuItem);
}
}
}

关于java - 来自数据库的 Jsf2 primefaces 菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29120722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com