JSF 2 动态菜单处理的最佳实践?
我正在寻找使用 JSF 和 CDI 处理简单菜单的最佳实践。
我想要一个带有动态条目的顶部菜单。类似于(来自主模板):
<ul>
<c:forEach var="menuItem" items="#{navigationBean.topMenuItems}">
<c:choose>
<c:when test="#{navigationBean.isSelectedTop(menuItem.name)}">
<li class="current_page_item"><h:outputLink value="#{menuItem.url}"><h:outputText value="#{menuItem.name}" /></h:outputLink></li>
</c:when>
<c:otherwise>
<li><h:outputLink value="#{menuItem.url}"><h:outputText value="#{menuItem.name}" /></h:outputLink></li>
</c:otherwise>
</c:choose>
</c:forEach>
</ul>
NavigationBean(Sessionscoped,并保存NavigationItems列表)和NavigationItem只是bean。然后打开另一个视图,我将当前视图 ID 设置为在 navigatiobean 中选择的:
@PostConstruct
private void init (){
navigation.setSelectedTopMenu(getViewId());
}
问题是,为时已晚。页面已呈现,并且未选择视图 ID。
这种做法是不是一点都不好呢?有没有最佳实践?搜索 JSF 菜单和 JSF 导航会导致其他问题,因此我找不到任何内容。
提前致谢!
i am looking for a best practice handling a simple menu with JSF and CDI.
I would like to have a top menu with dynamic entries. Something like (from the main template):
<ul>
<c:forEach var="menuItem" items="#{navigationBean.topMenuItems}">
<c:choose>
<c:when test="#{navigationBean.isSelectedTop(menuItem.name)}">
<li class="current_page_item"><h:outputLink value="#{menuItem.url}"><h:outputText value="#{menuItem.name}" /></h:outputLink></li>
</c:when>
<c:otherwise>
<li><h:outputLink value="#{menuItem.url}"><h:outputText value="#{menuItem.name}" /></h:outputLink></li>
</c:otherwise>
</c:choose>
</c:forEach>
</ul>
NavigationBean (Sessionscoped, and holding a list of NavigationItems) and NavigationItem are only beans. Then opening another view I set the current view ID as selected within the navigatiobean:
@PostConstruct
private void init (){
navigation.setSelectedTopMenu(getViewId());
}
The problem is, it is too late. The page is already rendered and no view id is selected.
Is this approach not good at all? Are there any best practices? Searching for JSF menu and JSF navigation leads to other problems, so I couldn't find anything.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是使用组件绑定(例如到 h:panelGroup)并在绑定方法中生成整个菜单。
JSF 页面:
Bean:
One way is to use component binding (for example to h:panelGroup) and whole menu generate in binding method.
JSF page:
Bean: