2013-01-22 97 views
4

我想添加到我的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); 
} 

这种方法是否可行?你能否提出一些更好的方法来跟踪页面导航,而不需要利用生命周期监听器?非常感谢你的帮助。

回答

3

我已经为我的网络应用程序实现了我自己的一个,在我的情况下我没有使用p:breadCrumb组件,因为它使用按钮实现。

基本上,我有一个包含堆栈(导航堆栈)的@SessionScoped bean,将所有的url存储在breadcrumb中,并为它们中的每一个存储params。该视图(xhtml)部分由p:button元素组成,其中包含堆栈存储的url的outcome

当您导航到一个URL,相应的bean的f:event type="preRenderView"被称为(结合的方式,你这样做)和豆从URL的参数,可以认为它建立自己后入堆栈(而不是豆本身,因为它的@ViewScoped并将被销毁,只是网址和参数)。

如果你点击一个后退按钮,你会发送一个附加的参数,它表示按钮的索引。根据该索引,目的地bean知道你正在尝试恢复这样的视图,因此要求导航堆栈查看该视图params,并且导航堆栈删除其后的navegable。

我花了一段时间,但它功能齐全。祝你好运。使用session范围内保存当前导航状态时

编辑

要小心。它会影响所有打开的选项卡,因此可能不是您想要的,除非您希望最终用户只在一个选项卡中使用您的应用程序。无论如何,一般的可用性指导方针说你应该为你的面包屑使用类别而不是导航路径(HTTP is stateless,历史本身由浏览器保存)。 因此,一个动态的面包屑已经没有意义了,至少如果你使用不同的网址作为你的观点。