我想使用模型文件呈现嵌套的 2 级导航。导航可以改变。
有一个简单的 Bean NavigationItem,其中包含标题、目标、图标和可能的子项目列表。
导航的创建非常简单:
@ViewScoped
@Named
public class Navigation implements Serializable {
private List<NavigationItem> navigation = new ArrayList<>();
public List<NavigationItem> getValues() {
if (navigation == null || navigation.size() <= 0) {
NavigationItem navA = new NavigationItem("A", "icon-gauge", "site_a");
{
List<NavigationItem> subNav = new ArrayList<>();
subNav.add(new NavigationItem("1", "", "subsite_1"));
subNav.add(new NavigationItem("2", "", "subsite_2"));
subNav.add(new NavigationItem("3", "", "subsite_3"));
navA.setSubItems(subNav);
}
NavigationItem navB = new NavigationItem("B", "icon-layout", "site_b");
{
List<NavigationItem> subNav = new ArrayList<>();
subNav.add(new NavigationItem("4", "", "subsite_4"));
subNav.add(new NavigationItem("5", "", "subsite_5"));
navB.setSubItems(subNav);
}
NavigationItem navC = new NavigationItem("C", "icon-layout", "site_c");
{
List<NavigationItem> subNav = new ArrayList<>();
subNav.add(new NavigationItem("6", "", "subsite_6"));
navC.setSubItems(subNav);
}
navigation.add(navA);
navigation.add(navB);
navigation.add(navC);
navigation.add(new NavigationItem("Test", "icon-gauge", "site_d"));
}
return navigation;
}
}
这是我的JSF
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:b="http://butterfaces.org/components" >
<ul id="side-nav" class="main-menu navbar-collapse collapse">
<b:repeat value="#{navigation.values}" var="nav" rendered="true">
<li class="#{nav.hasSubItems()==false ? '' : 'has-sub'}">
<h:link outcome="#{nav.target}">
<i class="#{nav.icon}"></i>
<span class="title">#{nav.title}</span>
</h:link>
<ui:fragment rendered="#{nav.hasSubItems()}">
<ul class="nav collapse">
<b:repeat value="#{nav.subItems}" var="subNav" rendered="true">
<li>
<h:link outcome="#{subNav.target}">
<span class="title">#{subNav.title}</span>
</h:link>
</li>
</b:repeat>
</ul>
</ui:fragment>
</li>
</b:repeat>
</ul>
</ui:composition>
我的预期结果:
- 一个
- 1
- 2
- 3
- 乙
- 4
- 5
- C
- 6
- D
我的实际结果是:
- 一个
- 1
- 2
- 3
- 乙
- 1
- 2
- 3
- C
- 1
- 2
- 3
- D
有什么我想念或有错吗?我在 Tomcat 8 上使用 Mojarra 2.2.8