我想添加到我的web应用动态面包屑使用Primefaces组件。我创建了一个模型来将项目推送到面包屑导航栏,以便当其中一个链接被跟踪时,尾部链接被删除。这在大多数情况下都适用,但有时bradcrumb的行为并不像我期望的那样。基本上,为了跟踪登录页面,我在每个可导航页面上添加了一个preRenderView
侦听器,并在会话作用域bean中实现了模型更新逻辑。动态面包屑与primefaces
<f:event type="preRenderView" listener="#{bcb.onRenderView}" />
<f:attribute name="pageName" value="ThisPage" />
监听器接收页面名称作为属性,并从外部上下文获取完整的URL(包括查询字符串)这些信息与来自UIViewRoot
创建的唯一ID一起,被用来建立被在模型上推一个BreadCrumbItem
:模型的
public void onRenderView(ComponentSystemEvent evt) {
UIViewRoot root = (UIViewRoot)evt.getSource();
final String reqUrl = FacesUtils.getFullRequestURL();
String pageName = (String) evt.getComponent().getAttributes().get("pageName");
if(pageName != null) {
model.push(new BreadCrumbItem(root.createUniqueId(), pageName, reqUrl));
} else {
model.reset();
}
}
的push()
和reset()
方法是这样实现的:
/**
* When a link is pushed on the bread crumb, the existing items are analyzed
* and if one is found to be equal to the pushed one, the link is not added
* and all the subsequent links are removed from the list.
*
* @param link
* the link to be added to the bread crumb
*/
public void push(BreadCrumbItem link) {
boolean found = removeTrailing(link);
if(!found) {
addMenuItem(link);
}
}
/**
* Reset the model to its initial state. Only the home link is retained.
*/
public void reset() {
BreadCrumbItem home = new BreadCrumbItem();
removeTrailing(home);
}
这种方法是否可行?你能否提出一些更好的方法来跟踪页面导航,而不需要利用生命周期监听器?非常感谢你的帮助。