JSF 2では、組み込みのajaxタグを使用して、事前定義されたキーワードでレンダリングまたは実行ターゲットを選択できます。例えば:
<f:ajax execute="@form" render="@this" />
カスタムコンポーネントでセレクターコードを再利用する簡単な方法はありますか?例えば:
<f:mycustomcomponent update="@form" />
jsf.js
スクリプトファイルによってクライアント側で処理されるという単純な理由から、そのための標準のJSFAPIはありません。JSF ajax JavaScriptは、などに置き換え@form
られます。正確に何のために必要かは不明ですが、基本的にに渡す必要がある場合は、実際に自分で操作する必要はありません。element.form.id
@this
element.id
jsf.ajax.request()
しかし、何らかの理由でコンポーネントツリー内のいくつかのJSFコンポーネントを参照するために実際に必要な場合、最善の策は実際にそれを自作することです。の中にいる場合UIComponent
、キックオフの例を次に示します(複数の値を含めることができ、スペースで区切ることができます)
if (update.charAt(0) != '@') {
component = findComponent(update);
} else if (update.equals("@all")) {
component = context.getViewRoot();
} else if (update.equals("@form")) {
component = getClosestParent(this, UIForm.class);
} else if (update.equals("@none")) {
component = null;
} else if (update.equals("@this")) {
component = this;
} else {
throw new IllegalArgumentException("Wrong update value " + update);
}
この小さなヘルパーメソッド(OmniFaces Components
ユーティリティクラスからコピー)を使用
public static <T extends UIComponent> T getClosestParent(UIComponent component, Class<T> parentType) {
UIComponent parent = component.getParent();
while (parent != null && !parentType.isInstance(parent)) {
parent = parent.getParent();
}
return parentType.cast(parent);
}