我想添加到我的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);
}
這種方法是否可行?你能否提出一些更好的方法來跟蹤頁面導航,而不需要利用生命週期監聽器?非常感謝你的幫助。