Primefaces コンポーネントを使用して動的ブレッドクラムを Web アプリに追加したいと考えています。ブレッドクラムに項目をプッシュするモデルを作成して、そのリンクの 1 つがたどられたときに末尾のリンクが削除されるようにしました。これはほとんどのシナリオで機能しますが、ブラッドクラムが期待どおりに動作しないことがあります。基本的に、ランディング ページを追跡するために、preRenderView
ナビゲート可能な各ページにリスナーを追加し、モデル更新ロジックをセッション スコープ Bean に実装しました。
<f:event type="preRenderView" listener="#{bcb.onRenderView}" />
<f:attribute name="pageName" value="ThisPage" />
リスナーはページ名を属性として受け取り、外部コンテキストから完全な URL (クエリ文字列を含む) を取得します。これらの情報は、 から作成された一意の ID とともに、モデルにプッシュUIViewRoot
される を構築するために使用されます。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);
}
このアプローチは実現可能ですか?ライフサイクル リスナーを活用する必要なく、ページ ナビゲーションを追跡するためのより良い方法を提案できますか? どうもありがとうございました。