I have a block of code from my project, and i want to call menu from database table that contains menu display name string, here is my code:
public static String subMenu(List<Menu> menus, String parentMenuId, boolean isSubMenu){
StringBuilder sb = new StringBuilder();
for(Menu menu : menus){
if(menu.getParentMenu()!=null && menu.getParentMenu().equalsIgnoreCase(parentMenuId)
&& menu.getActive() == Menu.MENU_ACTIVE){
String menuId = menu.getMenuId();
boolean isHasChild = false;
for(Menu menuChild : menus){
if(menuChild.getParentMenu()!=null &&
menuChild.getParentMenu().equalsIgnoreCase(menuId)){
isHasChild = true;
}
}
sb.append("<ul>");
if(isHasChild){
sb.append("<li>")
.append("<a href=\"#\">")
.append(menu.getDisplayName())
.append("</a>");
}else{
sb
.append("<li>")
.append("<a href=\""+ contextPath + menu.getActualUrl()+"\">")
.append(menu.getDisplayName())
.append("</a>")
.append("</li>");
}
sb.append("</ul>");
}
}
return sb.toString();
}
i dont know where but, i think i misplaced something on my code, so the output are like this
<ul>
<ul><li>...</li></ul>
<ul><li>...</li></ul>
<ul><li>...</li></ul>
</ul>
Can anyone tell me, and fix my code to get output like this?
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
thanks in advance.
sb.append("<ul>");
and
sb.append("</ul>");
has been moved out from the loop:
public static String subMenu(List<Menu> menus, String parentMenuId, boolean isSubMenu){
StringBuilder sb = new StringBuilder();
sb.append("<ul>");
for(Menu menu : menus){
if(menu.getParentMenu()!=null && menu.getParentMenu().equalsIgnoreCase(parentMenuId)
&& menu.getActive() == Menu.MENU_ACTIVE){
String menuId = menu.getMenuId();
boolean isHasChild = false;
for(Menu menuChild : menus){
if(menuChild.getParentMenu()!=null &&
menuChild.getParentMenu().equalsIgnoreCase(menuId)){
isHasChild = true;
}
}
if(isHasChild){
sb.append("<li>")
.append("<a href=\"#\">")
.append(menu.getDisplayName())
.append("</a>");
}else{
sb
.append("<li>")
.append("<a href=\""+ contextPath + menu.getActualUrl()+"\">")
.append(menu.getDisplayName())
.append("</a>")
.append("</li>");
}
}
sb.append("</ul>");
}
return sb.toString();
}
I make it an answer:
Move sb.append("<ul>")
and sb.append("</ul>")
outside of the loop:
sb.append("<ul>");
for (Menu menu : menus) { ... }
sb.append("</ul>")
Can you try this;
public static String subMenu(List<Menu> menus, String parentMenuId, boolean isSubMenu){
StringBuilder sb = new StringBuilder();
for(Menu menu : menus){
if(menu.getParentMenu()!=null && menu.getParentMenu().equalsIgnoreCase(parentMenuId)
&& menu.getActive() == Menu.MENU_ACTIVE){
String menuId = menu.getMenuId();
boolean isHasChild = false;
for(Menu menuChild : menus){
if(menuChild.getParentMenu()!=null &&
menuChild.getParentMenu().equalsIgnoreCase(menuId)){
isHasChild = true;
}
}
if(isHasChild){
sb.append("<li>")
.append("<a href=\"#\">")
.append(menu.getDisplayName())
.append("</a>");
}else{
sb
.append("<li>")
.append("<a href=\""+ contextPath + menu.getActualUrl()+"\">")
.append(menu.getDisplayName())
.append("</a>")
.append("</li>");
}
}
}
sb.insert(0, "<ul>");
sb.append("</ul>");
return sb.toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.